I am trying to create a figure for a paper compounding previous matplotlib figures that I have saved in pickle format.
For doing so I have used the get_offsets() command, which allows me to retrieve the points of my scatter plots and get_facecolor() to get the color of the points.
Code:
### Loading my figure (visible in the enclosed picture)
with open('Figures paper/Individual spectra figures/Spectra_1000_GALL_chunk_0.pickle', 'rb') as fl:
ax1 = pickle.load(fl)
### Getting the plot features
xd = axp.collections[0].get_offsets()[:,0]
yd = axp.collections[0].get_offsets()[:,1]
cd = axp.collections[0].get_facecolor()
ax.scatter(xd,yd, s=ar,c=cd ,alpha=0.8)
And then I can plot the points in the sub-panel that I want of my new compound figure. However, the colorbar of my initial plot does not appear, and I don't know how to retrieve it to include it in the new plot.
I have tried creating a new colorbar with the data extracted from the scatterplot, but it's not possible because .collections[0].get_facecolor() gets only the value of the colors, but not the original variable that originated them.
Any idea about how to do it?
Yes and maybe. Here's code demonstrating retrieval and reuse of a pickled colorbar -- if you pickled the whole figure originally this should apply.
BUT I suspect you would like not just the colorbar but the
colormapfrom data to color. And I don't think that's explicit anywhere in the colorbar. "How to extract a colormap from an unpickled colorbar" might be your next question.(If I didn't have any other way, I'd recreate the colormap by making a
LinearSegmentedColormapusing an image editor to get the colors at each tick value.)And the four states of the plot: