垂直填充3D Matplotlib图 [英] Vertically fill 3d matplotlib plot
问题描述
我有一个使用matplotlib制作的3d图.现在,我想填充绘制的线和x,y轴之间的垂直空间,以突出显示z轴上线的高度.在2d图上,这可以使用fill_between
完成,但对于3d图来说似乎没有任何相似之处.有人可以帮忙吗?
I have a 3d plot made using matplotlib. I now want to fill the vertical space between the drawn line and the x,y axis to highlight the height of the line on the z axis. On a 2d plot this would be done with fill_between
but there does not seem to be anything similar for a 3d plot. Can anyone help?
这是我当前的代码
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()
和当前情节
为了清楚起见,我想对x,y轴交点进行垂直填充,不是这个...
just to be clear I want a vertical fill to the x,y axis intersection NOT this...
推荐答案
您是对的.似乎2D绘图函数 fill_between
在3D绘图中没有等效项.我建议的解决方案是将数据转换为3D多边形.这是相应的代码:
You're right. It seems that there is no equivalent in 3D plot for the 2D plot function fill_between
. The solution I propose is to convert your data in 3D polygons. Here is the corresponding code:
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()
它产生下图:
我希望这会对您有所帮助.
I hope this will help you.
这篇关于垂直填充3D Matplotlib图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!