使用 pyplot 取消裁剪图形图像

uncrop graph image with pyplot

首先,我使用的是 pycharm 专业版,它仅以图像形式显示 plt.show()。我正在制作一个条形图,其中每一列都有一个很长的名称,因此它们必须是垂直的。但是,在从显示的图表(附加)中裁剪之前,现在每个名称只显示几个字母。我试过 plt.set_margins()plt.autoscale(),都没有用。我该怎么做才能扩展该图的底部,以便至少可以看到每个列名称的某些单词?另一种选择是使每一列的宽度足以水平放置某些名称。我假设这是一个简单的解决方案,因为我对 pyplot 了解不多。

谢谢

你有3种可能

  1. 手动调整,如Rotating custom tick labels下的官方文档所示(我没有写下面的代码)

    import matplotlib.pyplot as plt
    
    x = [1, 2, 3, 4]
    y = [1, 4, 9, 6]
    labels = ['Frogs', 'Hogs', 'Bogs', 'Slogs']
    
    plt.plot(x, y)
    # You can specify a rotation for the tick labels in degrees or with keywords.
    plt.xticks(x, labels, rotation='vertical')
    # Pad margins so that markers don't get clipped by the axes
    plt.margins(0.2)
    # Tweak spacing to prevent clipping of tick-labels
    plt.subplots_adjust(bottom=0.15)
    plt.show()
    

  2. plt.show()

    之前使用plt.tight_layout()
  3. 实例化图形使用constrained_layout

    fig, ax = plt.subplots(..., constrained_layout=True)
    ...