【问题标题】:text on xlabel is cutted off in matplotlib [duplicate]xlabel 上的文本在 matplotlib 中被截断 [重复]
【发布时间】:2022-01-11 13:50:12
【问题描述】:

我想绘制这些数据:

datetime
2021-12-06 00:00:00     40354
2021-12-06 00:05:00     94557
2021-12-06 00:10:00     53314
2021-12-06 00:15:00     91334
2021-12-06 00:20:00     94168
2021-12-06 00:25:00     92049
2021-12-06 00:30:00     89400
2021-12-06 00:35:00     86499
2021-12-06 00:40:00     87517

我用

plt.plot(data)
plt.xticks(rotation=90)
plt.axis('tight')
plt.savefig('plot.png')
plt.close()

但是x标签被切断了:

我解决了这个问题X-axis Label Gets Cut Off Of Graph - Python Matplotlib 并尝试使用plt.tight_layout()plt.savefig('plot.png', bbox_inches='tight') 但它没有帮助。你能帮帮我吗?

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    你可以试试这个

    import numpy as np
    from datetime import datetime
    from matplotlib import pyplot as plt 
    
    date_t = [str(datetime(2021,12,6,0,item,0,0)) for item in range(0,50,5)]
    y_vals = np.random.randint(100,200,len(date_t))
    
    fig,ax = plt.subplots(figsize=(8,6))
    ax.plot(y_vals)
    ax.set_xticks(range(len(y_vals)))
    ax.set_xticklabels(date_t)
    ax.tick_params(axis='x',labelrotation=90)
    

    输出是这样的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-05
      • 2021-12-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多