This question already has answers here:
Matplotlib: Specify format of floats for tick lables
                                
                                    (4个答案)
                                
                        
                                去年关闭。
            
                    
我的年份数据在表格上显示时是一个整数,但是当我将其输入到x轴下的折线图中时,它会在一年中的不同时间分裂。有人可以帮我把年份归类吗?

这是我当前的代码:

 df.plot( x = "Year", kind="line", figsize = (10,5), legend = True)
 plt.ticklabel_format(style="plain", axis="y")
 plt.xlabel("Year")
 plt.ylabel("Profit")
 plt.title("Yearly Profit")
 plt.show()


python - 从折线图中删除小数-LMLPHP

最佳答案

我认为您需要指定列Yeardatetime对象:

df['Year'] = pd.to_datetime(df['Year'], format='%Y')


输出:
python - 从折线图中删除小数-LMLPHP

10-02 08:17