【问题标题】:Removing decimal from line chart [duplicate]从折线图中删除小数[重复]
【发布时间】:2019-03-09 20:22:25
【问题描述】:

我的年份数据在表格上显示时是一个整数,但是当我将其输入到 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()

【问题讨论】:

  • 创建一个轴实例 ax,绘制你的 df,然后使用ax.xaxis.set_major_formatter(FormatStrFormatter('%d'))

标签: python python-2.7 matplotlib


【解决方案1】:

我认为您需要指定列Year 是datetime 对象:

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

输出:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    相关资源
    最近更新 更多