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)

Any suggestions would be appreciated!
source share