【问题标题】:Remove padding from matplotlib plotting从 matplotlib 绘图中删除填充
【发布时间】:2012-07-23 04:30:46
【问题描述】:

我在 matplotlib 中绘制图像,它不断给我一些填充。这是我尝试过的:

def field_plot():
    x = [i[0] for i in path]
    y = [i[1] for i in path]
    plt.clf()
    plt.axis([0, 560, 0, 820])
    im = plt.imread('field.jpg')
    field = plt.imshow(im)
    for i in range(len(r)):
        plt.plot(r[i][0],r[i][1],c=(rgb_number(speeds[i]),0,1-rgb_number(speeds[i])),linewidth=1)
    plt.axis('off')
    plt.savefig( IMG_DIR + 'match.png',bbox_inches='tight', transparent="True")
    plt.clf()

【问题讨论】:

  • 图片中您所指的内边距在哪里?此外,您的示例不是很有用,因为没有您的图像文件我们无法运行它。
  • @BrenBarn 图片左侧和下方是最大的填充。上面和右边我有更小的填充物。就像它为标签留出空间一样
  • 你能提供一个不依赖外部文件的可运行示例吗?另请参阅之前的问题 herehere。如果这些不起作用,请说明他们没有做什么,而您需要他们做。
  • @BrenBarn 我对此很陌生,所以我不知道如何为此提供一个更好的例子。我很抱歉,没有图像我不知道怎么想.. :(
  • @Tshepang 我已经附上了保存的图像。正如您在下载它时所看到的那样,它有白色的大填充物。 r 是一个列表,其中每个点都有 [x,x+1],[y,y+1] (每个图的坐标)

标签: python django matplotlib padding


【解决方案1】:

尝试使用pad_inches=0,即

plt.savefig( IMG_DIR + 'match.png',bbox_inches='tight', transparent="True", pad_inches=0)

来自documentation

pad_inches:当 bbox_inches 为时,图形周围的填充量 “紧”。

我认为默认是pad_inches=0.1

【讨论】:

  • True 周围不需要引号;透明=真。 transparent="False" 的原因也转化为 transparent=True。
【解决方案2】:

这对我有用。绘图后,使用 ax = plt.gca() 从 plt 获取 Axes 对象。然后设置 ax 对象的 xlim 和 ylim 以匹配图像宽度和图像高度。绘图时,Matplotlib 似乎会自动增加可视区域的 xlim 和 ylim。请注意,在设置 y_lim 时,您必须颠倒坐标的顺序。

for i in range(len(r)):
  plt.plot(r[i][0],r[i][1],c=(rgb_number(speeds[i]),0,1-rgb_number(speeds[i])),linewidth=1)

plt.axis('off')
ax = plt.gca();
ax.set_xlim(0.0, width_of_im);
ax.set_ylim(height_of_im, 0.0);
plt.savefig( IMG_DIR + 'match.png',bbox_inches='tight', transparent="True")

【讨论】:

    【解决方案3】:

    以前的所有方法都不太适合我,它们都在图形周围留下了一些填充。

    以下行成功删除了留下的白色或透明填充:

    plt.axis('off')
    ax = plt.gca()
    ax.xaxis.set_major_locator(matplotlib.ticker.NullLocator())
    ax.yaxis.set_major_locator(matplotlib.ticker.NullLocator())
    plt.savefig(IMG_DIR + 'match.png', pad_inches=0, bbox_inches='tight', transparent=True)
    

    【讨论】:

      【解决方案4】:

      只需在plt.savefig() 之前添加plt.tight_layout() !!

      plt.figure(figsize=(16, 10))
      
      # ... Doing Something ...
      
      plt.tight_layout()
      plt.savefig('wethers.png')
      plt.show()
      

      【讨论】:

        【解决方案5】:

        使用plt.gca().set_position((0, 0, 1, 1)) 让轴跨越整个图形,请参阅reference。 如果使用plt.imshow,则要求图形具有正确的纵横比。

        import matplotlib as mpl
        import matplotlib.pyplot as plt
        
        # set the correct aspect ratio
        dpi = mpl.rcParams["figure.dpi"]
        plt.figure(figsize=(560/dpi, 820/dpi))
        
        plt.axis('off')
        plt.gca().set_position((0, 0, 1, 1))
        
        im = plt.imread('field.jpg')
        plt.imshow(im)
        
        plt.savefig("test.png")
        plt.close()
        

        【讨论】:

          猜你喜欢
          • 2017-02-28
          • 2017-04-05
          • 2016-05-15
          • 1970-01-01
          • 2019-04-12
          • 2023-01-08
          • 2017-11-23
          • 2015-12-29
          • 2017-03-09
          相关资源
          最近更新 更多