以预定义的宽度/高度比将matplotlib图保存在右侧的图例中 [英] Save matplotlib plot with legend on the right in predefined width/height ratio

查看:56
本文介绍了以预定义的宽度/高度比将matplotlib图保存在右侧的图例中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个 matplotlib 图保存为一个宽/高比为 1.25 的 png 文件.我通过figsize参数指定了该比率.但是,当我使用选项 bbox_inches ="tight" 保存图形时,输出png的大小为553至396像素,比率为1.39.我想保留 bbox_inches = "tight" 选项以防止图形边框中出现不必要的空白.我尝试了类似 stackoverflow 帖子中建议的不同方法,但找不到解决方案.

这是示例代码:

 将matplotlib.pyplot导入为plt将numpy导入为npfig = plt.figure(figsize = (3, 2.4), dpi = 150)ax = plt.subplot(111)对于范围内的 i (3):ax.plot(np.random.random(10),np.random.random(10),"o",label = i)ax.legend(bbox_to_anchor =(1,0.6),title ="Title")plt.ylabel("标签")plt.xlabel("标签")plt.title("标题", loc = "left")plt.savefig("test.png", format = "png", dpi = 150, bbox_inches = "tight")

这是输出的png

解决方案

bbox_inches = "tight" 明确地告诉 matplotlib 裁剪或扩展图形;因此,图形大小的任何设置都将丢失.因此,如果要控制图形尺寸,则不能使用此选项.

您还有其他选择:

定义BBox

  • 定义您自己的 bbox_inches,它确实具有所需的方面.
    该图像现在为547 x 438像素,因此纵横比为1.2488,与您可以达到的1.25尽可能接近.

调整内边距

  • 使用原始图形尺寸(3,2.4)并调整填充,以使所有元素都适合图形.这将使用 fig.subplots_adjust() 完成.

    fig = plt.figure(figsize = (3, 2.4), dpi = 150)fig.subplots_adjust(top=0.89,底部=0.195,左= 0.21,右 = 0.76)


    此图像现在具有 (3, 2.4)*150 = 450 x 360 像素的预期大小.

  • 对于子图参数的自动确定,也看看这个问题:这个答案到如何将图例从情节中排除".

    I want to save a matplotlib figure as a png file with a width/height ratio of 1.25. I specified this ratio via the figsize argument. But when I save the figure using the option bbox_inches = "tight" then the output png has a size of 553 to 396 pixels which is a ratio of 1.39. I would like to keep the bbox_inches = "tight" option to prevent unnecessary white space in the figure borders. I tried different approaches suggested in similar posts on stackoverflow but couldn't figure out a solution.

    Here is example code:

    import matplotlib.pyplot as plt
    import numpy as np
    
    fig = plt.figure(figsize = (3, 2.4), dpi = 150)
    ax = plt.subplot(111)
    
    for i in range(3):
        ax.plot(np.random.random(10), np.random.random(10), "o", label = i)
    ax.legend(bbox_to_anchor=(1, 0.6), title = "Title")
    plt.ylabel("Label")
    plt.xlabel("Label")
    plt.title("Title", loc = "left")
    plt.savefig("test.png", format = "png", dpi = 150, bbox_inches = "tight")
    

    This is the output png

    解决方案

    The bbox_inches = "tight" explicitely tells matplotlib to crop or expand the figure; any settings for the figure size will hence be lost. Thus you cannot use this option if you want to have control over the figure size.

    Other options you have:

    Define BBox

    • Define your own bbox_inches, which does have the desired aspect. The dimensions of the Bbox would be [[x0,y0],[x1,y1]].

      import matplotlib.transforms
      bbox = matplotlib.transforms.Bbox([[-0.2, -0.36], [3.45, 2.56]])
      plt.savefig("test.png", format = "png", dpi = 150,bbox_inches =bbox)
      


      This image is now 547 x 438 pixels, thus having an aspect of 1.2488, which is as close as you can get to 1.25.

    Adjust padding

    • Use the original figure size of (3, 2.4) and adjust the padding, such all elements fit into the figure. This would be done using fig.subplots_adjust().

      fig = plt.figure(figsize = (3, 2.4), dpi = 150)
      fig.subplots_adjust(top=0.89,
                          bottom=0.195,
                          left=0.21,
                          right=0.76)
      


      This image now has the expected size of (3, 2.4)*150 = 450 x 360 pixels.

    • For an automatic determination of the subplot parameters, also look at this question: Creating figure with exact size and no padding (and legend outside the axes)

    In general, I would recommend reading this answer to "How to put the legend out of the plot`.

    这篇关于以预定义的宽度/高度比将matplotlib图保存在右侧的图例中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆