简体   繁体   English

使用mplot3d在Python中绘制三个列表作为表面图

[英]Plotting three lists as a surface plot in python using mplot3d

I have three lists, X , Y , Z . 我有三个列表XYZ 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. X和Y列表仅包含3或4个唯一值-但是X和Y的每个组合都是唯一的,并且具有关联的Z值。

I need to produce a surface plot using .plot_surface . 我需要使用.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. 我知道我需要为此创建一个meshgrid ,但是鉴于我有包含重复数据的列表,因此我不知道如何生成该网格,并且保持Z列表的完整性至关重要。 I could also use tri_surf as this works straight away, but it is not quite what I need. 我也可以使用tri_surf因为它可以立即工作,但这不是我所需要的。

I'm using the mplot3d library of course. 我正在使用mplot3d库。

Given the scattered nature of your data set, I'd suggest tri_surf . 考虑到数据集的分散性,我建议使用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 . 由于您说的不是“您真正需要的”,因此您的另一种选择是创建一个meshgrid ,然后使用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表面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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