Ale*_*r K 5 python numpy matplotlib
我正在寻找使用 numpy 网格创建等高线图的最佳方法。
我的列中有 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
Run Code Online (Sandbox Code Playgroud)
x 和 y 值定义一个 2d 平面,其长度(x 轴)为 7 个值,深度(y 轴)为 2 个值。z 值定义相应点(或多或少是 z 轴)处的颜色。
我试过了:
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()
Run Code Online (Sandbox Code Playgroud)
我很确定我不明白网格是如何工作的。我是否必须使用 x 和 y 值的整个列表才能正常工作?
如何创建长度 (x) 为 7、深度 (y) 为 2 以及定义 x 和 y 值处的阴影/颜色的 z 值的二维矩形图?
预先感谢各位!
尝试
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()
Run Code Online (Sandbox Code Playgroud)
编辑:如果您想根据您的评论进行平滑处理,您可以尝试此处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]))
Run Code Online (Sandbox Code Playgroud)
然后像以前一样绘制:
fig = plt.figure()
ax1 = plt.contourf(x_,y_,z_grid_interp)
plt.show()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12245 次 |
| 最近记录: |