matplotlib - 来自高度矩形阵列的 3d 表面 [英] matplotlib - 3d surface from a rectangular array of heights

查看:24
本文介绍了matplotlib - 来自高度矩形阵列的 3d 表面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 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屋!

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