pad_inches=0 and bbox_inches=“tight” makes the plot smaller than declared figsize

前端 未结 3 1078
死守一世寂寞
死守一世寂寞 2020-12-08 07:10

I am producing a publication-quality plot to be embedded in latex and I would like to be very precise in terms of sizes and fonts (so that fonts are of the same size in the

相关标签:
3条回答
  • 2020-12-08 07:54

    Your original plot must have whitespace around it, otherwise bbox_inches=tight would not remove any of the area. There are two solutions to this that I know of:

    1. The simple method is to use tight_layout as mentioned by tcaswell.

    2. The more complicated, but more controllable method is to avoid using fig.add_subplot(111) and instead use fig.add_axes() which allows you to be much more strict in terms of how large your axes are when you define the axes instance. You can then tweak the size of your axes to take up as much of the figure area as required to maintain a 5" figure area. Once you have done this, I would recommend simply not using bbox_inches or set it to None (the default) to avoid unnessicary cropping. fig.add_axes() requires a rect parameter as its first argument which consists of [left_position, bottom_position, width, height] each of which range from 0 to 1.

    Edit: After going through the tight_layout tutorial again, I have realized that it covers pretty much everything. I hadn't realized that it was able to maintain the aspect ratio of the axes instance easily, even if the aspect ratio of the figure is different from that of the axes instance. I tend to try to be very explicit when I define my axes areas because I deal with satellite imagery and try to keep it as the native sensor resolution or some factor of that sensor resolution, which requires a little bit more control.

    0 讨论(0)
  • 2020-12-08 08:01

    I have the same problem making matplotlib plots for LaTeX. Since my plots did not allow to use tight_layout, giving this warning, I ended up using fig.subplots_adjust(bottom=bottom_pos, top=top_pos, left=left_pos, right=right_pos). See this tutorial. It requires manually finding the best values for bottom_pos etc. but in my case it seems to be the only possibility to get images without much whitespace not changing the figure size of the output.

    0 讨论(0)
  • 2020-12-08 08:13

    The problem you are having is that bbox_inches='tight' just removes all of the extra white space around your figure, it does not actually re-arrange anything in your figure, after it has been rendered.

    You might need to tweak the parameters you pass to tight_layout (tutorial) to get your desired effect.

    Hopefully this gets you pointed in the right direction.

    0 讨论(0)
提交回复
热议问题