Reduce the size of pyplot subplot by scaling - matplotlib

I am using Julia v.0.6.0, with Juno+Atom IDE, and I am trying to make subplots with PyPlot package, v2.3.2. (Pretty new to this)
Consider the following MWE:
using PyPlot
fig = figure("Test subplots",figsize=(9,9))
subplot(2,2,1)
title("Plot 221")
fig[:add_subplot](2,2,2,polar="true")
title("Plot 222")
fig[:canvas][:draw]() # Update the figure
suptitle("2x2 Subplot",fontsize=15)
tight_layout(pad=2)
which yields me this:
Note how the second subplot is too big such that its title is too close to the polar plot.
What I want to achieve is to have the subplot 222 to still take up the same amount of space in the grid, but to have the polar plot scaled down in size, perhaps to 0.9 of its current size.
Note that this should not affect the size of rectangular grid in subplot 221 as well.
Is there an argument that I am missing from the matplotlib documentation?

The main thing here is to capture any subplot axes, title objects etc into 'handles', so that you can manipulate their properties individually in an easy manner. So change your initial code like so:
using PyPlot
fig = figure("Test subplots",figsize=(9,9))
subplot(2,2,1)
title("Plot 221")
S = subplot(2,2,2,polar="true") ## captured
T = title("Plot 222") ## captured
fig[:canvas][:draw]() # Update the figure
ST = suptitle("2x2 Subplot",fontsize=15) ## captured
tight_layout(pad=2)
Now you can use properties such as T[:get_verticalalignment] to inspect, and T[:set_verticalalignment] to set it to one of "center", "bottom", "top" or "baseline" (as per the matplotlib documentation). E.g.
T[:set_verticalalignment]("bottom")
ST[:set_verticalalignment]("center")
seems to get the amount of separation you'd probably expect.
Alternatively, for even finer control, you can inspect or change the absolute positioning (and implicitly the size) for S, T, or ST via their [:get_position] and [:set_position] methods respectively.
These methods accept either via a normal array denoting [start_x, start_y, width_x, width_y], or a Bbox, which is the form returned by the get methods above, therefore you might want to get that object from matplotlib:
Bbox = PyPlot.matplotlib[:transforms][:Bbox]
Now you can do stuff like this:
# inspect the object's position
S[:get_position]()
#> PyObject Bbox([[0.544201388889, 0.517261679293],
#> [0.943952721661, 0.917013012065]])
# absolute positioning using 'width' notation
S[:set_position]([0.51, 0.51, 0.4, 0.4])
# absolute positioning using a 'Bbox' (note 2D array input, not a vector!)
S[:set_position](Bbox([0.51 0.51; 0.89 0.89]))
# 'relative' by adjusting current position, and wrapping back in a Bbox
S[:set_position](Bbox( S[:get_position]()[:get_points]() + [0.1 0.1; -0.1 -0.1]))
# inspect title position
T[:get_position]()
#> (0.5, 1.05)
# raise title a bit higher (manually)
T[:set_position]([0.5, 1.10])
etc.

Related

Using a subfigure as textbody fails on tight layout with a lot of text

Using a subfigure as textbody fails on tight layout with a lot of text. As seen in the provided example, the bound of a figure are overreached by one subfigure, as if the text was not wrapped.
import pandas as pd
from matplotlib import pyplot as plt
# Paramters for A4 Paper
fullheight = 11.69
fullwidth = 8.27
# how the subfigures dive the space
fig0_factor = 0.7
fig1_factor = 0.3
# generate the figure
fig = plt.figure(constrained_layout = True, figsize=(fullwidth, fullheight)) #
# generate 2 subfigures
subfigs = fig.subfigures(2,height_ratios=[fig0_factor,fig1_factor])
# fill the fist subfigure
axes = subfigs[0].subplots(nrows=1)
# some plot
ax = plt.plot([0,0],[-1,1], lw=1, c='b', figure=subfigs[0])
# some text
subfigs[0].text(0.55, 0.00001, 'Figure 1', ha='center', va='center', rotation='horizontal',weight='bold')
# fill the second subfigure
text_ax = subfigs[1].subplots(1)
# make its background transparent
subfigs[1].patch.set_alpha(0.0)
# remove the axis, not removing it makes no difference regarding the problem
text_ax.set_axis_off()
# generate some text
message = ['word']*50 # 50 is enough to make the problem visable, my usecase has a way longer text
message = ' '.join(message)
# fill in the Text and wrap it
text_ax.text(0.00001, 0.8, message, horizontalalignment='left', verticalalignment='top',size=7, wrap=True)
# call tight layout
# this is neccecary for the whole plot, that is not shown for simplicity reasons
# explaination: subfigure one would be an array of subplots with different scales and descriptions,
# ever changing depending on the data that is to be plotted, so tight_layout mittigates
# a lot of tideos formatting
fig.tight_layout()
Please notice the figures right edge reaches for the whole panel, as seen in the second image
# omitting the wrap, it is clear why the figure is out of bound: somehow the layout gets its information from the unwrapped text
text_ax.text(0.00001, 0.8, message, horizontalalignment='left', verticalalignment='top',size=7, wrap=False)
Is there another way to have the text rendered in the subplot with it correctly noticing the wrapped bounds instead of the unwrapped bounds?
If you modify your code above to remove fig.tight_layout() and to take the text out of the layout then everything works as expected.
# fill in the Text and wrap it
thetext = text_ax.text(0.00001, 0.8, message, horizontalalignment='left',
verticalalignment='top',size=7, wrap=True)
thetext.set_in_layout(False)
There should be nothing that tight_layout does that layout='constrained' cannot do, and constrained layout is much more flexible.
BTW, you might consider subfigs[0].supxlabel() instead of the text command for "Figure 1"

Plotting xarray.DataArray and Geopandas together - aspect ratio errors

I am trying to create two images side by side: one satellite image alone, and next to it, the same satellite image with outlines of agricultural fields. My raster data "raster_clip" is loaded into rioxarray (original satellite image from NAIP, converted from .sid to .tif), and my vector data "ag_clip" is in geopandas. My code is as follows:
fig, (ax1, ax2) = plt.subplots(ncols = 2, figsize=(14,8))
raster_clip.plot.imshow(ax=ax1)
raster_clip.plot.imshow(ax=ax2)
ag_clip.boundary.plot(ax=ax1, color="yellow")
I can't seem to figure out how to get the y axes in each plot to be the same. When the vector data is excluded, then the two plots end up the same shape and size.
I have tried the following:
Setting sharey=True in the subplots method. Doesn't affect shape of resulting images, just removes the tic labels on the second image.
Setting "aspect='equal'" in the imshow method, leads to an error, which doesn't make sense because the 'aspect' kwarg is listed in the documentation for xarray.plot.imshow.
plt.imshow's 'aspect' kwarg is not available in xarray
Removing the "figsize" variable, doesn't affect the ratio of the two plots.
not entirely related to your question but i've used cartopy before for overlaying a GeoDataFrame to a DataArray
plt.figure(figsize=(16, 8))
ax = plt.subplot(projection=ccrs.PlateCarree())
ds.plot(ax=ax)
gdf.plot(ax=ax)

Matplotlib widget, secondary y axis, twinx

i use jupyterlab together with matplotlib widgets. I have ipywidgets installed.
My goal is to choose which y-axis data is displayed in the bottom of the figure.
When i use the interactive tool to see the coordinates i get only the data of the right y-axis displayed. Both would be really nice^^ My minimal code example:
import matplotlib.pyplot as plt
import numpy as np
%matplotlib widgets
x=np.linspace(0,100)
y=x**2
y2=x**3
fig,ax=plt.subplots()
ax2=ax.twinx()
ax.plot(x,y)
ax2.plot(x,y2)
plt.show()
With this example you might ask why not to plot them to the same y-axis but thats why it is a minimal example. I would like to plot data of different units.
To choose which y-axis is used, you can set the zorder property of the axes containing this y-axis to a higher value than that of the other axes (0 is the default):
ax.zorder = 1
However, that will cause this Axes to obscure the other Axes. To counteract this, use
ax.set_facecolor((0, 0, 0, 0))
to make the background color of this Axes transparent.
Alternatively, use the grab_mouse function of the figure canvas:
fig.canvas.grab_mouse(ax)
See here for the (minimal) documentation for grab_mouse.
The reason this works is this:
The coordinate line shown below the figure is obtained by an event callback which ultimately calls matplotlib.Axes.format_coord() on the axes instance returned by the inaxes property of the matplotlib events that are being generated by your mouse movement. This Axes is the one returned by FigureCanvasBase.inaxes() which uses the Axes zorder, and in case of ties, chooses the last Axes created.
However, you can tell the figure canvas that one Axes should receive all mouse events, in which case this Axes is also set as the inaxes property of generated events (see the code).
I have not found a clean way to make the display show data from both Axes. The only solution I have found would be to monkey-patch NavigationToolbar2._mouse_event_to_message (also here) to do what you want.

pandas.plot and pyplot.save_fig create different sized PNGs for same figsize

When I call the same function that uses pandas.plot with the same figsize, I get different sized PNG files. The width is same but the height in pixels changes. I suspect that the length of the x-axis labels changes the height.I have not yet tried directly calling the matplotlib functions.
I have also tried plt.rcParams['figure.figsize'] = (7,4). The problem does not appear to be in how figsize is set. My print_fig_info always produces the desire values.
# Primitive way that confirmed that the figure size does not change
def print_fig_info(label=""):
print(label,str(plt.gcf().get_size_inches()))
def my_plot(df):
global c
print_fig_info("Before plot")
df.plot(kind='bar', figsize=(7,4))
print_fig_info("After plot")
# want to make output files unique
c += 1
plt.savefig("output"+str(c), bbox_inches='tight', dpi='figure')
In your call to savefig you explicitely ask matplotlib to change the figsize to the minimal size that still fits all the elements in via bbox_inches='tight'.
Or in other words, bbox_inches='tight' is especially designed for changing the figure size to the minimum bounding box, and matplotlib is therefore doing what it's being asked for.
Solution: Don't use bbox_inches='tight'.

colorbars for grid of line (not contour) plots in matplotlib

I'm having trouble giving colorbars to a grid of line plots in Matplotlib.
I have a grid of plots, which each shows 64 lines. The lines depict the penalty value vs time when optimizing the same system under 64 different values of a certain hyperparameter h.
Since there are so many lines, instead of using a standard legend, I'd like to use a colorbar, and color the lines by the value of h. In other words, I'd like something that looks like this:
The above was done by adding a new axis to hold the colorbar, by calling figure.add_axes([0.95, 0.2, 0.02, 0.6]), passing in the axis position explicitly as parameters to that method. The colorbar was then created as in the example code here, by instantiating a ColorbarBase(). That's fine for single plots, but I'd like to make a grid of plots like the one above.
To do this, I tried doubling the number of subplots, and using every other subplot axis for the colorbar. Unfortunately, this led to the colorbars having the same size/shape as the plots:
Is there a way to shrink just the colorbar subplots in a grid of subplots like the 1x2 grid above?
Ideally, it'd be great if the colorbar just shared the same axis as the line plot it describes. I saw that the colorbar.colorbar() function has an ax parameter:
ax
parent axes object from which space for a new colorbar axes will be stolen.
That sounds great, except that colorbar.colorbar() requires you to pass in a imshow image, or a ContourSet, but my plot is neither an image nor a contour plot. Can I achieve the same (axis-sharing) effect using ColorbarBase?
It turns out you can have different-shaped subplots, so long as all the plots in a given row have the same height, and all the plots in a given column have the same width.
You can do this using gridspec.GridSpec, as described in this answer.
So I set the columns with line plots to be 20x wider than the columns with color bars. The code looks like:
grid_spec = gridspec.GridSpec(num_rows,
num_columns * 2,
width_ratios=[20, 1] * num_columns)
colormap_type = cm.cool
for (x_vec_list,
y_vec_list,
color_hyperparam_vec,
plot_index) in izip(x_vec_lists,
y_vec_lists,
color_hyperparam_vecs,
range(len(x_vecs))):
line_axis = plt.subplot(grid_spec[grid_index * 2])
colorbar_axis = plt.subplot(grid_spec[grid_index * 2 + 1])
colormap_normalizer = mpl.colors.Normalize(vmin=color_hyperparam_vec.min(),
vmax=color_hyperparam_vec.max())
scalar_to_color_map = mpl.cm.ScalarMappable(norm=colormap_normalizer,
cmap=colormap_type)
colorbar.ColorbarBase(colorbar_axis,
cmap=colormap_type,
norm=colormap_normalizer)
for (line_index,
x_vec,
y_vec) in zip(range(len(x_vec_list)),
x_vec_list,
y_vec_list):
hyperparam = color_hyperparam_vec[line_index]
line_color = scalar_to_color_map.to_rgba(hyperparam)
line_axis.plot(x_vec, y_vec, color=line_color, alpha=0.5)
For num_rows=1 and num_columns=1, this looks like: