Plot a 3d surface from a 'list of lists' u

2020-07-15 15:29发布

问题:

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.

I'm a bit lost when it comes to numpy/matplotlib and the terminologies and sequences of steps that I have seen suggested.

The closest I found was Plotting a 3d surface from a list of tuples in matplotlib

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

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.

回答1:

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()

please refer to http://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html and http://nbviewer.ipython.org/github/jrjohansson/scientific-python-lectures/blob/master/Lecture-4-Matplotlib.ipynb for further information