如何将x轴列为国家/地区而不是python中的数字

时间:2018-06-27 17:25:44

标签: python matplotlib jupyter-notebook bar-chart

我试图在jupyter笔记本中创建条形图,其中x轴为国家/地区,y轴为印象。不知道为什么将这些国家转换为每个国家的索引号。也不确定为什么要创建2个图表,其中一个为空。关于如何解决此问题的任何想法吗?

impunder1000 = dataset2.loc[totimps < 1000, ['Total impressions']]
countryunder1000 =dataset2.loc[totimps < 1000, ['Country']]

imp1000 = dataset2.loc[(totimps > 1000) & (totimps < 10000), ['Total impressions']]
country1000 = dataset2.loc[(totimps > 1000) & (totimps < 10000), ['Country']]

imp10000 = dataset2.loc[(totimps > 10000) & (totimps < 100000), ['Total impressions']]
country10000 = dataset2.loc[(totimps > 10000) & (totimps < 100000), ['Country']]

imp100000 = dataset2.loc[(totimps > 100000) & (totimps < 1000000), ['Total impressions']]
country100000 = dataset2.loc[(totimps > 100000) & (totimps < 1000000), ['Country']]

imp1000000 = dataset2.loc[(totimps > 1000000) & (totimps < 10000000), ['Total impressions']]
country1000000 = dataset2.loc[(totimps > 1000000) & (totimps < 10000000), ['Country']]


x = countryunder1000
y = impunder1000

plt.xlabel('Country')
plt.ylabel('Impressions')



plt.title('Countries With Imps Under 1000')

ax = plt.axes()

plt.rcParams["figure.figsize"] = (30,5)
plt.figure
my_plot = x, y.plot(kind='bar')
plt.show()

谢谢!

screenshot enter image description here

1 个答案:

答案 0 :(得分:0)

您必须按照以下说明更改代码以获得条形图

fig, ax = plt.subplots()
ax.set_xlabel('Country')
ax.set_ylabel('Total Impressions')
ax.bar(x,y)
plt.show()

有关条形图绘制工具中的其他选项,您可以参考文档:https://matplotlib.org/api/_as_gen/matplotlib.pyplot.bar.html