如何在python中将xaxis列为国家而不是数字

2024-10-03 23:24:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着在jupyter笔记本上创建一个条形图,x轴是country,y轴是impressions。不知道为什么这些国家被转换成每个国家的指数。也不知道为什么它会创建两个图表,其中一个是空的。有什么办法解决这个问题吗?在

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()

谢谢!在

screenshotenter image description here


Tags: plot笔记本jupyterplt国家countryloctotal
1条回答
网友
1楼 · 发布于 2024-10-03 23:24:53

您必须按如下方式更改代码才能获得条形图

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

相关问题 更多 >