![]() ![]() I managed to work around those with Removing white space around a saved image plt.tight_layout(pad=1) ![]() 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. ![]() If you really need a specific width in addition to height, this seems to work OK: width.py #!/usr/bin/env python3 Plt.savefig(dpi=h/fig.get_size_inches() + width control this is an important use case that I don't have a great solution for yet.Īsked at: How to obtain a fixed height in pixels, fixed data x/y aspect ratio and automatically remove remove horizontal whitespace margin in Matplotlib? Which gives the exact desired height for height equals 431:įixed height, set_aspect, automatically sized width and small marginsĮrmmm, set_aspect messes things up again and prevents plt.tight_layout from actually removing the margins. Instead, this other approach proposed in the same question seems to work well: plt.tight_layout(pad=1) However, that works by cropping the image, and you won't get the desired sizes with it. Removing white space around a saved image I always feel that there is too much white space around images, and tended to add bbox_inches='tight' from: Plt.savefig(bbox_inches='tight' changes image size 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. I think this is what I'll go with most of the time, as it is simple and scales: get_size.py #!/usr/bin/env python3 My best approach so far: plt.savefig(dpi=h/fig.get_size_inches() height-only control Just to have a comparison point: base.py #!/usr/bin/env python3 I couldn't easily find an upstream discussion about this.īaseline example without trying to set the image dimensions Summary of current status: things are messy, and I am not sure if it is a fundamental limitation, or if the use case just didn't get enough attention from developers. Here is a quick comparison of some of the approaches I've tried with images showing what the give. savefig: how to save to a file, not just show on screen.This answer allows easily to combine all three images in one image file to see the difference in sizes.Ĭomparison of different approaches to set exact image sizes in pixels The module comments and the actual output differ. # this also results in a 1595x1132 image, but the fonts are larger. # Now make the image twice as big, making all the fonts and linesį.set_size_inches( DefaultSize )# resetthe sizeį.savefig("test3.png", dpi = (200)) # change the dpi # Now make the image twice as big, while keeping the fonts and all theį.set_size_inches( (DefaultSize*2, DefaultSize*2) ) # this gives me a 797 x 566 pixel image, which is about 100 DPI Print "Which should result in a %i x %i Image"%(DPI*DefaultSize, DPI*DefaultSize) Print "Default size in Inches", DefaultSize ![]() # Now check everything with the defaults: e("WXAgg") # do this before pylab so you don'tget the default back end. Print "using MPL version:", matplotlib._version_ This is a small demo file that helps teach how to adjust figure sizes It creates test.png files of different sizes of the same image: #!/usr/bin/env python Here's a test script from the above page. The first link in Google for 'matplotlib figure size' is AdjustingImageSize ( Google cache of the page). ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |