Send
Close Add comments:
(status displays here)
Got it! This site "robinsnyder.com" uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website. Note: This appears on each machine/browser from which this site is accessed.
Matplotlib: DPI tricks and plot class and rsPlot module
1. Matplotlib: DPI tricks and plot class and rsPlot module
Matplotlib was designed in terms of
DPI (Dots Per Inch) which is somewhat consistent but not always exact.
If one wants an image with an exact width and height (in pixels), tricks can be used in matplotlib to (usually) approximate the desired width and height.
2. Compromise
Whenever faced with getting exact placement of graphics and image sizes, I ended up creating whatever graphics functionality I needed as a class and then use the canvas from the
PIL (Python Imaging Library) rather than use matplotlib - other than for charts and graphs.
3. DPI tricks
To get close to the desired width and height of an image, one can play "tricks" with the DPI parameter of matplotlib.
The following code shows this by example. This also shows how to save multiple images of the same figure and the method can be used to save multiple formats of the same figure.
4. rsPlot class
The first example program uses explicit local/global variables and calls.
The second program abstracts the first example program into a class called plot.
To third program abstracts the essential code into the module rsPlot containing class plot.
The module rsPlot and class plot will be used in most matplotlib examples.
5. Explicit code example
Here is the Python code [#1]
6. Text output
Here is the output of the Python code.
7. Default image layout
The first image does have the desired width and height but has extra white space around the edge.
Note: The results can vary a little if the DPI is not an even multiple (i.e., other then base 2) of the desired width and height.
8. Tight image layout
The
tight_layout function adds extra space between subplots (not relevant here since there is one row and one column).
The
bbox_inches parameter removes extra space at the outside of the image.
As a result, the second image does not have the desired width and height but does not have the extra white space either.
9. Abstracted class code example
Here is the Python code [#2]
Note that this code has some default font settings - not in the original code. So the border effect is similar but the actual text on the images is different.
The code shows the way in which (global) matplotlib settings can be changed. There are ways to change local settings.
10. Text output
Here is the output of the Python code.
11. Default image layout
The same image results (desired width and height, but extra white space around the edges, border shown for comparison).
12. Tight image layout
The same image results (reduced width and height, but removed white space around the edges, border show for comparison).
13. rsPlot module
Here is a code module in Python [module rsPlot in rsPlot.py].
Note that the module code has the default font settings - not in the original code.
14. Abstracted module example
Here is the Python code [#4]
Note that the same images result.
15. Text output
Here is the output of the Python code.
16. Default image layout
The same image results (desired width and height, but extra white space around the edges, border shown for comparison).
17. Tight image layout
The same image results (reduced width and height, but removed white space around the edges, border show for comparison).
18. End of page