如何在Matplotlib中为3D条形图创建图例?

如何在Matplotlib中为3D条形图创建图例?

要在Matplotlib中为3D条形图创建图例,我们可以使用legend()方法绘制3D条形图并放置图例。

步骤

  • 设置图形大小并调整子图之间和周围的填充。
  • 使用 figure() 方法创建一个新图或激活现有图。
  • 作为子图安排的一部分,向图中添加轴。
  • 使用numpy创建数据x3、y3、z3、dx、dy和dz的列表。
  • 使用 bar3d() 方法绘制3D条形图。
  • 为图例放置创建矩形轴。
  • 使用 legend() 方法为条形图放置图例。
  • 使用 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(x3, y3, z3, dx, dy, dz, color="green")
b1 = plt.Rectangle((0, 0), 1, 1, fc="green")
ax1.bar3d(y3, x3, z3, dx, dy, dz, color="red")
b2 = plt.Rectangle((0, 0), 1, 1, fc="red")
ax1.legend([b1, b2], ['type1', 'type2'])
plt.show()

输出

如何在Matplotlib中为3D条形图创建图例?

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程