直方图条不能在python中使用matplotlib堆叠

莫森·阿里

我已经在python中使用“ matplotlib”实现了直方图。我有两个变量x1和x2,每个变量有15个元素。运行代码时,直方图条不会堆叠,而是重叠,如下图所示。

我想绘制变量条形的堆积直方图。

的是代码:

x1= [23, 25, 40, 35, 40, 53, 33, 28, 55, 34, 20, 37, 36, 23, 33]
x2= [36, 20, 27, 50, 34, 47, 18, 28, 52, 21, 44, 34, 13, 40, 49]
colors = ['blue', 'orange']
bins = [10,20,30,40,50,60]
fig, (ax0, ax1, ax2) = plt.subplots(nrows=3)

ax0.hist(x1,bins = bins,  histtype='bar',  label=colors[0], rwidth=0.8)
ax0.hist(x2,bins, histtype='bar', stacked=True, label=colors[1], rwidth=0.8)

ax1.hist(x1, bins = bins, histtype='bar',  label=colors[0], rwidth=0.8)
ax1.hist(x2,bins = bins, histtype='bar', stacked=True,  label=colors[1], rwidth=0.8)

ax2.hist(x1, bins = bins, histtype='bar',  label=colors[0], rwidth=0.8)
ax2.hist(x2,bins = bins, histtype='bar', stacked=True,  label=colors[1], rwidth=0.8)

plt.show()

输出量 在此处输入图片说明

谢尔多雷

尝试将两个列表一起传递并使用stacked=True仅传递一个列表并使用stacked=True没有多大意义。

ax0.hist([x1, x2], bins, histtype='bar', stacked=True, label=colors, rwidth=0.8)
ax1.hist([x1, x2], bins, histtype='bar', stacked=True, label=colors, rwidth=0.8)
ax2.hist([x1, x2], bins, histtype='bar', stacked=True, label=colors, rwidth=0.8)

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用matplotlib在python中绘制堆叠的直方图

来自分类Dev

Gnuplot:堆叠直方图条的颜色

来自分类Dev

通过Python的Matplotlib彼此堆叠3条

来自分类Dev

Python:如何使用Plotly堆叠或叠加直方图

来自分类Dev

为什么我不能在Python中使用“ +”合并字典?

来自分类Dev

python不能在处理程序中使用异常字段

来自分类Dev

不能在 Python 中使用列表作为函数参数

来自分类Dev

Python堆叠直方图

来自分类Dev

Python并绘制直方图(使用matplotlib)

来自分类Dev

使用matplotlib创建直方图-Python

来自分类Dev

如何使用seaborn绘制将直方图条围绕刻度线居中?堆叠条是必不可少的

来自分类Dev

为什么我不能在Spyder中使用matplotlib.pyplot?

来自分类Dev

python exec命令可在控制台中使用,但不能在程序中使用

来自分类Dev

不能在awk命令中使用“(”

来自分类Dev

不能在或if语句中使用

来自分类Dev

不能在Linqpad中使用“包含”

来自分类Dev

不能在jQuery方法中使用alert()

来自分类Dev

不能在猫鼬中使用诺言

来自分类Dev

不能在AppController cakephp中使用__()

来自分类Dev

不能在findstr中使用变量?

来自分类Dev

不能在Android Studio中使用BaseGameActivity

来自分类Dev

不能在Column中使用DropShadow?

来自分类Dev

不能在黑斑羚中使用

来自分类Dev

不能在gensub(awk)中使用sprint

来自分类Dev

不能在组件中使用道具

来自分类Dev

不能在Makefile中使用变量

来自分类Dev

不能在findstr中使用变量?

来自分类Dev

不能在html中使用php(混合)

来自分类Dev

不能在颜色盒中使用jqZoom