Matplotlib's Figure and Axes explanation - matplotlib

I am really pretty new to matplotlib, though I know that it can be very powerful.
I've been reading number of tutorials and examples and it's a real hassle to understand how does matplotlib's Figure and Axes work. I am illustrating, what I am trying to understand, with the attached figure.
I know how to create a figure instance of certain size in inches. However, what bothers me is how can I create subplots and then axes, within each subplot, with relative coordinates (bottom=0,left=0,top=1,right=1) as illustrated.
So, for example I want to create a "parent" plot area (say (6in,10in)). Then, I want to create two subplot areas, each with size (3in,3in), with 1in space from the top, 2in space between the two vertical subplot areas and 1in from bottom. Then, 1in space on the left and 2in space on the write. In the same time, I would like to be able to get the coordinates of the subplot areas with respect to the main plot area.
Then, inside the first subplot area, I'd like to create 2 axis instances, with Axis 1, having coordinates with respect to Subplot Area1 (0.1,0.7,0.7,0.2) and Axes 2 (0.1,0.2,0.7,0.5). And then of course I'd like to be able to plot on these axes e.g., ax1.plot()....
If you could provide a sample code to achieve that, then I can study it.
Your help will be very much appreciated!

a subplot and an Axes object are really the same thing. There is not really a "subplot" as you describe it in matplotlib. You can just create your three Axes objects using gridspec without the need to put them in your "subplots".
There are a few different ways to create Axes instances within your figure.
fig.add_axes will create an Axes instance at the position given to it (you give it [left,bottom,width,height] in figure coordinates (i.e. 0,0 is bottom left, 1,1 is top right).
fig.add_subplot will also create an Axes instance. In this case, rather than giving it a rectangle to be created in, you give it the number of rows and columns of subplots you would like, and then the plot_number, where plot_number starts at 1, increments across rows first and has a maximum of nrows * ncols.
For example, to create the top-left Axes in a grid of 2 row and 2 columns, you could do the following:
fig.add_subplot(2,2,1)
or the shorthand
fig.add_subplot(221)
There are some more customisable ways to create Axes as well, for example gridspec and subplot2grid which allow for easy creation of many subplots of different shapes and sizes.

Related

Holoviews: Format legend and colors of Spread and Curve Overlay

Given a tidy Pandas column with 4 or more columns, I want an otherwise very straightforward plot: two of the columns should be the x-y axes of a single figure, and one of the columns should index an Overlay of N Curve objects based on the x-y columns, and N Spread objects, using the final column as error. So if N=4 there should be 4 curves and four spreads. The curves and spreads with same index should be the same color, and the legend should attest to this.
Using table.to(hv.Curve,'col1','col2') I can get a Holomap for the curves, and with some effort I can do the same for the spread. If I then call .overlay() I get a nice figure for the curves including a legend, but when I do the same for the spread the legend vanishes. If I overlay the two, the legend likewise vanishes and the color cycle stops working, making all curves and spreads the same color. If I create a Holomap of curve*spread objects, then the colors match but the legend is still gone.
This seems like a very standard plot, but I can find very little in the Holoviews docs about pairing different Elements or controlling the legend.
This is a bit difficult to answer without any concrete code, for example I can't reproduce some of the issues you are describing. However the first issue is simply that show_legend is not enabled by default for the Spread elemen. In the case of plotting a Curve and Spread using .to and .overlay, here is what I can confirm works:
%%opts Spread [show_legend=True width=600] Overlay [legend_position='right']
df = pd.DataFrame({
'index': np.arange(100), 'y': np.random.randn(100).cumsum(),
'err': np.random.rand(100)+0.1, 'z': np.repeat(np.arange(10), 10)
})
ds = hv.Dataset(df)
ds.to(hv.Curve, 'index', 'y', 'z').overlay() * ds.to(hv.Spread, 'index', ['y', 'err']).overlay()
If I create a Holomap of curve*spread objects, then the colors match but the legend is still gone.
This is indeed a current limitation since we recommended against nesting objects in this way in the past, however I have just opened this PR which will allow this approach as well.

subplot with shared axis but different ticks and labels

I make a plot with different subplots (using gridspec.GridSpec). Two subplots share the same x-axis (sharex=ax1 in the definition of the second subplot).
However, as one subplot shows the indices of the chronologically sorted data, and the second subplot shows the corresponding decades, I want seperate ticks and labels for the x-axes of both plots. This seems not possible, a unique set of labels and ticks are assigned to both subplots. Until now, I can only:
use different x-axes and thus assign two sets of ticks and labels.
In that case, the axes are not alligned although
ax1.set_xlim([start, stop]) are similarly defined for both subplots
use a common x-axis and one set of ticks and labels
I do not find a solution for this on the internet. Is someone able to help? Thank you in advance!

Seaborn Heatmap Colorbar Location

The cbar_kws argument of seaborn.heatmap accepts the parameters that fig.colobar accepts.
Is there a way to adjust the placement of the colorbar, simply to adjust the location to the left (especially when the correlation matrix is adjusted to have only a lower triangle).
I can adjust the labels by overriding the tick labels. As of now I still have to adjust the upper-right borders in post-processing, but it would make things much easier if I didn't have to edit the color bar as well.
heatmap accepts a cbar_ax argument; if you want to specify the position of the colorbar, the best thing to do is to set up the figure how you want it and then pass the specific axes.
You can also move axes around after plotting through normal matplotlib commands.

How can I get and set the position of a draggable legend in matplotlib

I'm trying to get and set the position of a draggable legend in matplotlib. My application consists of an interactive GUI, which has a redraw/plot function that should perform the follow steps:
save the position of the current legend.
clear the current axes and perform various plotting operations, which may or may add labels to their plots.
build a new draggable legend (ax.legend().draggable()) and restore the old position of the legend.
In between these steps the user is free to drag the legend around, and the goal is to persist the legend position when the plots are redrawn.
My first approach was to use oldpos = legend.get_bbox_to_anchor() and legend.set_bbox_to_anchor(oldpos) in steps 1 and 3. However this causes to move the legend completely off the visible area.
Note that I have to use ax.legend() and cannot use fig.legend(lines, labels), since step 2 is completely decoupled, i.e., I don't know anything about lines and labels in step 3. According to answers to the question How to position and align a matplotlib figure legend? there seems to be a difference between these two possibilities regarding axes or figure coordinates. Obviously my problem calls for figure coordinates, but I haven't fully understood how to convert the bbox to a "bbox in figure coordinates".
The even more severe problem I just realized is that apparently legend.get_bbox_to_anchor() always seems to return the same values irrespective of the drag position. So maybe the anchor can only be (ab-)used to manipulate the position of static legends? Is there another/proper way to save and restore the position of a draggable legend?
By looking at the implementation of Legend I found out that there is an undocumented property _loc, which exactly does what I want. My solution now looks astonishingly simple:
oldLegPos = ax.get_legend()._loc
# perform all plotting operations...
legend = ax.legend().draggable()
legend._loc = oldLegPos
It looks like _loc automatically stores figure coordinates, since I do not have to convert the coordinates in any way (eg. when the plotting operations completely change the axes ranges/coordinates).

Positioning figure and table in figure

I'm trying to put both a plot and a table in a figure, but I want some whitespace to separate the two. How do I position the table/plot at arbitrary positions? What I have now is the table of values showing up IMMEDIATELY under the x-axis (so that it's actually colliding into my axis labels...)
I don't know matplotlib at all...The documentation is not written very well either...
To position something in a figure you have to use the function set_position([left, bottom, width, height]). Example:
matplotlib.pyplot.axes().set_position([0.15, 0.20, 0.80, 0.70])