【问题标题】:Pandas' figure is getting cut when saving [duplicate]保存时熊猫的身材被削减了[重复]
【发布时间】:2017-10-17 22:07:37
【问题描述】:

DataFrame data,其中date 是一个索引,如下所示:

date        count   
2017-03-20  119
2017-03-29  100
2017-04-03  118
2017-04-10  113

date(index) 的格式为datetime64[ns]。

当我尝试用

绘制它时

p = data.plot(kind="bar"),

它将小时:分钟:秒添加到轴值(到日期)。所以,当我用

保存它时
fig = p.get_figure()
fig.savefig('figure.jpeg'),

框架截断了日、月、年,只留下零、分、秒...

【问题讨论】:

    标签: python pandas plot


    【解决方案1】:

    你可以使用strftime + tight_layout:

    import matplotlib.ticker as ticker
    import matplotlib.pyplot as plt
    
    ticklabels = data.index.strftime('%Y-%m-%d')
    ax = data.plot(kind="bar")
    plt.tight_layout()
    ax.xaxis.set_major_formatter(ticker.FixedFormatter(ticklabels))
    

    【讨论】:

    • 请看编辑
    • 如果更改 data.plot(kind="bar",figsize=(12,8)) 没有帮助?
    • 这行得通,但你的输出中的绘图方块也是如此吗?直方图本身变得非常小。有没有办法在不损害组织大小的情况下增加蜱的空间?
    • 嗯,对我来说是个难题,但也许可以帮助this
    • 好的,谢谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 2012-01-18
    • 2019-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-03
    • 2021-08-09
    • 1970-01-01
    相关资源
    最近更新 更多