Matplotlib graphs (pcolormesh and colorbar) shift relative to their axes when using rasterized = True

I use matplotlib pcolormesh graphics with colorbars, apply rasterization to graphs and color panels to reduce file size and save the shape as a PDF file. Thus, I noticed that after rasterization, the color region itself is slightly shifted up and down relative to the axes, so that a white bar appears at the bottom and right edges of the graph. The same thing happens with the color panel, which I found even worse: with thin colored stripes, the white strip is very obvious and interferes. Is there a way to avoid this behavior of rasterized graphs and keep the rasterized area in the same place as before rasterization?

I tried to play with rasterization_zorder and zorder settings . This helped a bit with the pcolormesh graphs (the bottom white bar disappeared), but I did not find a way to apply it to the colorbar .

Down there is a simple example with four graphs showing the problem. Please zoom in on the PDF file in the lower right corner of the plots to see what I mean.

  import numpy as np import matplotlib.pyplot as plt d = np.arange(100).reshape(10, 10) myfig = plt.figure(figsize=(5, 5)) '''plot 1, no rasterization''' ax1 = plt.subplot(221) plot1 = ax1.pcolormesh(d) cbar1 = plt.colorbar(plot1) ax1.set_title("no rasterization", fontsize = 10) '''plot 2, main plot rasterized, colorbar not''' ax2 = plt.subplot(222) plot2 = ax2.pcolormesh(d, rasterized=True) cbar2 = plt.colorbar(plot2) ax2.set_title("plot rasterized", fontsize = 10) '''plot 3, main plot and colorbar rasterized''' ax3 = plt.subplot(223) plot3 = ax3.pcolormesh(d, rasterized=True) cbar3 = plt.colorbar(plot3) cbar3.solids.set_rasterized(True) # !!!!!!!! ax3.set_title("plot and cbar rasterized", fontsize = 10) '''plot 4, whole axes of main plot and colorbar rasterized, attempt to use rasterization_zorder''' ax4 = plt.subplot(224) ax4.set_rasterization_zorder(-10) plot4 = ax4.pcolormesh(d, zorder=-20) '''colorbarbar gets its own axis''' from mpl_toolkits.axes_grid1.inset_locator import inset_axes ax_cbar4 = inset_axes(ax4, width="3%", height="100%", loc=6) ax_cbar4.set_rasterization_zorder(-10) locator_ax_cbar4 =ax_cbar4.get_axes_locator() locator_ax_cbar4.set_bbox_to_anchor ((1.0, 0 , 1, 1), ax4.transAxes) cbar4=plt.colorbar(plot4, cax=ax_cbar4) #cbar4.solids.set_rasterization_zorder(-10) # ---> NOT WORKING cbar4.solids.set_rasterized(True) ax4.set_title("axes rasterized and zorder changed", fontsize = 10) plt.savefig("D:/test_rasterization_3plots.pdf", dpi=150) print 'pdf file saved' plt.show() 

example

Any suggestions would be appreciated!

+4
source share
1 answer

This is a bug that was fixed somewhere between 1.2.0 and 1.2.1 (maybe this one: https://github.com/matplotlib/matplotlib/issues/1085 , I leave the commit tracking that fixed the problem as an exercise for the reader;)).

The simplest solution is up to 1.2.1 or higher.

+7
source

Source: https://habr.com/ru/post/1479906/


All Articles