具有 X Y Z 数据的 2D 密度图 [英] 2D Density Plot with X Y Z data

查看:102
本文介绍了具有 X Y Z 数据的 2D 密度图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制x,y和z(高程)的2d地形图.我按照以下链接中的步骤操作,但我得到了非常奇怪的情节.

任何帮助将不胜感激

我为此编写了Jupyter Notebook主题,请查看其他几种插值方法,例如克里金法和样条拟合.

I am trying to plot 2d terrain map with x,y and z (elevation). I followed the steps from the following link but I am getting very weird plot.

Python : 2d contour plot from 3 lists : x, y and rho?

I spent almost half day searching but got nowhere.

import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate

# import data:
import xlrd
loc = "~/Desktop/Book4.xlsx"
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
sample=500

# Generate array:
x=np.array(sheet.col_values(0))[0:sample]
y=np.array(sheet.col_values(1))[0:sample]
z=np.hamming(sample)[0:sample][:,None]

# Set up a regular grid of interpolation points

xi, yi = np.meshgrid(x, y)

# Interpolate
rbf = scipy.interpolate.Rbf(x, y, z, function='cubic')
zi = rbf(xi, yi)
# Plot
plt.imshow(zi, vmin=z.min(), vmax=z.max(), origin='lower',
           extent=[x.min(), x.max(), y.min(), y.max()])
plt.colorbar()
plt.show()

The first of the following fig is what I am getting and the last one is how it should look like.

Any help shall be appreciated

Link to data file

解决方案

I think the problem is that the data you're giving it is not smooth enough to interpolate with the default parameters. Here's one approach, using mgrid instead of meshgrid:

import numpy as np
import pandas as pd
from scipy.interpolate import Rbf

# fname is your data, but as a CSV file.
data = pd.read_csv(fname).values
x, y = data.T

x_min, x_max = np.amin(x), np.amax(x)
y_min, y_max = np.amin(y), np.amax(y)

# Make a grid with spacing 0.002.
grid_x, grid_y = np.mgrid[x_min:x_max:0.002, y_min:y_max:0.002]

# Make up a Z.
z = np.hamming(x.size)

# Make an n-dimensional interpolator.
rbfi = Rbf(x, y, z, smooth=2)

# Predict on the regular grid.
di = rbfi(grid_x, grid_y)

Then you can look at the result:

import matplotlib.pyplot as plt

plt.imshow(di)

I get:

I wrote a Jupyter Notebook on this topic recently, check it out for a few other interpolation methods, like kriging and spline fitting.

这篇关于具有 X Y Z 数据的 2D 密度图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆