python绘制直方图根据不同分类_如何在python中绘制具有多个类别的直方图

下面是一个使用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()

它给出了:

H8eeV.png

编辑:来自this matplotlib demo的预装箱数据的解决方案。每个条的位置都是自定义计算的。我稍微修改了数据,通过替换零值来验证对齐是否正确。在

^{pr2}$

它给出了:

S8Igh.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值