绘图保存为 jpeg 时的像素化字体 [英] Pixelated fonts when plot is saved as jpeg

查看:60
本文介绍了绘图保存为 jpeg 时的像素化字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将 matplotlib 图形保存为 jpeg 时,刻度字体会像素化.我不确定发生了什么,或者是否有任何破解方法可以解决此问题.有没有人有任何见解?

When I save a matplotlib figure as a jpeg the tick fonts are pixelated. I'm not sure what is going on or if there is any hack to fix this. Does anyone have any insight?

%matplotlib nbagg

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-1.2,1.2,1000,endpoint=True)
y = np.copy(x)

x,y = np.meshgrid(x,y)
z   = -x**2 + y**2 - y**3

fig = plt.figure()

ax  = fig.add_subplot(111)

CS = plt.contour(x,y,z, [0,-0.1,0.1], colors=['black','blue', 'gray'])
plt.clabel(CS, fontsize=14, inline=1, fmt='%1.1f', manual=[(-0.15,0), (-0.4,0), (0.25,0.5)])

plt.savefig('plot.png', format='png')
plt.savefig('plot.jpg', format='jpg')
plt.savefig('plot.tiff', format='tiff')

这里是 plot.png:

Here is plot.png:

这是plot.jpg:

Here is plot.jpg:

这是 plot.tiff:

Here is the plot.tiff:

我相信这与我之前的一个问题有关:动画中的抗锯齿字体

I believe this is related to a previous question I had: Anti-aliased Fonts in Animations

推荐答案

如上所述,这种情况的出现取决于所使用的后端.您可以使用以下方法避免该问题:

As noted above, this situation appears is dependent on the backend used. You can avoid the issue by using:

import matplotlib
matplotlib.use('webagg')

相对于:

%matplotlib nbagg

我认为问题与PIL试图保存具有透明性的图形的jpeg有关.如果你坚持使用nbagg,看来如果你设置了:

I believe the issue has to do with PIL trying to save a jpeg of a figure with transparency. If you insist on using nbagg, it appears that if you set:

matplotlib.rcParams['nbagg.transparent'] = False

您的 jpeg 图像字体不会被像素化,并且看起来与问题中显示的 png 和 tiff 文件几乎相同.不幸的是使用 rcParams:

Your jpeg image fonts won't be pixelated and will look nearly identical to the png and tiff files shown in the question. Unfortunately using the rcParams:

matplotlib.rcParams['savefig.transparent'] = False

是不够的.似乎savefig.transparent"rcParam 将控制图形内部绘图的透明度,而nbagg.transparent"将控制图形外部的透明度(即:轴、刻度、标题等).在保存为不支持透明度的文件格式时,通过确保后端强制透明 = False 可能是一项简单的工作.

is not sufficient. It appears that the 'savefig.transparent' rcParam will control the transparency of the plot inside the figure and the 'nbagg.transparent' will control the transparency outside of the figure (ie: axis, ticks, titles, etc..). There is probably an easy work by ensuring the backend forces transparency = False for when saving to file formats that don't support transparency.

其他一些后端可能不支持透明性,这就是为什么当您更改后端时似乎可以解决问题的原因.

Some of the other backends may not support transparency which is why it appears to fix the problem when you change backends.

我会将此作为错误报告给 github.

I will report this to github as a bug.

这篇关于绘图保存为 jpeg 时的像素化字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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