gpt4 book ai didi

python - 使用 matplotlib 的 3D 曲面图,使用数据框列输入数据

转载 作者:行者123 更新时间:2023-12-01 00:01:33 24 4
gpt4 key购买 nike

我有一个电子表格文件,我想输入该文件以使用 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()

enter image description here

这是我的 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 一起使用,但我得到的确实是一个图表,但它不起作用,这是使用这种方法的图表的样子: enter image description here非常感谢

最佳答案

基于重新网格化数据的不同方法,不需要在常规网格上指定原始数据[深受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()

enter image description here

关于python - 使用 matplotlib 的 3D 曲面图,使用数据框列输入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60319715/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com