使用mplot3d在python中绘制三个列表作为表面图(Plotting three lists as a surface plot in python using mplot3d)

系统教程 行业动态 更新时间:2024-06-14 17:03:52
使用mplot3d在python中绘制三个列表作为表面图(Plotting three lists as a surface plot in python using mplot3d)

我有三个列表, X , Y , Z 。 每条数据都通过索引关联。

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]

X和Y列表仅包含3或4个唯一值 - 但X和Y的每个组合都是唯一的,并且具有关联的Z值。

我需要使用.plot_surface生成表面图。 我知道我需要为此创建一个meshgrid ,但我不知道如何生成这个,因为我有包含重复数据的列表,并且保持Z列表的完整性至关重要。 我也可以使用tri_surf因为它可以直接使用,但它不是我需要的。

我当然正在使用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.

最满意答案

鉴于数据集的分散性,我建议使用tri_surf 。 因为你说“它不是你需要的”,你的另一个选择是创建一个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'

结果:

 interp </ code> d surface

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:

interpd surface

更多推荐

本文发布于:2023-04-24 12:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/6ed3d154c1104f7cd4b439f875ca5c8f.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表面   列表   python   mplot3d   plot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!