使用 mplot3d 绘制二维数组 [英] Plotting a 2d Array with mplot3d

查看:25
本文介绍了使用 mplot3d 绘制二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 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) 的数组.它应该绘制一个 3D 图形,其中 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.

我该怎么做?

推荐答案

听起来您正在尝试创建一个 surface 图(或者你可以画一个 线框 图或填充的countour plot.

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.

这篇关于使用 mplot3d 绘制二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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