Join separate boxplot figure files - python

I have three boxplot figures that I am loading using pickle and I would like to join and plot them side-by-side. It seems that the recommendation is to plot one figure to begin with and use matplotlib subplots. However, due to the nature of the data this would be difficult and it is easiest to make three separate figure files first. All three figures have the same y axis limits and the same x ticks.
How can I join them such that they are side-by-side on the same row?

The solution is to create a new figure with the layout you want, then insert the Axes from the existing figures into it. You can get the Axes from an existing figure with only one set of Axes using fig.axes[0], then insert it into a new figure by following this answer.
PS Emersons unite!

Related

Python3 - Plotting the same matplotlib axes object on multiple figures?

I have a script which I'm adapting to include a GUI. In it, I create a plot with subplots (the arrangement of which depends on the number of plots - e.g. 4 plots go into a square rather than 4-across). That plot (with a subplot for each of the "targets" analyzed) gets saved to a .png.
In building the GUI, I'm writing up the 'results' frame and would like to show these individual subplots on their own tabs. I've written the code to lay out the frame how I want it, but in order to separate the subplots into their own plots, I need to draw the completed Axes object (e.g. the entire subplot for that target) onto a new figure in the frame.
Since the number of subplots isn't known before runtime, I already have my Axes objects/subplots in an array (/list?) axs, whose members are the individual Axes objects (each containing data points created with ax.scatter() and several lines and annotations created with ax.plot() and ax.annotate).
When I initially create the axes, I do so with
fig, axs = plt.subplots(num='Title', nrows=numrow, ncols=numcol,
figsize=[numcol*5, numrow*5],
subplot_kw={'adjustable':'box', 'aspect':1})
Is there a way to now take these axes and draw them onto a new figure (the one that will be contained in the 'results' frame of the GUI)? In my searches, I only came up with ways to plot multiple axes onto a single figure (i.e. how to use subplots()) but nothing came up on how I'd throw a pre-existing Axes object into a new figure that it wasn't originally associated with. I'd rather not re-draw the axes from scratch -- there's quite a bit of decoration and multiple datasets / lines plotted onto them already.
Any ideas? Happy to post code as requested, but since this more of a "How do I do this" than a "why doesn't my code work", I didn't post much of it.
Thank you!
I believe that's not possible and you will have to recreate the Axes objects inside the other figure. Which is just a matter of code reorganization. Note that your approach would not noticeably improve rendering performance. Matplotlib would have to re-render the Axes objects anyway, and that's the computationally expensive part. Creating the objects is relatively cheap.
What you're trying to do is pretty much this:
from matplotlib import pyplot
pyplot.ion()
figure1 = pyplot.figure()
axes = figure1.add_subplot()
axes.plot([0, 1], [0, 1])
figure2 = pyplot.figure()
figure2.add_axes(axes)
Which raises:
ValueError: The Axes must have been created in the present figure
And the documentation of add_axes() notes:
In rare circumstances, add_axes may be called with a single argument, an Axes instance already created in the present figure but not in the figure's list of Axes.
So that's a pretty clear indication that this is not a supported use case.

How to Mix Markers and Labels in Matplotlib Legend

I am working on a figure where space is constrained and I want to combine my legend using parentheticals as in this picture.
At the moment I just make some parenthesis in my labels for the plot and then edit the figure in Inkscape later on to add the missing markers, but this makes iterating on the plot more expensive in terms of time before having a usable figure. Is there any way to hack matplotlib into doing something similar without having to go through an external program?

Create a figure of figures with matplotlib

I would like to know if there is a way to combined several figures created with matplotlib in one unique figure.
Most of the existing topics are related to multiple plots within one figure. But here, I have several functions which all create one elaborated figure (not just a plot, the figure itself is a multiple plot with texts, title, legends,...)
So instead of just doing the layout of those several figures using a software like Word, is there a way to directly combined all my figures in one unique figure under python ?
Thank you in advance !
The concept of figure in matplotlib does not allow to have a figure inside a figure. The figure is the canvas for other artists, like axes. You may of course add as many axes to a figure as you like. So for example instead of one figure with 4 axes and another figure with 6 axes, you can create a figure with 10 axes.
A good choice may be to use the gridspec, as detailed on the respecive matplotlib page.
After additional researches, it seems my problem has no easy solution within Matplotlib itself. Multiple figures layout needs external post-processing of plots.
For those having the same problem, here is an interesting link :
Publication-quality figures with matplotlib and svgutils

How to sit the size for seaborn pairplot chart?

I am trying to draw an sns.pairplot with one value in x_axis but multiple values in y_axis
This is what I got. All the figure in one row. Does Anyone know how I can get a bigger plot? or in multiple columns?
The chart is here:
As per the comment to your question, the reason that the plot is small, is because it does not fit into the Jupyter Notebook output cell. Try right clicking and opening in a new tab or saving it to a file.
If you want display the figure within a Notebook, you can make several calls to sns.pairplot with a subset of columns each time. You could also use sns.FaceGrid with plt.scatter for more granular control over what is plotted where.

How to remove renderers from a plot?

I'm experimenting with Bokeh server. I have a document with three figures and I'm trying to update two of them depending on the selection I perform on the third. The number of lines to plot in the two figures changes every time.
If I could use multi_line, this would be trivial: I would change the xs and ys in the data_source of the multi_line.
Alas, I need to use multiple scatter plots because multi_line does not support hover and I need it.
So, what I would like to accomplish is to clear the two plots every time I select something in the third, and display the scatter plots corresponding to the new selection.
There are a few possible workarounds, of course (appending scatter points to have a single GlyphRenderer with all scatter plots together, for example, but this would mean using very clunky ways to send the right hover message...). But if it was possible to just clear and update single figures, everything would be cleaner. I couldn't find anything in the docs, however.
I have read the thread you created on the mailing list and this other thread where Bryan says:
Technically, glyph renderers are stored in the .renderers property of
Plots, but I would not recommend rooting around there by hand.
Specifically the "Continuous Updating" notebook I linked earlier has
an example of updating both the data and appearance of an existing
glyph using python and push_notebook. There is not any easy way to remove glyphs at the moment,
other options would be:
recreate a new plot
set the glyph to be invisble
update the glyphs data
So it seems they are the only solutions at the moment

Categories

Resources