What is the best way to rasterize a plot without blurring tags in matplotlib?

I usually use ax.set_rasterized(True) to rasterize the drawing so that it can handle transparency when saved in eps format, but rasterization also blur labels and label labels, so is there a way to rasterize only patches within and not the whole figure? or is it better to export eps format with transparency? Thanks.

+6
source share
2 answers

As matplotlib, Artists can be rasterized, any class received from Artist ( http://matplotlib.sourceforge.net/api/artist_api.html ) can be rasterized using the rasterized keyword set to True . This way you can rasterize your patches.

I just tried a few combinations and it seemed to work. However, the quality does not seem very good (see also http://www.mail-archive.com/ matplotlib-users@lists.sourceforge.net /msg13276.html ).

 import numpy as np import matplotlib.pyplot as plt def add_patch(ax, **kwargs): if 'rasterized' in kwargs and kwargs['rasterized']: ax.set_rasterization_zorder(0) ax.fill_between(np.arange(1, 10), 1, 2, zorder=-1, **kwargs) ax.set_xlim(0, 10) ax.set_ylim(0, 3) if 'alpha' in kwargs and kwargs['alpha'] < 1: txt = 'This patch is transparent!' else: txt = 'This patch is not transparent!' ax.text(5, 1.5, txt, ha='center', va='center', fontsize=25, zorder=-2, rasterized=True) fig, axes = plt.subplots(nrows=4, sharex=True) add_patch(axes[0], alpha=0.2, rasterized=False) add_patch(axes[1], alpha=0.2, rasterized=True) add_patch(axes[2], rasterized=False) add_patch(axes[3], rasterized=True) plt.tight_layout() plt.savefig('rasterized_transparency.eps') 

I converted eps to png to show it in a browser:

rasterized_transparency.png

See also: How to save numbers in pdf as bitmaps in matplotlib .

+7
source

The results are better if you specify dpi - by default, this default value is pretty low. For example, change the last line to

 plt.savefig('rasterized_transparency.eps',dpi=200) 

and the file grows to 4.5M and looks great in Acrobat with an increase of up to 200%. However, I agree that there are probably more compact formats that support transparency.

+2
source

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


All Articles