垂直填充3d matplotlib图

问题描述 投票:2回答:1

我有一个使用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

python matplotlib plot 3d
1个回答
5
投票

你是对的。似乎2D绘图函数fill_between在3D绘图中没有等价物。我建议的解决方案是转换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

我希望这能帮到您。

© www.soinside.com 2019 - 2024. All rights reserved.