【发布时间】:2020-09-20 01:01:40
【问题描述】:
所以我试图绘制这个数据框的条形图,团队由字符串组成,提及由整数组成。试图在 x 轴上绘制团队,在 y 轴上绘制提及。但是我遇到了 y 轴标签的问题,其中增量和标签是小数,我希望它们是整数/整数:
top_5_teams = ['England', 'France', 'Ireland', 'Italy', 'Wales']
top_5_team_mentions = [20, 11, 13, 6, 11]
df4 = pd.DataFrame({"Team":top_5_teams,
'Number of Times Mentioned':top_5_team_mentions})
df4 = df4.sort_values("Number of Times Mentioned", ascending=False)
df4_team = df4.iloc[:,0]
df4_mentions = df4.iloc[:,1]
plt.bar(arange(len(df4_team)),df4_mentions)
plt.xticks(arange(len(df4_team)),team,rotation=30)
plt.show()
这是我的图表: enter image description here
我想基于变量(例如 arange() 而不是单个数字(例如 20 或 25)来执行此操作,因此我的代码可以与任何数据帧一起使用。 那么如何更改它,使 y 轴上的间隔是整数/整数而不是小数/浮点数?
【问题讨论】:
-
您能否提供其他人运行您的代码所需的最少数据量。
-
添加了额外的数据
-
from matplotlib.ticker import MaxNLocator和plt.gca().xaxis.set_major_locator(MaxNLocator(integer=True))。见Matplotlib: How to force integer tick labels?。
标签: python numpy matplotlib plot graph