matplotlib-高度矩形阵列中的3d表面 [英] matplotlib - 3d surface from a rectangular array of heights
问题描述
我正在尝试在matplotlib中绘制一些HDF数据.使用h5py导入它们后,数据以数组形式存储,如下所示:
I am trying to plot some HDF data in matplotlib. After importing them using h5py, the data is stored in a form of array, like this:
array([[151, 176, 178],
[121, 137, 130],
[120, 125, 126])
在这种情况下,x和y值只是数组字段的索引,而z值是特定字段的值.在(x,y,z)形式中,它看起来像:
In this case, x and y values are just the indexes of the array's fields, while z value is the value of specific field. In the (x,y,z) form it would look like:
(1,1,151)
(2,1,176)
(3,1,178)
(1,2,121)
...
以此类推.
是否有一种简便的方法可以从此类数据绘制表面图?我知道我可以通过遍历整个数组将其更改为(x,y,z)元组,但是也许不需要它吗?
Is there an easy way to do a surface plot from this kind of data? I know I can change this to (x,y,z) tuples by iterating all over the array, but maybe it is not needed?
推荐答案
如果需要3-d表面图,则必须首先创建meshgrid
.您可以尝试:
If you want a 3-d surface plot, you have to create the meshgrid
first. You can try:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
X = np.arange(1, 10)
Y = np.arange(1, 10)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='hot', linewidth=0, antialiased=False)
ax.set_zlim(-1.01, 1.01)
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()
将会生成,
但是,如果唯一相关的信息在z值中,则可以简单地使用imshow
.在此,z值由其颜色表示.您可以通过以下方式实现此目标:
However, if the only relevant information is in the z-values, you can simply use imshow
. Here, z-values are represented by their color. You can achieve this by:
im = plt.imshow(Z, cmap='hot')
plt.colorbar(im, orientation='horizontal')
plt.show()
哪个会给,
这篇关于matplotlib-高度矩形阵列中的3d表面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!