how to combine matplotlib figures after they are saved? - python

I know that the ideal solution would be to plot them in multiple axes by calling plt.subplots(nrows=x, ncols=y) but the code where I am plotting was already structured in a way which would make it very hard for me to convert to plotting in the same context like that...
So that being said, I have a folder with 5 saved .pdf images that were created in matplotlib. Is there a way I can open these up and save them in the same file while maintaining the .pdf filetype (i.e. not converting them to .pngs in some image editing program)?

Related

How to export plotly plots, tabs and images into a single html

I would like to know if there is a way to export multiple plotly plots, tabs and images into a single HTML file.
In detail the problem is this. I want to create a standalone .exe file with python able to plot several graphs and images, given some input files containing the data; writing code with PySimpleGUI for the GUI.
But once the several interactive graphs, tabs and images are plotted in a window, I would like to add in the window two buttons to export these data respectively in a excel file (so with no interactivity) and in a HTML file (to keep interactivity).
The first point is no trouble for me: I use xlsxwriter to export images and graphs as images. The problem is the second point: I would like to export all these data keeping interactions in graphs in a single HTML with a certain design I choose. Remember the fact that I want to build a standalone .exe file, because this program will run in a PC with Python not installed.
My question is this: is there some python library or html writer in python I can use to do this?
P.S.: plotly graphs are not subplots of a single plots but each one is a single plot, each one will have its own zoom etc. commands.

Matplotlib: Saving an self-contained, editable Figure

Is there way to save a "Figure" in matplotlib to a file such that if you later wanted to modify the Figure, e.g. change data points, resize the figure, etc. you could load up the file in a new python script and do that?
Right now I save the majority of my plots as Pdfs, but that doesn't allow me to make edits later on. I have to go dig up my old source code and data files. I've lost track of the number of times I've lost the plot-generating code and have to essentially reproduce it all from scratch.
It would be nice if I could just save a plot as a self-contained data file, like Photoshop does with its .psd files, so that I can just load it up directly, type "object.plot()", and not have to worry about external dependencies. Does such a format exist, or if not is there any way I could achieve this?
There is a method of saving the plotted object called pickling. I don't have much experience with it but it should allow you to save the plot to a file using
fig = plt.figure
pl.dump(fig, file('file_name.pickle','w'))
and using
fig = pl.load(open('file_name.pickle','rb'))
fig.show()
to load the saved graph.
Matplotlib warns that, "Pickle files are not designed for long term storage, are unsupported when restoring a pickle saved in another matplotlib version". To be safe, I would just save the array containing the data to the plot to either a .csv or .txt file, and keep this file in a folder with the python file to plot the graph. This way you will always be able to plot your data (no matter the version of matplotlib you are using). You will also have the data and code in the same place, and you can easily read the data from the .csv or .txt file, save it to arrays, and graph it using
file = open("file_name.txt", "r")
if file.mode == 'r':
data = f.read().splitlines()
data_array1 = data[0].split(",")
data_array2 = data[1].split(",")
p, = plt.plot(data_array1, data_array2)
I also suggest uploading your python files along with your .csv or .txt files to Github.
If you would like to read more about pickling in matplotlib I suggest reading the two pages linked below.
(1) Pickle figures from matplotlib
and (2) https://matplotlib.org/3.1.3/users/prev_whats_new/whats_new_1.2.html#figures-are-picklable

Is there any way to save all graphs generated using the script to a single file (image preferably)

The scripts I use generate a lot of graphs. I was wondering if there was a way to save them or specified ones into a single file as image or pdf for quicklooks.
Thank you.
If you are using matplotlib, it would be easiest to use the subplots feature. This will make all of your graphs part of a single object, which can be saved as an image like you want. I would redirect you to the matplotlib website for a ton of good example on the subject. https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.subplots.html
Then, do
fig.savefig('mysweetsubplots.png')
and you are done.

saving multiple figures in python to create array plot

So say I have several 2 d plots of the usual x-y format:
ex: 1) x=(0,1,2,3,4,..100), y=(0,2,4,6,8,10,....)
I would like to have my x-y scatter plots saved somehow, so that once all the x-y plots are generated, I could merge them on to one big graph where these graphs now form an array. Just imagine if each of the plots represent some kind of a time evolution. I have already tried using savefigure but cannot see a saved file. Thanks!
Can't find the figures after issuing plt.savefig(some_filename)? Find out what is your current working directory, the figure files should be there:
import os
os.getcwd() #Gives you the current working directory.
Of course, if you not only provided the filename but also the path to it for plt.savefig(), you should see your figure in the path you specified.
You figure comes out OK right? If you issue a plt.show(), do you get the correct figure?

Writing a table to a matplotlib pdf file

I am using matplotlib and a modified version of this example to generate plots in pdf files. So I am plotting each plot on a single page and the results are just fine.
Now I would like to list all the data used in the plots in a rather long table. This table should be placed below the last plot (so not each plot should get its own table).
Is there a way to plot LaTeX like tables in a pdf file using matplotlib?
In principle, you can place almost any TeX stuff onto a plot using something like plt.text(1,2,r'$a^2+b^2=42$'). For aligning equations things like eqnarray work as well, like this. Just don't forget to use raw strings, for otherwise python can misinterpret TeX commands which start with backslashes.
Unless using a plot to write text, I think it is save to say, that it is not possible to only write a table to a matplotlib pdf output file.
Currently I am using tex to write the table and pyPdf to merge the two results. I think this is the cleanest solution to the problem.

Categories