其他分享
首页 > 其他分享> > matplotlib bar3d画3d柱状图

matplotlib bar3d画3d柱状图

作者:互联网

Rendering

bar3d效果:

Code

import numpy as np
import matplotlib
matplotlib.rcParams['font.family'] = 'Times New Roman'
matplotlib.rcParams['mathtext.default'] = 'regular'
import matplotlib.pyplot as plt
# from mpl_toolkits.mplot3d import Axes3D


COLOR = ["blue", "cornflowerblue", "mediumturquoise", "goldenrod", "yellow"]
lambda1 = lambda2 = [10 ** x for x in range(-2, 3)]


# x, y: position
x = list(range(len(lambda1)))
y = list(range(len(lambda2)))
x_tickets = [str(_x) for _x in lambda1]
y_tickets = [str(_x) for _x in lambda2]

# acc = np.random.rand(len(x), len(y))
acc = np.arange(len(x) * len(y)).reshape(len(x), len(y)) + 1
acc = acc / acc.max()
xx, yy = np.meshgrid(x, y)
# print(xx)
# print(yy)
color_list = []
for i in range(len(x)):
    c = COLOR[i]
    color_list.append([c] * len(y))
color_list = np.asarray(color_list)
# print(color_list)
xx_flat, yy_flat, acc_flat, color_flat = \
    xx.ravel(), yy.ravel(), acc.ravel(), color_list.ravel()
# print(xx_flat)
# print(yy_flat)


# fig, ax = plt.subplots(projection="3d")
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
ax.bar3d(xx_flat - 0.35, yy_flat - 0.35, 0, 0.7, 0.7, acc_flat,
    color=color_flat,  # 颜色
    edgecolor="black",  # 黑色描边
    shade=True)  # 加阴影

# 座标轴名
ax.set_xlabel(r"$\lambda_1$")
ax.set_ylabel(r"$\lambda_2$")
ax.set_zlabel("ACC")

# 座标轴范围
ax.set_zlim((0, 1.01))

# 座标轴刻度标签
# 似乎要 `set_*ticks` 先,再 `set_*ticklabels`
# has to call `set_*ticks` to mount `ticklabels` to corresponding `ticks` ?
ax.set_xticks(x)
ax.set_xticklabels(x_tickets)
ax.set_yticks(y)
ax.set_yticklabels(y_tickets)

# 保存
plt.tight_layout()
fig.savefig("bar3d.png", bbox_inches='tight', pad_inches=0)
plt.close()

References

  1. Demo of 3D bar charts
  2. 3D plots as subplots
  3. matplotlib实现三维柱状图
  4. 第三十一章 3D 条形图
  5. Grouped bar chart with labels
  6. apply color map to mpl_toolkits.mplot3d.Axes3D.bar3d
  7. List of named colors
  8. How to make bar3d plot with transparent faces and non-transparent edges?

标签:flat,set,color,list,len,柱状图,bar3d,matplotlib,ax
来源: https://blog.csdn.net/HackerTom/article/details/119322168