如何在不模糊matplotlib中的标签的情况下更好地栅格化图? [英] How to better rasterize a plot without blurring the labels in matplotlib?

查看:44
本文介绍了如何在不模糊matplotlib中的标签的情况下更好地栅格化图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常使用 ax.set_rasterized(True) 来光栅化图形,以便在以 eps 格式保存时它可以处理透明度,但光栅化也会模糊轴标签和刻度标签,那么有没有办法只栅格化轴内的补丁而不是整个图形?还是可以更好地导出具有透明度的eps格式?谢谢.

解决方案

As matplotlib .

I usually use ax.set_rasterized(True) to rasterize the figure so that it could handle transparency when saved as in eps format, but the rasterization also blurs the axis labels and ticklabels, so is there a way to rasterize only patches within the axis rather than the whole figure? or is there a better around for exporting eps format with transparency? Thanks.

解决方案

As matplotlib Artists can be rasterized, any class derived from Artist (http://matplotlib.sourceforge.net/api/artist_api.html) can be rasterized with the keyword rasterized set to True. So you can only rasterize your patches.

I just tried some combinations and it seems to work. However the quality seems to be not 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 the eps to png to show it in the browser:

See also: How to save figures to pdf as raster images in matplotlib.

这篇关于如何在不模糊matplotlib中的标签的情况下更好地栅格化图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆