I am generating a large PDF with numerous pages. I trying to rasterize everything I can within it to save some disk space. However, the color bars are particularly tough to deal with.
Can someone manage to rasterize the color bar axis spines in the example below?
import matplotlib.pyplot as plt
import numpy as np
ax = plt.subplot(111)
im = ax.imshow(np.arange(100).reshape((10, 10)))
cb = plt.colorbar(im)
ax.set_rasterized(True)
plt.suptitle('Title')
plt.savefig('test.pdf')
As it is now, when zooming in, the axes of the image look pixelated but not the axes of the color bar. I want both to be pixelated.
I thought im.colorbar.solids.set_rasterized(True)
would make the trick but it did not. Saving the image as a PNG file is not an option since I want to keep the figure title not rasterized (this is the only thing I do not want rasterized)
It turns out it is necessary to access the axis of the color bar directly. The for loop below makes the trick
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure() # <--- New line
ax = plt.subplot(111)
im = ax.imshow(np.arange(100).reshape((10, 10)))
cb = plt.colorbar(im)
for ax in fig.get_axes(): # <--- New line
ax.set_rasterized(True) # <--- New line
plt.suptitle('Title')
plt.savefig('test.pdf')