简体   繁体   中英

Plotting three lists as a surface plot in python using mplot3d

I have three lists, X , Y , Z . Each piece of data is associated by index.

X = [1,1,1,1,2,2,2,2,3,3,3,3]
Y = [1,4,5,6,1,4,5,6,1,4,5,6]
Z = [2,6,3,6,2,7,4,6,2,4,2,3]

The X and Y lists only contain 3 or 4 unique values - but each combination of X and Y is unique and has an associated Z value.

I need to produce a surface plot using .plot_surface . I know I need to create a meshgrid for this, but I don't know how to produce this given i have lists containing duplicate data, and maintaining integrity with the Z list is crucial. I could also use tri_surf as this works straight away, but it is not quite what I need.

I'm using the mplot3d library of course.

Given the scattered nature of your data set, I'd suggest tri_surf . Since you're saying "it is not quite what [you] need", your other option is to create a meshgrid , then interpolate your input points with scipy.interpolate.griddata .

import numpy as np
import scipy.interpolate as interp
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

X = [1,1,1,1,2,2,2,2,3,3,3,3]
Y = [1,4,5,6,1,4,5,6,1,4,5,6]
Z = [2,6,3,6,2,7,4,6,2,4,2,3]

plotx,ploty, = np.meshgrid(np.linspace(np.min(X),np.max(X),10),\
                           np.linspace(np.min(Y),np.max(Y),10))
plotz = interp.griddata((X,Y),Z,(plotx,ploty),method='linear')

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(plotx,ploty,plotz,cstride=1,rstride=1,cmap='viridis')  # or 'hot'

Result:

<code> interp </ code> d表面

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM