bash
复制代码
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["font.sans-serif"] = ["SimHei"] # 设置字体
plt.rcParams["axes.unicode_minus"] = False # 该语句解决图像中的"-"负号的乱码问题
# 数据
regions = ['东北', '中南', '华东', '华北', '西南']
years = [2015, 2016, 2017, 2018, 2019]
data = np.random.randint(50, 100, size=(5, 5))
# 创建图形和3D轴
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')
# 设置颜色
colors = ['r', 'g', 'b', 'y', 'c']
# 绘制立柱
x_pos = np.arange(len(regions))
y_pos = np.arange(len(years))
x_pos, y_pos = np.meshgrid(x_pos, y_pos)
x_pos = x_pos.flatten()
y_pos = y_pos.flatten()
z_pos = np.zeros_like(x_pos)
dx = dy = 0.8
dz = data.flatten()
for i, (x, y, z, h) in enumerate(zip(x_pos, y_pos, z_pos, dz)):
ax.bar3d(x, y, z, dx, dy, h, color=colors[i % len(colors)], alpha=0.8)
# 设置坐标轴
ax.set_xticks(np.arange(len(regions)))
ax.set_yticks(np.arange(len(years)))
ax.set_xticklabels(regions)
ax.set_yticklabels(years)
ax.set_zlabel('销售额')
# 设置视角
ax.view_init(elev=20, azim=45)
# 设置标题
plt.title('每年每地区的销售额')
# 显示图形
plt.tight_layout()
plt.show()