【问题标题】:3D surface graph with matplotlib using dataframe columns to input the data使用数据框列输入数据的 matplotlib 3D 曲面图
【发布时间】:2020-06-04 18:22:34
【问题描述】:

我有一个电子表格文件,我想输入它以在 Python 中使用 Matplotlib 创建一个 3D 曲面图。

我使用了plot_trisurf,它有效,但我需要将轮廓轮廓投影到可以使用表面函数like this example 获得的图形上。

我正在努力将我的 Z 数据排列在一个二维数组中,我可以用它来输入 plot_surface 方法。我尝试了很多东西,但似乎都没有效果。

这就是我的工作,使用plot_trisurf

import matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

import numpy as np
import pandas as pd

df=pd.read_excel ("/Users/carolethais/Desktop/Dissertação Carol/Códigos/Resultados/res_02_0.5.xlsx")

fig = plt.figure()
ax = fig.gca(projection='3d')
# I got the graph using trisurf 
graf=ax.plot_trisurf(df["Diametro"],df["Comprimento"], df["temp_out"], cmap=matplotlib.cm.coolwarm)

ax.set_xlim(0, 0.5)
ax.set_ylim(0, 100)
ax.set_zlim(25,40)
fig.colorbar(graf, shrink=0.5, aspect=15)
ax.set_xlabel('Diâmetro (m)')
ax.set_ylabel('Comprimento (m)')
ax.set_zlabel('Temperatura de Saída (ºC)')

plt.show()

这是我的 df 数据框的一部分:

       Diametro  Comprimento   temp_out
0      0.334294     0.787092  34.801994
1      0.334294     8.187065  32.465551
2      0.334294    26.155976  29.206090
3      0.334294    43.648591  27.792126
4      0.334294    60.768219  27.163233
...         ...          ...        ...
59995  0.437266    14.113660  31.947302
59996  0.437266    25.208851  30.317583
59997  0.437266    33.823035  29.405461
59998  0.437266    57.724209  27.891616
59999  0.437266    62.455890  27.709298

我尝试this approach 将导入的数据与plot_surface 一起使用,但我得到的确实是一个图表,但它不起作用,这是图表在这种方法下的样子: 非常感谢

【问题讨论】:

  • 在没有任何重要信息的情况下很难为您提供帮助。你做了什么,你的数据是如何组织的,你尝试了什么......
  • 对不起,这是我的第一个问题。如果您可以看一下,我会用更多信息编辑问题,我会很高兴。谢谢。
  • 看来你有60000个点,它们是在一个规则的网格上排列的吗? 300x200 也许?如果这些点确实在常规网格上,您可以按照plot_surface 所需的格式重塑数据

标签: python matplotlib graph 3d


【解决方案1】:

一种基于重新网格化数据的不同方法,不需要在常规网格上指定原始数据 [深受this example 的启发;-]。

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.tri as tri
from mpl_toolkits.mplot3d import Axes3D

np.random.seed(19880808)

# compute the sombrero over a cloud of random points
npts = 10000
x, y = np.random.uniform(-5, 5, npts), np.random.uniform(-5, 5, npts)
z = np.cos(1.5*np.sqrt(x*x + y*y))/(1+0.33*(x*x+y*y))

# prepare the interpolator
triang = tri.Triangulation(x, y)
interpolator = tri.LinearTriInterpolator(triang, z)

# do the interpolation
xi = yi = np.linspace(-5, 5, 101)
Xi, Yi = np.meshgrid(xi, yi)
Zi = interpolator(Xi, Yi)

# plotting
fig = plt.figure()
ax = fig.gca(projection='3d')
norm = plt.Normalize(-1,1)
ax.plot_surface(Xi, Yi, Zi,
                cmap='inferno',
                norm=plt.Normalize(-1,1))
plt.show()

【讨论】:

  • 对不起,我没有早点回复......它工作。非常感谢!
【解决方案2】:

您的数据由 3 个 1D 数组组成,因此使用 plot_trisurf 绘制它们是直接的,但您需要使用 plot_surface 才能将等值线投影到坐标平面上...您需要重塑数据。

您似乎有 60000 个数据点,在下面我假设您有一个规则网格,x 方向有 300 个点,y 方向有 200 个点 - 但重要的是规则网格。

下面的代码显示

  1. plot_trisurf 的使用(使用较粗的网格),类似于您的代码;
  2. reshaping的正确使用及其在plot_surface中的应用;
    注意reshaping中的行数对应于number y 中的点数和 x 中点数的列数;
  3. 和 4. 不正确地使用重塑,生成的子图不知何故 类似于您展示的情节,也许您只需要修复数字 行和列。

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

x, y = np.arange(30)/3.-5, np.arange(20)/2.-5
x, y = (arr.flatten() for arr in np.meshgrid(x, y))
z = np.cos(1.5*np.sqrt(x*x + y*y))/(1+0.1*(x*x+y*y))

fig, axes = plt.subplots(2, 2, subplot_kw={"projection" : "3d"})
axes = iter(axes.flatten())

ax = next(axes)
ax.plot_trisurf(x,y,z, cmap='Reds')
ax.set_title('Trisurf')

X, Y, Z = (arr.reshape(20,30) for arr in (x,y,z))
ax = next(axes)
ax.plot_surface(X,Y,Z, cmap='Reds')
ax.set_title('Surface 20×30')

X, Y, Z = (arr.reshape(30,20) for arr in (x,y,z))
ax = next(axes)
ax.plot_surface(X,Y,Z, cmap='Reds')
ax.set_title('Surface 30×20')

X, Y, Z = (arr.reshape(40,15) for arr in (x,y,z))
ax = next(axes)
ax.plot_surface(X,Y,Z, cmap='Reds')
ax.set_title('Surface 40×15')

plt.tight_layout()
plt.show()

【讨论】:

  • 非常感谢,帮了大忙。现在我意识到我没有一个规则的网格。我的 x 有 1000 个唯一数字,我的 y 有 60000。这些数字彼此非常接近(例如 0.00112 - 0.00113),但它们并不完全相同。所以,为了克服这个问题,我需要做这样的事情:matplotlib.org/3.1.1/gallery/images_contours_and_fields/… ?
  • “不规则数据网格”看起来像是要走的路......我会发布一个例子
猜你喜欢
  • 2017-06-17
  • 1970-01-01
  • 1970-01-01
  • 2019-10-02
  • 2018-01-02
  • 2012-02-27
  • 2021-04-06
  • 1970-01-01
相关资源
最近更新 更多