You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
So it does seem that fonts are scaling correctly, we just get some trouble for very small widths with labels getting cut off, e.g. the 100 on the top left.
We understand from this however that for this approach to scale nicely, you need to make every DPI-dependant setting proportional to the size in inches.
In the previous example, we only made the "Hello" text proportional, and it did retain its height between 60 and 80 as we'd expect. But everything for which we didn't do that, looks tiny, including:
line width of axes
tick labels
point markers
SVG
I could not find how to set it for SVG images, my approaches only worked for PNG e.g.:
and if I open it in Chromium 86 the browser debug tools mouse image hover confirm that height as 460.79.
But of course, since SVG is a vector format, everything should in theory scale, so you can just convert to any fixed sized format without loss of resolution, e.g.:
https://stackoverflow.com/questions/13714454/specifying-and-saving-a-figure-with-exact-size-in-pixels/64632093#64632093 deleted November 2022 by Machavity https://stackoverflow.com/users/2370483/machavity Since undeleted.
Comparison of different approaches
Here is a quick comparison of some of the approaches I've tried with images showing what the give.
Baseline example without trying to set the image dimensions
Just to have a comparison point:
base.py
run:
outputs:
My best approach so far:
plt.savefig(dpi=h/fig.get_size_inches()[1]
height-only controlI think this is what I'll go with most of the time, as it is simple and scales:
get_size.py
run:
outputs:
and
outputs:
I tend to set just the height because I'm usually most concerned about how much vertical space the image is going to take up in the middle of my text.
plt.savefig(bbox_inches='tight'
changes image sizeI always feel that there is too much white space around images, and tended to add
bbox_inches='tight'
from:https://stackoverflow.com/questions/11837979/removing-white-space-around-a-saved-image-in-matplotlib
However, that works by cropping the image, and you won't get the desired sizes with it.
Instead, this other approach proposed in the same question seems to work well:
which gives the exact desired height for height equals 431:
Fixed height,
set_aspect
, automatically sized width and small marginsErmmm,
set_aspect
messes things up again and preventsplt.tight_layout
from actually removing the margins...Asked at: https://stackoverflow.com/questions/64642855/how-to-obtain-a-fixed-height-in-pixels-fixed-data-x-y-aspect-ratio-and-automati
plt.savefig(dpi=h/fig.get_size_inches()[1]
+ width controlIf you really need a specific width in addition to height, this seems to work OK:
width.py
run:
output:
and for a small width:
output:
So it does seem that fonts are scaling correctly, we just get some trouble for very small widths with labels getting cut off, e.g. the
100
on the top left.I managed to work around those with https://stackoverflow.com/questions/11837979/removing-white-space-around-a-saved-image-in-matplotlib/57498238#57498238
which gives:
From this, we also see that
tight_layout
removes a lot of the empty space at the top of the image, so I just generally always use it.Fixed magic base height,
dpi
onfig.set_size_inches
andplt.savefig(dpi=
scalingI believe that this is equivalent to the approach mentioned at: https://stackoverflow.com/a/13714720/895245
magic.py
run:
outputs:
And to see if it scales nicely:
outputs:
So we see that this approach also does work well. The only problem I have with it is that you have to set that
magic_height
parameter or equivalent.Fixed DPI +
set_size_inches
This approach gave a slightly wrong pixel size, and it makes it is hard to scale everything seamlessly.
set_size_inches.py
run:
outputs:
so the height is slightly off, and the image:
The pixel sizes are also correct if I make it 3 times larger:
outputs:
We understand from this however that for this approach to scale nicely, you need to make every DPI-dependant setting proportional to the size in inches.
In the previous example, we only made the "Hello" text proportional, and it did retain its height between 60 and 80 as we'd expect. But everything for which we didn't do that, looks tiny, including:
SVG
I could not find how to set it for SVG images, my approaches only worked for PNG e.g.:
get_size_svg.py
run:
and the generated output contains:
and identify says:
and if I open it in Chromium 86 the browser debug tools mouse image hover confirm that height as 460.79.
But of course, since SVG is a vector format, everything should in theory scale, so you can just convert to any fixed sized format without loss of resolution, e.g.:
gives the exact height:
TODO regenerate image, messed up the upload somehow.
I use Inkscape instead of Imagemagick's
convert
here because you need to mess with-density
as well to get sharp SVG resizes with ImageMagick:And setting
<img height=""
on the HTML should also just work for the browser.Tested on matplotlib==3.2.2.
The text was updated successfully, but these errors were encountered: