python - Matplotlib plots (pcolormesh and colorbar) shift with respect to their axes when using rasterized=True -
i use matplotlib pcolormesh
plots colorbars, apply rasterization plots , colorbars in order reduce file size , save figure pdf file. thereby noticed, after rasterization color-area shifts bit respect axes towards , left, white stripe @ lower , right edge of plot appears. same happens colorbar, found worse: thin colorbars, white stripe obvious , disturbing. there way avoid behaviour of rasterized plots , keep rasterized area @ same place before rasterization?
i tried play around rasterization_zorder
, zorder settings
. helped bit pcolormesh
plots (the lower white stripe disappeared), found no way apply colorbar
.
down there simple example 4 plots demonstrating problem. please zoom in pdf file @ lower right edges of plots see mean.
import numpy np import matplotlib.pyplot 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 , 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 , cbar rasterized", fontsize = 10) '''plot 4, whole axes of main plot , colorbar rasterized, attempt use rasterization_zorder''' ax4 = plt.subplot(224) ax4.set_rasterization_zorder(-10) plot4 = ax4.pcolormesh(d, zorder=-20) '''colorbarbar gets own axis''' 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 , zorder changed", fontsize = 10) plt.savefig("d:/test_rasterization_3plots.pdf", dpi=150) print 'pdf file saved' plt.show()
any suggestions appreciated!
this bug fixed someplace between 1.2.0 , 1.2.1 ( maybe one: https://github.com/matplotlib/matplotlib/issues/1085, leave tracking down commit fixed problem exercise reader;) ).
the simplest solution upgrade 1.2.1 or higher.
Comments
Post a Comment