Matplotlib: Make subplot fill axes tick/labels space - python

I'm creating a multi-panel figure, and I have an image I show using matplotlib.image.imshow in one of the panels. I want to maximize space utilization, so this image can be seen in higher detail.
I cannot find documentation to expand this image into the whitespace occupied by axes ticks and labels in other subplots. Have tried the various layout options (TightLayout, GridSpec or ConstrainedLayout).
Here is an example image from the matplotlib documentation. The desired version of this figure would be with the MRI image expanded to touch the bottom horizontal of the "Intensity (a.u.)" label. The aspect ratio of the MRI image should be maintained.
Any wisdom?

Related

How to increase matplotlib figure dpi without increasing the window size of the shown plot?

I want to increase the dpi of plots in matplotlib, but the window that displays the plot gets far too large when deviating from the default of
100. I've been using
import matplotlib
matplotlib.rcParams['figure.dpi'] = 300
matplotlib.rcParams['figure.figsize'] = (6.4, 4.8)
to increase the dpi of all plots shown and forcing it to have the default size but it still has the size issue. I would like it so that all plots displayed are uniform in size and dpi without having to individually set this for every figure. Any way to do this?
I think that this won't work as you wish for. The resolution (given in dpi) determines how many points an inch has. The size defines how many inches the figure should have. But none of both sets the number of pixels that your monitor should display for an inch. The thing is that matplotlib and python do not resize plots (only images). So if you save the plot as an image and open it again (with any image viewer) and you click on "show me 100% size", the figure will behave as you intended it to. But while drawing the pixels in a plot (that is what matplotlib does if you call matplotlib.pyplot.draw()), it needs to draw every pixel, which is why one might think that figuresize and dpi both result in a larger plot in matplotlib. Essentially figuresize tells the image viewer how to resize the image when displaying it.
I found this post is particularly useful for explaining the different behavior of size and resolution.

Prevent Matplotlib to stretch plot to only bbox of drawing area

Please see below image:
I set the figure size of plt equal to something like (4,6) and set axis to off and margin to zero.
Then continue to draw polyline using coordinate array by ax.plot(line[:,1],line[:,0])
after this I don’t use the plt.show()
But convert the plot to numpy array which has correct (4,6) size but surprisingly fill the plot by stretched to only bbox of the draw line
How can i see all the unused space of figure?
Is there any flag that i have to change in somewhere in matplotlib?
Any help appreciated
The plot of matplotlib will define the output based on shape and size of drawing not based on the pre-defined figure size,some kind of back-end and front-end, or simply it is a responsive-layout when you resize the window everything will scale else those are passed through linewidth= and ...
then i changed my workflow and problem solved ;)

How to increase the physical size of axes in matplotlib pyplot?

I am plotting the following graph,
I would like to change the physical size of the axes. I have plotted the xticks in hopes of being able to increase the spacing between them and thus increase the physical size of the axes, but no luck. I want a resulting image, that is possibly very large, but when zoomed in, I am able to see individual points and the arrows plotted. I tried changing the size of the figure, but it just creates a larger image, with higher resolution, and same level of detail as before, it does not allow me to see more points when I zoom in.
Can someone please tell me how can I increase the physical size of the axes so when I zoom in on the saved image (right now I'm saving it in a pdf format) I can see individual points? Thanks so much
You could try plt.figure(figsize=(20, 10)) in order to increase the width of the figure. It should be possible to obtain a suitable ratio of height and width by playing with these numbers (20=width, 10=height). There is a similar question here: How do you change the size of figures drawn with matplotlib?

Vector axes but raster points for Matplotlib scatter plots

I have a Matplotlib scatter plot with 10,000+ points that I plan to insert as a figure in a LaTeX document for publication.
I would like the plot points to be raster graphics (e.g. PNG) because vector graphics with that many points often causes problems for PDF readers. I would like the ticks and axes labels to be vector graphics so I don't have to worry about resolution issues for the text and lines.
Is there a simple way to get matplotlib to make parts of the plot raster graphics while keeping the axes/ticks vector graphics?
My best guess so far is to do some sort of pre-render to PNG then imshow the resulting image with appropriate axes bounds before saving to PDF.
Add rasterized=True to the call to plt.scatter
See the docs here
You can control the dpi of the rasterized parts of the figure by setting dpi=300 (for example) in the call to plt.figure

Matplotlib: how can I adjust the figure size to be tight around subplots with **fixed** aspect ratios?

I often make series of subplots with mpl_toolkits.basemap.Basemap projections, which generates geographic projections on axes with fixed aspect ratios. When I use fig.tight_layout(), this adjusts only the axes to be tight inside the bounding figure window -- so if the axes aren't allowed to have changed aspect ratios, this will often leave huge amounts of whitespace -- note this cannot be fixed by using bbox_inches='tight' in figsave(), because e.g. 2x2 networks of fixed-AR subplots will have extra interior whitespace.
Here's an example, where each axis has aspect 1:
Is there a way I can adjust the figure to be tight around the inner axes objects? Note this method would necessarily eliminate extra interior whitespace -- fixed-aspect ratio axes are centered in their GridSpec zone (e.g. the lower-right quadrant) until the total figure height/width is such that the aspect ratio of their GridSpec zone matches the axes aspect ratio.
Ideally, this method should work for the "outer bounding box" of each axes including its labels, titles, etc, rather than the box itself. One way would be to retrieve the aspect ratios of each subplot and adjust figsize accordingly, but that leaves the problem of room for titles, ticklabels, etc.; we really want the outer-bounding box aspect ratios.

Categories