How to save a figure with multiple overlapping plots? - matplotlib

I am plotting multiples signals overlapping in one figure using matplotlib and then saving it in pdf/eps using matplotlib.pyplot.savefig, the problem is that when I try to insert the figure in a pdf-latex file, it takes like 1.5 min to load because it starts rendering every plot. Is there any way to properly save or create these plots in one figure?
The final pdf works perfectly when I use preview on macOS, regularly when I use edge or chrome navigators and it is a disaster if I use adobe-reader. I am looking for a multiplatform solution.

Related

DataFrame (pandas) plot opens window but there is no plot

I am unable to produce any charts in Python (matplotlib 3.5.1) with the pandas DataFrame plot() method. A window opens and the axes return value is <AxesSubplot:> as opposed to returning an object like that prints as somethig like <matplotlib.axes._subplots.AxesSubplot at 0x7f3958bcf9d0>, which is what I usually see when the plot works.
The backend is QtAgg and as far as I can tell from a poke between fora pages that report a similar problem this should be all right. This is also not a problem with needing to run matplotlib.pyplot.ion() as I see a window open, but it is black with no plot in it.
Any advice would be useful, thanks!
I have resolved the (mis)behaviour without really being sure that I have resolved the issue responsible for this misbehaviour.
As looked around for a way out, I found this discussion (How to change backends in matplotlib / Python) which made a lot of getting the back end setting right. Wondering if that would make a difference, I changed the back end. The QtAgg back end loaded by default from the matplotlibrc file and so following the instructions in the post on how to permanently change the back end, I modified the file so that the back end is now Qt5Agg. The data now plots.

Matplotlib plot a series of interactve plots one plot at a time

I'm trying to plot a series of plots one plot at a time. When a plot is displayed I want to be able to interact with it (i.e. zoom, pan, etc.) and wait until I'm done before the next plot is displayed.
I saw the post matplotlib plot and then wait for raw input, but I couldn't get it to work.
I'm using Spyder and Python 3.4.
Below is the code snippet of what I'd like to do. However the plots seem to get blocked by the waiting for input.
for i in range(4):
plt.figure()
plt.plot([x for x in range(100)],'x')
plt.show(block=False)
a=input('next plot')
If you are looking for interactive plots in Python, you might want to take a look at the Charts library. It enables you to use the excellent Highcharts javascript library to make beautiful and interactive plots. Highcharts uses the HTML svg tag so all your charts are actually vector images.
Some features:
Vector plots which you can download in .png, .jpg and .svg formats so you will never run into resolution problems
Interactive charts (zoom, slide, hover over points, ...)
Usable in an IPython notebook
Explore hundreds of data structures at the same time using the asynchronous plotting capabilities.
Disclaimer: I'm the developer of the library
I believe this is resolved in the recent matplotlib v.2.0.0 release. At least it resolved this problem for me.

PTVS plotting pandas Dataframes and Series with matplotlib in python debug interactive

Is it possible to plot pandas objects inside the PTVS interactive debugger? Is it possible to save plots to disk as jpeg's?
I think I was able to do this when I first started using PTVS (last year, its awesome by the way!) but I just tried again and I dont get any plots appearing. I cant remember if I had to do something special to get this to work and from doing some google searches I get a confusing picture of the current best practice in this regard.
I want to be able to plot diagrams from my debug interactive window, similar to what is shown on this pandas tutorial.
http://pandas.pydata.org/pandas-docs/stable/visualization.html
Is this possible?
Visual Studio Professional 2013 update 4 (latest I think)
PTVS 2.1.21008.00 (latest I think)
All help is greatly appreciated.
-Jason
[edit: more info on this http://pytools.codeplex.com/discussions/574776 ]
Unfortunately not. The regular interactive has an IPython mode that, among other things, enables inline plots. But the Debug interactive doesn't have that.
You can, of course, still load matplotlib in the Debug interactive and tell it to plot things. But because there's no integration of event loops between it and VS in that mode, the plots will basically work like modal windows - you won't be able to continue debugging or otherwise interact with VS until you close the plot window.

Photoshop jsx image grid

What I am ultimately trying to do is to create a grid of images for print that are minor variations of the same thing (different text is all). Looking through online resources I was able to create a script that changes the text and exports all of the images necessary (several hundred). What I am trying to do now is to import all of these images into a new photoshop document and lay them all out in a grid and I can't seem to find any examples of this.
Can anyone point me in the right direction to place a file at a specific coordinate (I'm using CS5 and have the design suite so if there is a way in illustrator to do this quickly...)?
Also, I'm open to other ideas on how to do this (even other programs) easily. It's for labels so the positioning on the sheet has to be pretty precise...
The art layer object has a translate() method that takes delta x and y params. You'll need to open each image, copy it to the target document, get its current location (using artLayer.bounds) and do the math to find the deltas to position it where you want it. Your deltas can be in pixels so you'll get plenty of precision.
Check out your 'JavaScript Scripting Reference' pdf in your Adobe install directory for more details.
Ok I'm marking Anna's response as the answer because though I didn't fully test it, it seems like it should work and answers the original question with jsx. However I'm also leaving my final solution in case anyone else runs across this with the same issue and may prefer this method as well.
What I ended up doing instead is using InDesign. I figured out that it has a grid option that lets you import a number of files and place them all in an equal grid in a single command. This is almost exactly what I was looking for, except that it leaves a small border/margin in between the columns and grids and mine were designed to meet exactly.
I couldn't figure out how to make it not have the border (I have very little experience with InDesign, it may be possible). However I was able to select all my images and scale them uniformly to be the correct size, then I just selected each column and dragged it over to snap to the adjacent column and the same with rows...

Saving "heavy" figure to PDF in MATLAB - rendering problem

I generate a figure in MATLAB with large amount of elements (100000+) and want to save it into a PDF file. With zbuffer or painters renderer I've got very large and slowly opened file (over 4 Mb) - all points are in vector format. Using OpenGL renderer rasterize the figure in PDF, ok for the plot, but not good for text labels. The file size is about 150 Kb.
Try this simplified code, for example:
x=linspace(1,10,100000);
y=sin(x)+randn(size(x));
plot(x,y,'.')
set(gcf,'Renderer','zbuffer')
print -dpdf -r300 testpdf_zb
set(gcf,'Renderer','painters')
print -dpdf -r300 testpdf_pa
set(gcf,'Renderer','opengl')
print -dpdf -r300 testpdf_op
The actual figure is much more complex with several axes and different types of plots.
Is there a way to rasterize the figure, but keep text labels as vectors?
Another problem with OpenGL is that is does not work in terminal mode (-nosplash -nodesktop -nodisplay) under Mac OSX. Looks like OpenGL is not supported. I have to use terminal mode for automation. The MATLAB version I run is 2007b. Mac OSX server 10.4.
This is a funny one. Your problem is not Matlab, it's Ghostscript (Matlab creates PDFs by calling Ghostscript, at least on Windows). When I run
x=linspace(1,10,100000);
y=sin(x)+randn(size(x));
plot(x,y,'.')
print -dpsc2 test.ps
I've got a 2Mb PS file (all vector, of course), which when compressed became a 164Kb ZIP. One would expect to get more-or-less the same result when converting PS to PDF, but ps2pdf test.ps produced your 4Mb file!
Since you are on a Mac, you probably have Distiller. I'd give it a try — generate PS files as above, and then run them through Distiller; you should get a 150K vector PDF.
If you insist on rasterizing, I can suggest printing the figure without any axes or labels to a tiff, opening the tiff, and recreating axes and labels on top of it.
If you don't want to go with a 2D histogram (i.e. an image where pixel brightness corresponds to density of points) as BlessedKey suggests, it looks like the only good way is to do the rasterizing yourself, as mentioned by AB.
getframe followed by frame2im seems to be the way to go for that. Unfortunately, getframe returns empty if you run with -nodisplay. Therefore, you'd have to save the figure as .fig, and on another computer run a script that
opens the figure, gets the content of the axes with getframe, displays the image from getframe and then saves to pdf.
As an alternative to simple plotting or a 2D histogram, you may want to look into scattercloud, which combines plotting the points with density information, by the way.
If at all possible you should try to subsample your problem before building the illustration. If you are plotting points on a curve then 10,000 is probably more than you need. A modern printer is only about 600 DPI afterall.
If the points are illustrating a cloud with some density properties, a better solution may be to build a two dimensional histogram first, and illustrate that with imshow or imagesc.
If multiple clouds are being illustrated with different colors you may be interested in building one such image for each cloud and the combining them with transparency.