我在将多个列绘制成直方图时遇到问题
x1 = list(df[df['newcol'] == 0]['Country/Region'])
x2 = list(df[df['newcol'] == 1]['Country/Region'])
colors = ['r', 'c']
names = ['warm','cool']
plt.hist([x1, x2], bins = 1, normed=True,
color = colors, label=names)Country/Region Total newcol
USA 450 0
Andorra 225 1
Bahamas 300 1
Uk 150 0
Nigeria 189 0我想让x轴上的国家/地区总数在y轴上,然后根据新的col值对条形图进行着色,例如,由于与0相关联,美国将被着色为绿色,而由于与1相关联,巴哈马将被着色为蓝色。我上面使用的代码为我提供了颜色,但由于在国家/地区列中有如此多的国家/地区,因此图表被压缩,而且y轴没有为我提供正确的数字
发布于 2020-04-18 04:01:22
我认为你可能需要柱状图而不是直方图
x1 = list(df[df['newcol'] == 0]['Country/Region'])
x2 = list(df[df['newcol'] == 1]['Country/Region'])
y1 = list(df[df['newcol'] == 0]['Total'])
y2 = list(df[df['newcol'] == 1]['Total'])
plt.bar(x1, y1, color='g')
plt.bar(x2, y2, color='b')

这是你需要的吗?
https://stackoverflow.com/questions/61278924
复制相似问题