saving multiple figures in python to create array plot - python

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?

Related

how to combine matplotlib figures after they are saved?

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)?

matplotlib: plot and fill high resolution coastline

I have (lon,lat) data that mark a high resolution coastline. It is a very irregular coastline (estuary) and so when I use matplotlib's fill or fill_between functions, it does not work as it is supposed to, as it also fills parts outside the coast, inside the estuary, as well.
I cannot use cartopy or basemap since their "full" resolution is not enough for what I need...
I have read a bunch of other posts (e.g.):
shapefile and matplotlib: plot polygon collection of shapefile coordinates
https://gis.stackexchange.com/questions/131716/plot-shapefile-with-matplotlib
That had the same issue as I do, but they work with shapefile, so I am not sure how can I use it with my data.
For what I have read so far, I think the answer to my problem is to create a closed polygon using path where one side is the coastline (from the data I have), and the others can be straight lines outside of my boundaries...I have read the documentation for path and Patch but have not been able to make it work. How can I do this?
Here is what I get using:
plt.plot(lonc,latc,'.')
and
plt.fill(lonc,latc)
(I am sorry I cannot provide the actual data I am working with)...

Matplotlib fill_between() plot not always displayed in a pdf

I often use fill_between() to represent the error on my time series plots and save them as pdf files (using plt.savefig()) which I then insert into either PowerPoint presentations, or MS Word files. I've personally never had any issues in seeing the plots. However, recently, a couple of instances occurred where others could not see the specific plots generated by the fill_between() function. The other plots in the figure were visible without issue.
I wonder if anybody else has had this issue, and any possible solution for this. Would saving as a .png file ensure that the shaded regions always show up?
Thanks
EDIT: The persons who cannot see the shaded regions due to fill_between() are anonymous reviewers of my research paper, hence its not possible to go communicate with them to investigate if the problem is in their systems or mine. Rather, my question is more generic and not situation-specific. Once a plot is saved (in this case, as a pdf), I didn't expect it to change.
An example of fill_between() in my code:
def plot(ts,err,Label,col,sty):
plt.plot(ts.index,ts,col,linestyle=sty,linewidth=2, label=Label)
plt.fill_between(ts.index,ts-err, ts+err
, alpha=0.15, edgecolor=col, facecolor=col)
plot(dataTs,err(dataTs),'label','k','-')
plt.savefig('../Figures/test.pdf', bbox_inches='tight')
Output plot:
The machine and the OS that I use: Macbook Pro, macOS Sierra.

Bokeh line graph looping

I’ve been working on bokeh plots and I’m trying to plot a line graph taking values from a database. But the plot kind of traces back to the initial point and I don’t want that. I want a plot which starts at one point and stops at a certain point (and circle back). I’ve tried plotting it on other tools like SQLite browser and Excel and the plot seems ok which means I must be doing something wrong with the bokeh stuff and that the data points itself are not in error.
I’ve attached the images for reference and the line of code doing the line plot. Is there something I’ve missed?
>>> image = fig.line(“x”, “y”, color=color, source=something)
(Assume x and y are integer values and I’ve specified x and y ranges as DataRange1d(bounds=(0,None)))
Bokeh does not "auto-close" lines. You can see this is the case by looking at any number of examples in the docs and repository, but here is one in particular:
http://docs.bokeh.org/en/latest/docs/gallery/stocks.html
Bokeh's .line method will only "close up" if that is what is in the data (i.e., if the last point in the data is a repeat of the first point). I suggest you actually inspect the data values in source.data and I believe you will find this to be the case. Then the question is why is that the case and how to prevent it from doing that, but that is not really a Bokeh question.

matplotlib shows different figure than saves from the show() window

I plot rather complex data with matplotlib's imshow(), so I prefer to first visually inspect if it is all right, before saving. So I usually call plt.show(), see if it is fine, and then manually save it with a GUI dialog in the show() window. And everything was always fine, but recently I started getting a weird thing. When I save the figure I get a very wrong picture, though it looks perfectly fine in the matplotlib's interactive window.
If I zoom to a specific location and then save what I see, I get a fine figure.
So, this is the correct one (a small area of the picture, saved with zooming first):
And this one is a zoom into approximately the same area of the figure, after I saved it all:
For some reason pixels in the second one are much bigger! That is vary bad for me - as you can see, it looses a lot of details in there.
Unfortunately, my code is quite complicated and I wasn't able to reproduce it with some randomly generated data. This problem appeared after I started to plot two triangles of the picture separately: I read my two huge data files with np.loadtxt(), get np.triu(data1) and np.tril(data2), mask zeroes, NAs, -inf and +inf and then plot them on the same axes with plt.imshow(data, interpolation='none', origin='lower', extent=extent). I do lot's of other different things to make it nicer, but I guess it doesn't matter, because it all worked like a charm before.
Please, let me know, if you need to know anything else specific from my code, that could be relevant to this problem.
When you save a figure in png/jpg you are forced to rasterize it, convert it to a finite number of pixels. If you want to keep the full resolution, you have a few options:
Use a very high dpi parameter, like 900. Saving the plot will be slow, and many image viewers will take some time to open it, but the information is there and you can always crop it.
Save the image data, the exact numbers you used to make the plot. Whenever you need to inspect it, load it in Matplotlib in interactive mode, navigate to your desired corner, and save it.
Use SVG: it is a vector graphics format, so you are not limited to pixels.
Here is how to use SVG:
import matplotlib
matplotlib.use('SVG')
import matplotlib.pyplot as plt
# Generate the image
plt.imshow(image, interpolation='none')
plt.savefig('output_image')
Edit:
To save a true SVG you need to use the SVG backend from the beginning, which is unfortunately, incompatible with interactive mode. Some backends, like GTKCairo seem to allow both, but the result is still rasterized, not a true SVG.
This may be a bug in matplotlib, at least, to the best of my knowledge, it is not documented.

Categories