2017-06-01 78 views
1

语境:
该杂志我要提交我的论文只接受.tiff(不LaTeX的工作),.jpg(不适合图表),以及.eps(其没有按除非我对图像进行光栅化处理,否则会导致巨大的文件大小)。我的许多地块使用seaborn的regplot,绘制了透明置信区间。是否可以绘制不透明的配置项而不必手动完成所有图形的重新配置(例如在背景中用虚线或纯色)?非透明的置信区间

例子:

import numpy as np 
import matplotlib.pyplot as plt 
import seaborn as sns 

sns.set_style("ticks") 
np.random.seed(0) 
n = 50 

fig, ax = plt.subplots(figsize=(8,6)) 

x = np.random.randn(n) 
y1 = np.random.randn(n) 
y2 = np.random.randn(n) 

sns.regplot(x, y1, ax=ax) 
sns.regplot(x, y2, ax=ax) 

plt.show() 

Example of a regplot with transparent overlapping confidence intervals

什么将它保存为一个文件.EPS不从重叠的置信区间丢失信息的最简单/最好的方法是什么?

回答

1

问题是,您需要透明度来显示两个置信区间重叠。人们需要光栅化图像。

如果期刊接受它,我实际上并没有看到使用jpg的问题。您可以使用

plt.savefig(__file__+".jpg", quality=95) 

使用EPS也有可能,在这里控制图像质量,而不是光栅化的一切,你可能只光栅化置信区间fill_between -curves。优点是轴,标签和点仍然是vecor图形,不会在不同的缩放级别上看起来像素化。

import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib.collections import PolyCollection as p 
import seaborn as sns 

sns.set_style("ticks") 
np.random.seed(0) 
n = 50 

fig, ax = plt.subplots(figsize=(8,6)) 

x = np.random.randn(n) 
y1 = np.random.randn(n) 
y2 = np.random.randn(n) 

sns.regplot(x, y1, ax=ax) 
sns.regplot(x, y2, ax=ax) 

plt.savefig(__file__+".jpg", quality=95) 
for c in ax.findobj(p): 
    c.set_zorder(-1) 
    c.set_rasterized(True) 
#everything on zorder -1 or lower will be rasterized 
ax.set_rasterization_zorder(0) 

plt.savefig(__file__+".eps") 
plt.savefig(__file__+".png") 
plt.show() 

最终的EPS文件看起来是这样的:
enter image description here

虽然文件大小当然是大一点的,我不知道这是否是一个真正的问题。

+2

千万不要使用jpg作图! – mwaskom

+0

哦,我不知道你只能光栅化图像的一部分。这非常整齐。谢谢!该杂志对文件大小非常严格,这就是为什么我不想使用大多数未压缩的JPG格式。对于最初的提交,我使用了一种解决方法,用虚线替换了seaborn源代码中的'fill_between'部分,但是您的解决方案看起来更优雅。 –

+1

PS你为什么要比较类型的字符串repr而不是使用'isinstance'?此外,我认为matplotlib轴有一个函数返回给定类型的所有艺术家,但我忘记了它的名称。 – mwaskom