下面是一个使用Matplotlib中的^{}为每个箱子提供多个条的直方图示例:import numpy as np
import matplotlib.pyplot as plt
length_of_flowers = np.random.randn(100, 3)
Lbins = [0.1 , 0.34, 0.58, 0.82, 1.06, 1.3 , 1.54, 1.78, 2.02, 2.26, 2.5 ]
# Lbins could also simply the number of wanted bins
colors = ['red','yellow', 'blue']
labels = ['red flowers', 'yellow flowers', 'blue flowers']
plt.hist(length_of_flowers, Lbins,
histtype='bar',
stacked=False,
fill=True,
label=labels,
alpha=0.8, # opacity of the bars
color=colors,
edgecolor = "k")
# plt.xticks(Lbins) # to set the ticks according to the bins
plt.xlabel('flower length'); plt.ylabel('count');
plt.legend();
plt.show()
它给出了:
编辑:来自this matplotlib demo的预装箱数据的解决方案。每个条的位置都是自定义计算的。我稍微修改了数据,通过替换零值来验证对齐是否正确。在
^{pr2}$
它给出了: