使用matplotlib从“列表列表"中绘制3d曲面 [英] Plot a 3d surface from a 'list of lists' using matplotlib
问题描述
我进行了一些搜索,虽然可以找到许多有用的Meshgrid示例,但是没有一个方法可以清楚地说明我如何以我见过的各种方式将列表中的数据转换为可接受的形式谈论过.
I've searched around for a bit, and whhile I can find many useful examples of meshgrid, none shhow clearly how I can get data from my list of lists into an acceptable form for any of the varied ways I've seen talked about.
当涉及到numpy/matplotlib以及我所看到的建议的术语和步骤顺序时,我有些失落.
I'm a bit lost when it comes to numpy/matplotlib and the terminologies and sequences of steps that I have seen suggested.
我找到的最接近的是 matplotlib中的元组列表中的3d曲面
我有一个高度数据列表的列表.
I have a list of lists of height data.
data=[[h1,h2,h3,h...],
[h,h,h,h],
[h,h,h,h],
[h,h,h,h16]]
data[0][1]==h2
data[4][4]==h16
如何使用matplotlib/numpy等生成这些值的简单3d表面图?就像将颜色值作为z值的色图一样.我可以使用imshow()很好,因为它直接获取列表列表.我只是不确定我该如何将我所拥有的东西切成与plot_surface可能会同意的东西.
How do I produce a simple 3d surface plot of these values using matplotlib/numpy etc..? just like a colourmap with the color values as z values. I can use imshow() just fine as it takes a list of lists directly. I'm just not certain how I need to slice up what I've got into something that plot_surface may agree with.
推荐答案
如果要使用3d曲面,则必须生成x和y坐标.如果您不在乎它们是什么,而只需要表面,则生成给定长度的整数网格:
if you want a 3d-surface, you have to generate x and y coordinates. If you don't care what they are and just want the surface, generate a meshgrid of integers in the given length:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
data = np.array(data)
length = data.shape[0]
width = data.shape[1]
x, y = np.meshgrid(np.arange(length), np.arange(width))
fig = plt.figure()
ax = fig.add_subplot(1,1,1, projection='3d')
ax.plot_surface(x, y, data)
plt.show()
请参考 http://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html和 http://nbviewer.ipython.org/github/jrjohansson/scientific-python-lectures/blob/master/Lecture-4-Matplotlib.ipynb 了解更多信息
这篇关于使用matplotlib从“列表列表"中绘制3d曲面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!