我正在尝试绘制一个直方图,显示按性别划分的热翅消费频率计数。它是 1 个图中的两个直方图。
id Hotwings Beer Gender 1 4 24 F 2 5 0 F 3 5 12 F 4 6 12 F 5 7 12 F 6 7 12 F 7 7 24 M 8 8 24 F 9 8 0 M 10 8 12 M 11 9 24 F 12 11 24 F 13 11 24 M 14 12 30 F 15 12 30 F 16 13 24 F 17 13 36 F 18 13 30 M 19 13 30 M 20 14 30 F 21 14 36 F 22 14 48 M 23 16 36 M 24 16 36 M 25 17 36 M 26 17 42 M 27 18 30 M 28 18 30 M 29 21 36 M 30 21 42 M
这是我上面的数据集...
plt.hist([data["Hotwings"][data["Gender"]=='M']],[data["Hotwings"][data["Gender"]=='F']] )
我一直收到 bins must be 1d, 当一个数组时,错误。我对如何继续或是否有更好的方法使用 matplotlib 来做到这一点感到非常困惑。谢谢!
编辑: 我也试过这个:
plt.hist(dta[dta["Gender"]=='M'][data["Hotwings"]],[data["Gender"]=='F'][data["Hotwings"] ])
我得到一个 keyError: "None of [Int64Index([ 4, 5, 5, 6, 7, 7, 7, 8, 8, 8, 9, 11, 11, 12, 12, 13, 13,\n 13, 13, 14, 14, 14, 16, 16, 17, 17, 18, 18, 21, 21],\n dtype='int64')] 在[列]中”
我真的很困惑,不确定如何并排制作条形图
请您参考如下方法:
这将在同一图表中显示您的条形图
plt.bar(dta[dta['Gender']=='F']['Hotwings']-1, dta[dta['Gender']=='F']['Beer'], 0.50, align='center', alpha=0.5, color='b')
plt.bar(dta[dta['Gender']=='M']['Hotwings']+1, dta[dta['Gender']=='M']['Beer'], 0.50, align='center', alpha=0.5, color='r')
plt.show()
并在您可以使用的不同图表中显示图表。
sp = plt.subplot(2,2,1)
l1 = plt.bar(dta[dta['Gender']=='F']['Beer'], dta[dta['Gender']=='F']['Hotwings'], 3.0, align='center', alpha=0.5, color='b')
sp = plt.subplot(2,2,2)
l1 = plt.bar(dta[dta['Gender']=='M']['Beer'], dta[dta['Gender']=='M']['Hotwings'], 2.0, align='center', alpha=0.5, color='r')
plt.show()
