简体   繁体   English

如何使用matplotlib将数组正确绘制为表面?

[英]How to plot an array correctly as surface with matplotlib?

I'been trying to plot a 3D-image with an array as input. 我正在尝试绘制一个3D图像,其中一个数组作为输入。 For testing I made an little np.array 为了测试,我做了一点np.array

ar = np.array([[1,2,3],[4,5,6],[7,8,9]])

My idea was to use the x-positions as x-value, the y-positions as y-value and the value of each position as z-value. 我的想法是将x位置用作x值,将y位置用作y值,并将每个位置的值用作z值。

fig = plt.figure(figsize=(12,10))
ax = fig.add_subplot(111, projection='3d')

plt.title("Titel")
X = range(len(a))
Y = range(len(a))
Z = a
ax.plot_surface(X,Y,Z, cmap=plt.cm.Reds, cstride=1, rstride=1)

My problem is that I get an plot, but the plot isnt like I tought it gonna be... 我的问题是我得到了一个情节,但情节并不像我想的那样... 情节

I want a "normal" surface plot, like this: 我想要一个“正常”表面图,如下所示: 正态图

In the Docs is explained how to make 3D plot with functions as input, but I didnt find a good description how to do it with an array. 在Docs中说明了如何使用函数作为输入来制作3D图,但是我没有找到很好的描述如何使用数组来做。

Sry for the big pictures, I dont know how to make them smaller :/ 对大图很抱歉,我不知道如何缩小它们:/

Found the solution in this question: Plotting a 2D Array with Matplotlib 找到了以下问题的解决方案: 使用Matplotlib绘制2D阵列

Dont know why I didnt see it before. 不知道为什么我以前没看过。

Adding 新增中

x = range(len(a))
y = range(len(a))
X,Y = np.meshgrid(x,y)
Z = a

gives this plot: 给出这个情节: 情节

I dont know what exactly does "meshgrid"... gonna search a little bit more about it. 我不知道“ meshgrid”到底是什么。。。

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

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