简体   繁体   English

使用 numpy 网格绘制二维等高线图的最佳方法

[英]Best way to plot a 2d contour plot with a numpy meshgrid

i'm looking for the best way to create a contour plot using a numpy meshgrid.我正在寻找使用 numpy 网格创建等高线图的最佳方法。

I have excel data in columns simplyfied looking like this:我在列中的 excel 数据看起来像这样:

x data values: -3, -2, -1, 0, 1, 2 ,3, -3, -2, -1, 0, 1, 2, 3
y data values:  1,  1,  1, 1, 1, 1, 1,  2,  2,  2, 2, 2, 2, 2
z data values:  7 , 5,  6, 5, 1, 0, 9,  5,  3,  8, 3, 1, 0, 4

The x and y values define a 2d plane with the length (x-Axis) of 7 values and depth (y-Axis) of 2 values. x 和 y 值定义了一个 2d 平面,其长度(x 轴)为 7 个值,深度(y 轴)为 2 个值。 The z values define the colour at the corresponing points (more or less a z-Axis). z 值定义了相应点(或多或少是 z 轴)的颜色。

I've tried:我试过了:

import matplotlib.pyplot as plt
import numpy as np

x = [-3,-2,-1,0,1,2,3]

y = [1,2]

z = [7,5,6,5,1,0,9,5,3,8,3,1,0,4]

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

A = np.array(z)
B = np.reshape(A, (-1, 2))

fig = plt.figure()
ax1 = plt.contourf(x, y, B)

plt.show()

I'm pretty sure i'm not getting how the meshgrid works.我很确定我不明白 meshgrid 是如何工作的。 Do i have to use the whole List of x and y values for it to work?我是否必须使用整个 x 和 y 值列表才能工作?

How do i create a rectangular 2d plot with the length (x) of 7 and the depth (y) of 2 and the z values defining the shading/colour at the x and y values?如何创建长度 (x) 为 7、深度 (y) 为 2 且 z 值定义 x 和 y 值处的阴影/颜色的矩形 2d 图?

Thanks in advance guys!在此先感谢各位!

Try尝试

x_, y_ = np.meshgrid(x, y)
z_grid = np.array(z).reshape(2,7)
fig = plt.figure()
ax1 = plt.contourf(x_,y_,z_grid)
plt.show()

Edit: If you would like to smooth, as per your comment, you can try something like scipy.ndimage.zoom() as described here , ie, in your case编辑:如果你想平滑,根据你的评论,你可以尝试像这里描述的scipy.ndimage.zoom()类的东西,即,在你的情况下

from scipy import ndimage

z_grid = np.array(z).reshape(2,7)
z_grid_interp = ndimage.zoom(z_grid, 100)
x_, y_ = np.meshgrid(np.linspace(-3,3,z_grid_interp.shape[1]),np.linspace(1,2,z_grid_interp.shape[0]))

and then plot as before:然后像以前一样绘制:

fig = plt.figure()
ax1 = plt.contourf(x_,y_,z_grid_interp)
plt.show()

图像在这里

This is one way where you use the shape of the meshgrid ( X or Y ) to reshape your z array.这是使用网格的形状( XY )来重塑z数组的一种方式。 You can, moreover, add a color bar using plt.colorbar()此外,您可以使用plt.colorbar()添加颜色条

import matplotlib.pyplot as plt
import numpy as np

x = [-3,-2,-1,0,1,2,3]
y = [1,2]
z = np.array([7,5,6,5,1,0,9,5,3,8,3,1,0,4])

X, Y = np.meshgrid(x, y)
print (X.shape, Y.shape)
# (2, 7) (2, 7) Both have same shape
Z = z.reshape(X.shape) # Use either X or Y to define shape

fig = plt.figure()
ax1 = plt.contourf(X, Y, Z)
plt.colorbar(ax1)
plt.show()

在此处输入图片说明

def f(x, y):
    return np.sin(x) ** 10 + np.cos(10 + y * x) * np.cos(x)
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2, 3 )
y = np.linspace(0, 3, 4)
X, Y = np.meshgrid(x, y)
Z = f(X, Y)
plt.contour(X, Y, Z, cmap='RdGy');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM