function:
matplotlib.pyplot.bar
代码:
import matplotlib.pyplot as plt
import numpy as np
# test_data
# X轴
species = (
"species_1",
"species_2",
"species_3",
)
# Y轴
weight_counts = {
"layer_1" : np.random.randint(5, 15, (3,)),
"layer_2" : np.random.randint(5, 15, (3,)),
"layer_3" : np.random.randint(5, 15, (3,)),
"layer_4" : np.random.randint(5, 15, (3,)),
"layer_5" : np.random.randint(5, 15, (3,)),
"layer_6" : np.random.randint(5, 15, (3,)),
}
# 条形的宽度
width = 0.5
bottom = np.zeros(3)
# 设置图形参数
plt.figure(figsize=(12,15))
plt.title('Stack bar chart', fontsize=20)
plt.xlabel('Species', fontsize=14)
plt.ylabel('Number', fontsize=14)
# 绘制堆叠柱状图
for boolean, weight_count in weight_counts.items():
plt.bar(species, weight_count, width, label=boolean, bottom=bottom)
bottom += weight_count
# 调整图例的顺序和位置
handles, labels = plt.gca().get_legend_handles_labels()
plt.legend(handles[::-1], labels[::-1], loc="upper right")
plt.show()