How can I improve the quality of my plots in matplotlib? - python

When I save my plots with plt.savefig() in PNG format the figures generated are very bad resolution. I've tried to save in PDF format and define a high number of dpi, but when I use images in that format in LaTeX, my file becomes very heavy and the PDF generated takes to much time to render.
How can I generate images with better quality and do not make my pdf generated by LaTeX so dificult to render?
:

Related

Saving Python (matplotlib) figures as eps with text as tex files

I have been a regular user of Matlab for plotting. I use matlabfrag to save figures as eps for the plot along with a tex file containing labels. The complete plot can be rendered in Latex upon calling the eps and tex files together.
Now that I switched to Python, I am wondering if there is any equivalent of matlabfrag for Python? I am aware of matplotlib2tikz but would like to avoid compute overload for generating plots each time on compiling Latex, as there are way too many plots sometimes.
PS: In the end, I am looking for plots with Latex-like fonts with a controllable font size. Reduced compute load is another requirement.

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

Saving images from plotly

How can I save images generated with plotly in different formats? Only "Download as PNG" is possible from the generated HTML figure. I would need to interact with the figure (change rotation, choose which data to plot) and save an .eps figure for each online modified plot. Thanks a lot!
Plotly supports exporting to EPS (the docs mention that you need the poppler library) and the Figure object has a write_image method that saves a figure to a file.
You can specify the format through the filename and the resolution with the width and height keyword arguments, representing logical pixels.
You can read more on static image exporting in Plotly here. This is a code example:
fig.write_image("name.eps", width=1920, height=1080)
In order to select what is plotted you will have to set the figure's camera controls.

Increase dpi in matplotlib chart without changing it's size

I'm trying to create a pdf using Python's reportlab module.
I generated a png with matplotlib and saved it in the pdf file using report labs canvas.drawImage method.
My problem is that the generated png file is very fuzzy. I specified the size in inches with plt.figure(figsize=(20,10)) and saved the picture with the plt.savefig method.
This works out perfectly (except the fuzzy quality of the picture).
But when I increase the dpi within the savefig method the size of the picture increases.
Is there any way to improve the dpi without changing the picture size.
Or is there a way to resize it to the predefined values?
Thanks!
f = df.plot()
fig = f.get_figure()
fig.set_size_inches((2,2))
fig.savefig('C:/temp/foo.png', bbox_inches='tight', dpi=1500)

Exporting Chaco plots to PDF

I want to save some Chaco plots to PDF (or another vector format such as EPS or SVG). I have already discovered PdfPlotGraphicsContext, but this suffers from two problems:
the PDF's page size is not adjusted to the size of the plot (requires a cropping post-processing step)
it doesn't seem to support transparency
Can these issues be worked around somehow, or is there an alternative way of exporting plots in a vector format?

Categories