gpt4 book ai didi

pandas - 绘制 Pandas DataFrame时缺少xticklabels的第一个值

转载 作者:行者123 更新时间:2023-12-04 14:38:30 27 4
gpt4 key购买 nike

我是Pandas和matplotlib的新手,想绘制此DataFrame

      season   won               team  matches    pct_won
0 2008/2009 28.0 Manchester United 38 73.684211
1 2009/2010 27.0 Manchester United 38 71.052632
2 2010/2011 23.0 Manchester United 38 60.526316
3 2011/2012 28.0 Manchester United 38 73.684211
4 2012/2013 28.0 Manchester United 38 73.684211
5 2013/2014 19.0 Manchester United 38 50.000000
6 2014/2015 20.0 Manchester United 38 52.631579
7 2015/2016 19.0 Manchester United 38 50.000000

使用以下代码行:
ax = aggregated_frame.plot(x='season', y='pct_won', figsize=(8,8), marker='o', rot=90, title='Performance by season of Team: {}'.format(aggregated_frame.iloc[0]['team']))
ax.set_xticklabels(aggregated_frame.season)
ax.set_xlabel('')
ax.yaxis.grid()
ax.yaxis.set_major_formatter(mtick.PercentFormatter())
ax.legend(['Percentage of Matches Won'], fontsize=14)
for item in ([ax.title, ax.xaxis.label] + ax.get_xticklabels() + ax.get_yticklabels()):
item.set_fontsize(14);

但是我的 xticklabels的第一个值在结果图中被删除:

我尝试了几个选项,甚至只设置了 ax.set_xticklabels([1,2,3,4,5,6,7,8]),但每次都删除第一个值。

这是怎么了任何帮助都超过了欢迎。

另外:为什么首先不将 season的值用作标签?如果我省略 set_xticklabels,则在x轴上根本不会打印任何标签。

最佳答案

您也不要在未设置刻度位置的情况下设置标签。 IE。如果使用ax.set_xticklabels,则始终也需要使用ax.set_ticks(否则,可能会有异常(exception),在这种情况下,您需要确切地知道自己在做什么)。

这里:

ax.set_xticks(range(len(df)))
ax.set_xticklabels(df.season)

产生

enter image description here

关于pandas - 绘制 Pandas DataFrame时缺少xticklabels的第一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51200289/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com