gpt4 book ai didi

python - 垂直填充 3d matplotlib 图

转载 作者:行者123 更新时间:2023-12-05 07:09:45 26 4
gpt4 key购买 nike

我有一个使用 matplotlib 制作的 3d 图。我现在想填充绘制的线和 x、y 轴之间的垂直空间,以突出显示 z 轴上线的高度。在 2d 图上,这将通过 fill_between 完成,但对于 3d 图似乎没有任何相似之处。谁能帮忙?

这是我当前的代码

from stravalib import Client
import matplotlib as mpl
import numpy as np
import matplotlib.pyplot as plt

... code to get the data ....

mpl.rcParams['legend.fontsize'] = 10

fig = plt.figure()
ax = fig.gca(projection='3d')
zi = alt
x = df['x'].tolist()
y = df['y'].tolist()
ax.plot(x, y, zi, label='line')
ax.legend()
plt.show()

和当前情节

enter image description here

需要说明的是,我想要垂直填充到 x,y 轴交点,不是这个...

enter image description here

最佳答案

你是对的。似乎 3D 绘图中没有 2D 绘图函数的等价物 fill_between .我建议的解决方案是将您的数据转换为 3D 多边形。下面是相应的代码:

import math as mt
import matplotlib.pyplot as pl
import numpy as np
import random as rd

from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection


# Parameter (reference height)
h = 0.0

# Code to generate the data
n = 200
alpha = 0.75 * mt.pi
theta = [alpha + 2.0 * mt.pi * (float(k) / float(n)) for k in range(0, n + 1)]
xs = [1.0 * mt.cos(k) for k in theta]
ys = [1.0 * mt.sin(k) for k in theta]
zs = [abs(k - alpha - mt.pi) * rd.random() for k in theta]

# Code to convert data in 3D polygons
v = []
for k in range(0, len(xs) - 1):
x = [xs[k], xs[k+1], xs[k+1], xs[k]]
y = [ys[k], ys[k+1], ys[k+1], ys[k]]
z = [zs[k], zs[k+1], h, h]
#list is necessary in python 3/remove for python 2
v.append(list(zip(x, y, z)))
poly3dCollection = Poly3DCollection(v)

# Code to plot the 3D polygons
fig = pl.figure()
ax = Axes3D(fig)
ax.add_collection3d(poly3dCollection)
ax.set_xlim([min(xs), max(xs)])
ax.set_ylim([min(ys), max(ys)])
ax.set_zlim([min(zs), max(zs)])
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")

pl.show()

它产生下图:

enter image description here

希望对您有所帮助。

关于python - 垂直填充 3d matplotlib 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61462960/

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