python - seaborn 中的非透明置信区间

标签 python matplotlib seaborn eps

上下文:
我想投稿的期刊只接受 .tiff(不适用于 LaTeX)、.jpg(不适用于图表)和 。 eps(这不适用于 alpha 透明度,除非我将图像栅格化,这会导致文件很大)。我的许多绘图都使用 seaborn 的 regplot,它绘制了透明的置信区间。是否可以在不完全手动重新绘制所有图形的情况下绘制非透明 CI(例如,背景中的虚线或纯色)?

示例:

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 文件的最简单/最佳方法是什么?

最佳答案

问题是您需要透明度来显示重叠的两个置信区间。需要对图像进行光栅化。

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

控制图像的质量
plt.savefig(__file__+".jpg", quality=95)

也可以使用 eps,在这里,您可以只对置信区间 fill_between 曲线进行栅格化,而不是对所有内容进行栅格化。优点是轴、标签和点仍然是矢量图形,不会在不同的缩放级别上看起来像素化。

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

虽然文件大小当然有点大,但我不确定这是否是一个真正的问题。

关于python - seaborn 中的非透明置信区间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44303349/

相关文章:

python - X 轴上日历周 YYYYWW 的 Seaborn 多线图

python - Django 与 python 2.7 utf-8 问题

python-2.7 - Python 条形图中 error_kw 的关键字值

python - 时间序列的简单 tsplot

python - 使用 Matplotlib 绘制椭圆体

python - 了解 Matplotlib 的箭袋绘图

python - residplot seaborn 中的参数应该是什么

python - 尝试仅获取亚马逊搜索的首页结果

python - Windows 10 Ubuntu 外壳 "CUDA driver version is insufficient"

python - python中的StopIteration错误