绘制一个二维数组与mplot3d数组、mplot3d

2023-09-07 13:54:42 作者:情深已故

我有一个2D numpy的数组,我想绘制它的3D。我听说mplot3d,但我不能得到正常工作

I have a 2D numpy array and I want to plot it in 3D. I heard about mplot3d but I cant get to work properly

这里是我想要做的一个例子。我与尺寸(256,1024)的阵列。它应绘制三维图,其中x轴是从0到256的Y轴为0至1024和图形的z轴在每个条目显示阵列的值。

Here's an example of what I want to do. I have an array with the dimensions (256,1024). It should plot a 3D graph where the x axis is from 0 to 256 the y axis from 0 to 1024 and the z axis of the graph displays the value of of the array at each entry.

我怎么去呢?

推荐答案

这听起来像你要创建一个的表面情节(或者你可以绘制的线框情节或充满countour剧情。

It sounds like you are trying to create a surface plot (alternatively you could draw a wireframe plot or a filled countour plot.

从问题的信息,您可以尝试沿着线的东西:

From the information in the question, you could try something along the lines of:

import numpy
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# Set up grid and test data
nx, ny = 256, 1024
x = range(nx)
y = range(ny)

data = numpy.random.random((nx, ny))

hf = plt.figure()
ha = hf.add_subplot(111, projection='3d')

X, Y = numpy.meshgrid(x, y)  # `plot_surface` expects `x` and `y` data to be 2D
ha.plot_surface(X, Y, data)

plt.show()

显然,你需要选择更明智的数据比使用 numpy.random ,以获得合理的表面。

Obviously you need to choose more sensible data than using numpy.random in order to get a reasonable surface.