如何使用 Matplotlib 在 Python 中渲染 3D 直方图?


若要在 Python 中渲染 3D 直方图,我们可以采取以下步骤:

  • 设置图形尺寸并调整子图之间以及周围的填充。

  • 使用 figure() 方法创建一个新图形或激活一个现有图形。

  • 将一个坐标轴以子图布置的方式添加到当前图形中。

  • 使用 numpy 创建 x3、y3 和 z3 数据点。

  • 使用 numpy 创建 dx、dydz 数据点。

  • 使用 bar3d() 方法绘制 3D 条形

  • 要隐藏坐标,请使用按名称划分的 axis('off') 类。

  • 要显示图形,请使用 show() 方法。

示例

import numpy as np
from matplotlib import pyplot as plt

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

fig = plt.figure()

ax1 = fig.add_subplot(111, projection='3d')

x3 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y3 = [5, 6, 7, 8, 2, 5, 6, 3, 7, 2]
z3 = np.zeros(10)

dx = np.ones(10)
dy = np.ones(10)
dz = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

ax1.bar3d(y3, x3, z3, dx, dy, dz, color="red")
ax1.axis('off')
plt.show()

输出

更新于: 2021-06-03

3K+ 浏览量

开启你的 职业生涯

完成课程认证

开始
广告
© . All rights reserved.