【问题标题】:uncrop graph image with pyplot使用 pyplot 取消裁剪图形图像
【发布时间】:2021-10-16 02:39:01
【问题描述】:

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

谢谢

【问题讨论】:

  • 也许是plt.tight_layout()
  • 您也可以考虑将标签旋转 45 度。

标签: python-3.x matplotlib graph


【解决方案1】:

你有 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)
    ...
    

【讨论】:

  • 谢谢!手动调整效果很好,我可以分辨出哪个列对应哪个响应。
  • @NathanWolf 不客气!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多