我怎么能画一个matplotlib的3d酒吧,但不是与方柱,而与圆柱

ia2d9nvy  于 10个月前  发布在  其他
关注(0)|答案(1)|浏览(43)

我想使用matplotlib来绘制一个3d条形图,但给出的演示只使用了方柱。


的数据
相反,我想绘制一个带有圆柱形条的3D条形图,如下图所示:



使用matplotlib可以做到这一点吗?

yr9zkbsy

yr9zkbsy1#

有可能。您将不得不使自己的圆柱形物体,并正确地放置它们。我已经为你的问题修改了答案here。我确信有一种方法可以通过深入研究Poly3D类或指定其他顶点来调整圆柱体属性,但这应该是一个良好的开端。

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

def cylinder(x, y, z, r=1, color='red'):
    phi = np.linspace(0, 360, 200) / 180.0 * np.pi
    z = np.linspace(0, z, z)
    PHI, Z = np.meshgrid(phi, z)
    CP = r * np.cos(PHI) + x
    SP = r * np.sin(PHI) + y
    XYZ = np.dstack([CP, SP, Z])
    verts = np.stack(
        [XYZ[:-1, :-1], XYZ[:-1, 1:], XYZ[1:, 1:], XYZ[1:, :-1]], axis=-2).reshape(-1, 4, 3)

    poly3 = Poly3DCollection(verts, facecolor=color, edgecolor="none")
    return poly3


fig = plt.figure()
ax = fig.add_subplot(121, projection='3d')

colors = (plt.cm.RdBu_r(i) for i in np.linspace(0, 1, 25))

for x in range(1, 6):
    for y in range(1, 6):
        cyl = cylinder(x=x, y=y, z=x*y, r=0.5, color=next(colors))
        cyl.set_alpha(0.7)
        ax.add_collection3d(cyl)

ax.set_xlabel('X')
ax.set_xlim3d(0.5, 5.5)
ax.set_ylabel('Y')
ax.set_ylim3d(0.5, 5.5)
ax.set_zlabel('Z')
ax.set_zlim3d(-25, 25)

字符串
这是输出:

的数据

相关问题