从重复数组制作python图 [英] Make an python plot from recurring arrays
本文介绍了从重复数组制作python图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的数据排列如下:
x = [0,1,2,0,1,2,0,1,2]
y = [0,0,0,1,1,1,2,2,2]
z = [2,4,3,3,5,1,1,2,1]
我将如何绘制它以便我有一个 3x3 imshow 或等高线图?
How would I plot this up so that I have a 3x3 imshow or contour plot?
推荐答案
您可以使用 contourf 为此.它要求您的数据遵循网格.您可以使用以下方法创建2D网格:
You can use contourf for that. It requires that your data is following a mesh. You can create a 2D mesh using:
x, y = np.meshgrid(x, y)
然后为所有(x,y)
对和绘图运行 z
函数.
Then run the function for z
for all the (x, y)
pairs and plot.
import matplotlib.pyplot as plt
...
plt.contourf(x , y, z)
plt.show()
或者,您可以使用 3D 绘图,例如:
Alternatively you can have a 3D plot like:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
...
ax.plot_surface(x, y, z, color='b')
plt.show()
如果您无权访问生成 z
的函数.你必须插值
If you do not have access to the function that generates z
. You have to interpolate.
from scipy.interpolate import griddata
# points is the original pair (x, y)
grid_z0 = griddata(points, z, (grid_x, grid_y), method='nearest')
这篇关于从重复数组制作python图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文