2017-06-01 80 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