Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot a 3d surface from a 'list of lists' using matplotlib

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.

like image 737
Matt Warren Avatar asked Jul 23 '14 20:07

Matt Warren


People also ask

Can matplotlib Pyplot be used to display 3D plots?

Matplotlib was introduced keeping in mind, only two-dimensional plotting. But at the time when the release of 1.0 occurred, the 3d utilities were developed upon the 2d and thus, we have 3d implementation of data available today! The 3d plots are enabled by importing the mplot3d toolkit.

What function would we use to plot a 3D surface in matplotlib?

Creating 3D surface Plot Surface plots are created by using ax. plot_surface() function.


1 Answers

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

like image 183
MaxNoe Avatar answered Oct 13 '22 04:10

MaxNoe