wpkzz February 2016

How to eliminate smoothing in matplotlib (pyplot)?

I am doing some colormap plot using the Julia implementation of PyPlot. I want the figure to be without any interpolation or smooting, and I want it to be saved as svg so it can be manipulated afterwards by other people. In the Jupyter notebook it appears as I want it to appear, and if I save it as png it gets saved as I want. But if I save it as svg, the colormap gets smoothed, which is plainly wrong for my data. The code is:

figure(figsize=(7.0, 5.5))
PyPlot.xlim(1,64)
PyPlot.ylim(1,64)
otraimagen=imshow(EjemploModificado, cmap="bwr", interpolation="none", origin="lower", vmin=-limites, vmax=limites, extent=[1,64,1,64])
use_gridspace='True')
use_gridspec="True",fraction=0.045 )
tick_params(axis="both", labelsize=14)   
xlabel("electrode number", fontsize=14)
ylabel("electrode number", fontsize=14)
savefig("CSD-Neg-Pos-$t.png", dpi=90) 
PyPlot.close_queued_figs()

The images produced are, as png: enter image description here

and as svg (converted afterwards to png so I could uplotad it here): enter image description here

Answers


Joe Kington February 2016

For what it's worth, in addition to the SVG-specific issue that @FelipeLema mentioned, there's another point that you should be aware of.

There is a difference between interpolation='none' and interpolation='nearest'. It mostly applies to vector formats, where you'll be using a viewer to produce a rasterized version that's displayed on-screen (e.g. PDF, SVG).

There's a nice visual summary in the matplotlib example here: http://matplotlib.org/examples/images_contours_and_fields/interpolation_none_vs_nearest.html In this case, we'd be talking mostly about the "small image blown up" portion of the example.

interpolation='none' tells the viewer that there's no specific interpolation type required. For raster images produced by matplotlib, it will be nearest interpolation. For some PDF viewers this is nearest, and for others it's bilinear or another smooth interpolator.

However, interpolation='nearest' specifies that you specifically want nearest-neighbor interpolation.

I don't recall offhand whether SVG viewers behave in a similar way as PDF viewers in this specific regard, but even with the bug that @FelipeLema mentioned fixed, you may not see nearest-neighbor interpolation in all SVG viewers with interpolation='none'.

Post Status

Asked in February 2016
Viewed 3,004 times
Voted 6
Answered 1 times

Search




Leave an answer