Note

Go to the endto download the full example code.

Rasterization for vector graphics#

Rasterization converts vector graphics into a raster image (pixels). It canspeed up rendering and produce smaller files for large data sets, but comesat the cost of a fixed resolution.

Whether rasterization should be used can be specified per artist. This can beuseful to reduce the file size of large artists, while maintaining theadvantages of vector graphics for other artists such as the Axesand text. For instance a complicatedpcolormesh orcontourf can be made significantly simpler by rasterizing.Setting rasterization only affects vector backends such as PDF, SVG, or PS.

Rasterization is disabled by default. There are two ways to enable it, whichcan also be combined:

The storage size and the resolution of the rasterized artist is determined byits physical size and the value of thedpi parameter passed tosavefig.

Note

The image of this example shown in the HTML documentation is not a vectorgraphic. Therefore, it cannot illustrate the rasterization effect. Pleaserun this example locally and check the generated graphics files.

importmatplotlib.pyplotaspltimportnumpyasnpd=np.arange(100).reshape(10,10)# the values to be color-mappedx,y=np.meshgrid(np.arange(11),np.arange(11))theta=0.25*np.pixx=x*np.cos(theta)-y*np.sin(theta)# rotate x by -thetayy=x*np.sin(theta)+y*np.cos(theta)# rotate y by -thetafig,((ax1,ax2),(ax3,ax4))=plt.subplots(2,2,layout="constrained")# pcolormesh without rasterizationax1.set_aspect(1)ax1.pcolormesh(xx,yy,d)ax1.set_title("No Rasterization")# pcolormesh with rasterization; enabled by keyword argumentax2.set_aspect(1)ax2.set_title("Rasterization")ax2.pcolormesh(xx,yy,d,rasterized=True)# pcolormesh with an overlaid text without rasterizationax3.set_aspect(1)ax3.pcolormesh(xx,yy,d)ax3.text(0.5,0.5,"Text",alpha=0.2,va="center",ha="center",size=50,transform=ax3.transAxes)ax3.set_title("No Rasterization")# pcolormesh with an overlaid text without rasterization; enabled by zorder.# Setting the rasterization zorder threshold to 0 and a negative zorder on the# pcolormesh rasterizes it. All artists have a non-negative zorder by default,# so they (e.g. the text here) are not affected.ax4.set_aspect(1)m=ax4.pcolormesh(xx,yy,d,zorder=-10)ax4.text(0.5,0.5,"Text",alpha=0.2,va="center",ha="center",size=50,transform=ax4.transAxes)ax4.set_rasterization_zorder(0)ax4.set_title("Rasterization z$<-10$")# Save files in pdf and eps formatplt.savefig("test_rasterization.pdf",dpi=150)plt.savefig("test_rasterization.eps",dpi=150)ifnotplt.rcParams["text.usetex"]:plt.savefig("test_rasterization.svg",dpi=150)# svg backend currently ignores the dpi
No Rasterization, Rasterization, No Rasterization, Rasterization z$<-10$
The PostScript backend does not support transparency; partially transparent artists will be rendered opaque.

References

The use of the following functions, methods, classes and modules is shownin this example:

Total running time of the script: (0 minutes 2.779 seconds)

Gallery generated by Sphinx-Gallery