Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python plotting: How can I make matplotlib.pyplot stop forcing the style of my markers?

I am trying to plot a bunch of data points (many thousands) in Python using matplotlib so I need each marker to be very small and precise. How do I get the smallest most simple marker possible? I use this command to plot my data:

 matplotlib.pyplot( x , y ,'.',markersize=0.1,linewidth=None,markerfacecolor='black')

Then I can look at it either with pl.show() and then save it. Or directly use plt.savefig('filename.ps') in the code to save it. The problem is this: when I use pl.show() to view the file in the GUI it looks great with small tiny black marks, however when I save from the show() GUI to a file or use directly savefig and then view the ps I created it looks different! Each marker has gained a little blue halo around it (as if it started at each point to connect them with the default blue lines, but did not) and the style is all wrong. Why does it change the style when saved? How do I stop python from forcing the style of the markers? And yes I have looked at some alternative packages like CairoPlot, but I want to keep using matplotlib for now.

Update: It turns out that the save to PNG first makes the colors turn out okay, but it forces a conversion of the image when I want to save it again as a .ps later (for inclusion in a PDF) and then I lose quality. How do I preserve the vector nature of the file and get the right formatting?

like image 259
Alex Avatar asked Feb 13 '09 02:02

Alex


People also ask

How do I get rid of Matplotlib scientific notation?

If you want to disable both the offset and scientific notaion, you'd use ax. ticklabel_format(useOffset=False, style='plain') .

How do I change the graph style in Matplotlib?

Another way to change the visual appearance of plots is to set the rcParams in a so-called style sheet and import that style sheet with matplotlib. style. use . In this way you can switch easily between different styles by simply changing the imported style sheet.

What is the default Matplotlib style?

The default font has changed from "Bitstream Vera Sans" to "DejaVu Sans".


3 Answers

For nice-looking vectorized output, don't use the '.' marker style. Use e.g. 'o' (circle) or 's' (square) (see help(plot) for the options) and set the markersize keyword argument to something suitably small, e.g.:

plot(x, y, 'ko', markersize=2)
savefig('foo.ps')

That '.' (point) produces less nice results could be construed as a bug in matplotlib, but then, what should "point" mean in a vector graphic format?

like image 52
Jouni K. Seppänen Avatar answered Nov 03 '22 07:11

Jouni K. Seppänen


Have you tried the ',' point shape? It creates "pixels" (small dots, instead of shapes).

You can play with the markersize option as well, with this shape?

like image 29
Eric O Lebigot Avatar answered Nov 03 '22 09:11

Eric O Lebigot


If you haven't, you should try saving in a rasterizing engine -- save it to a PNG file and see if that fixes it. If you need a vector plot, try saving to PDF and converting with an external utility. I've also had problems before with the PS engine that were resolved by saving with the Agg or PDF engines and converting externally.

like image 40
Matt Avatar answered Nov 03 '22 07:11

Matt