How do you improve matplotlib image quality? - python

I am using a python program to produce some data, plotting the data using matplotlib.pyplot and then displaying the figure in a latex file.
I am currently saving the figure as a .png file but the image quality isn't great. I've tried changing the DPI in matplotlib.pyplot.figure(dpi=200) etc but this seems to make little difference. I've also tried using differnet image formats but they all look a little faded and not very sharp.
Has anyone else had this problem?
Any help would be much appreciated

You can save the images in a vector format so that they will be scalable without quality loss. Such formats are PDF and EPS. Just change the extension to .pdf or .eps and matplotlib will write the correct image format. Remember LaTeX likes EPS and PDFLaTeX likes PDF images. Although most modern LaTeX executables are PDFLaTeX in disguise and convert EPS files on the fly (same effect as if you included the epstopdf package in your preamble, which may not perform as well as you'd like).
Alternatively, increase the DPI, a lot. These are the numbers you should keep in mind:
300dpi: plain paper prints
600dpi: professional paper prints. Most commercial office printers reach this in their output.
1200dpi: professional poster/brochure grade quality.
I use these to adapt the quality of PNG figures in conjunction with figure's figsize option, which allows for correctly scaled text and graphics as you improve the quality through dpi.

Related

Plot text is lost when importing matplotlib images to Microsoft Word 2013

I am using matplotlib 1.4.3 on python 2.7 and am saving my plots as ".eps" files on my linux server.
For my research paper, I need to include these images into MS Word 2013 where the text of my research lies. However, when I import the image into MS word, all the text from the plot is lost e.g. axis labels, tick labels, legends. Only the non-text parts of the image show up.
I searched multiple forums
I have tried converting the .eps file to .emf format as well using https://cloudconvert.com/eps-to-emf and few other similar services, but the issue with this process is that the font of my text changes. I have also used Inkscape 0.91for converting .eps to .emf and there also the quality is not good (the font changes, the grey shades become shades of red) and I am not able to replicate the exact style as was there in .eps file.
Has anyone been able to solve this, issue ?
What seems to work is that I save as .png, then import in Powerpoint, select and copy it and finally paste special in Word as .jpeg or .gif

Is it possible to generate vector based pdf using wordcloud

I am using wordcloud in python to generate word clouds.
I was able to reproduce this example on my machine, and then tried to change the last line plt.show() to plt.savefig('image.pdf') to have a pdf output.
I had a pdf with the same result, however, the pdf seems like pixel-based instead of vector-based. When I focus a particular point in the pdf it becomes a very low-quality picture.
Is there any way to produce vector-based pdf using wordcloud? If not, is there any other library that I can produce vector-based (pdf) wordclouds in Python?
If wordcloud can generate any sort of vector output such as ps or svg, inkscape can usually convert it to a PDF without rasterizing it. You can even do this headless, e.g. inkscape my.svg -A my.pdf.
Hmm, looking at wordcloud, it looks like it uses PIL. I don't think that PIL can produce vector images. But if you could use the logic in wordcloud and separate it from PIL, you can get vector fonts onto PDFs by drawing onto a reportlab canvas.
You can save the images in a vector format so that they will be scalable without quality loss. Such formats are PDF and EPS. Just change the extension to .pdf or .eps and matplotlib will write the correct image format.
plt.savefig('destination_path.eps', format='eps')
plt.savefig('destination_path.pdf', format='pdf')
I have found that eps/pdf files work best.

How to get highest quality .tiff from .pdf using linux software?

I have very picky label printer and need to convert black and white .pdf file to .tiff image saving as much quality as possible. I have an example .tiff converted using some adobe software and that's the quality I am aiming for.
Tried using graphicsmagick for the job, but I just can't get close enough. Here's a section of an image: on the left side is my try and on the right is the one converted with adobe software:
As you can see adobe converted image is much thicker but those images are of same resolution (200x400)
Can somebody give me a hint how to convert .pdf to .tiff using most common python libraries or Ubuntu packages and get similar results?
Cheers

matplotlib: saved imshow pdf looks different from the plot window

The following figure was plotted using imshow in matplotlib with option interpolation='none':
However, after I saved it as a pdf file, the saved pdf file looks quite different:
The problem is: the blue patterns become very blurry.
My question is: How can I save a pdf figure that looks exactly like the plot window?
I solved this problem by specifying the dpi in the savefig for filetype pdf. Even though i read online that dpi is not supposed to make a difference in the vector based pdf format in theory, it did solve the problem for me in practice.
plt.imshow(np.random.random((10,10)))
plt.savefig("test.pdf", dpi=300)
PDF format is a vector image format. This means it is upto the program you open it in to interpret how it should be drawn. This can have some benefits when you want to be able to arbitrarily zoom in and out of an image while keeping high quality. However some programs can modify the image through anti-aliasing.
Your best bet for consistency is to use a pixel based image format. I would suggest try saving it as a .png.

Recommendations for a simple 2D graphics python library that can output to screen and pdf?

I'm looking for an easy-to-use graphics lib for python that can output to screen as well as pdf. So, I would use code to draw some stuff (simple prims like ovals, rectangles, lines and points) to screen and then when things look good, have it output to pdf.
If you use Tkinter, you can draw on a Canvas widget, then use its .postscript method to save the contents as a PostScript file, which you can convert to PDF using ps2pdf.
postscript(self, cnf={}, **kw)
Print the contents of the canvas to a postscript
file. Valid options: colormap, colormode, file, fontmap,
height, pageanchor, pageheight, pagewidth, pagex, pagey,
rotate, witdh, x, y.
Matplotlib should be able to do it. See event handling here: http://matplotlib.sourceforge.net/examples/event_handling/index.html
You can use the Python Imaging Library for drawing images which can easily be displayed in various UIs, e.g. by displaying a jpg. Then, use ReportLab. Here's an example which shows how to use ReportLab with an image.
I'm not sure what you mean by drawing to "screen", i.e. if you're working with a specific UI toolkit. But if it's acceptable to draw and display PDFs without using an intermediate image (jpg, etc), then you might consider the PyX library, which makes it quite simple to do graphics with PDFs.
You could look into matplotlib, which is mainly for plotting but you could probably do some basic drawing.
Then there is pygame. But I'm not so sure if it can generate a pdf, however you can do 2D graphics with it.
There is something called ReportLab that can generate pdf's. Here is a bunch of tutorials using it.
This is a tricky question, because there are so many libraries available - there is a trade-off between beauty/easiness.
What I've done and works great is to produce the Postscript directly, it is not difficult at all, and you can preview it using Ghostview; converting tyo PDF is trivial (ps2pdf). Learning how to tell Postscript to create lines and circles is extremely simple.
If you want more extensibility, then go to Matplotlib, but beware of the many times when it will "decide for you what looks best" even if you don't like it.
Good luck.
Creating PDFs is always a pain, it doesn't make sense if you do not aim to lose sanity.
With that said, you are aiming to do two completely different things: when you draw to screen you draw into a raster bitmap, while PDFs are mostly dynamic, like HTML. (unlike HTML they are more prone to be the same over different platforms, but that's beside the point)
If you really want to do that, the solution might be finding something that outputs PDFs, and then showing the generated PDF on screen at every step.
I guess that's the only way to have WYSIWYG results.

Categories