在 matplotlib 中绘制不带 alpha 的置信区间

问题描述 投票:0回答:1

我想使用

matplotlib
两条曲线和一些置信区间在 Python 中进行绘图。我这样做:

import matplotlib.pyplot as plt
import numpy as np

# Generate fake data.
rng = np.random.default_rng(0)
y1 = np.cumsum(rng.uniform(0, 1, 10))
y2 = np.cumsum(rng.uniform(0, 1.5, 10))

# Plot the fake data and the fake confidence intervals.
fig, ax = plt.subplots()
ax.plot(y1)
ax.fill_between(range(len(y1)), y1 - 1, y1 + 1, alpha=0.2)
ax.plot(y2)
ax.fill_between(range(len(y2)), y2 - 1, y2 + 1, alpha=0.2)
plt.show()

结果如下:

Result of toy example

这是一个玩具示例,我知道这些置信区间在数学上没有任何意义。

我的问题:我需要将图形保存为EPS(我不能使用其他格式),但是

fill_between
使用
alpha=0.2
,并且EPS格式不支持透明度。有什么方法可以在不使用透明度的情况下生成相同的图吗?

python matplotlib eps
1个回答
0
投票

既然不能使用透明度,那么使用轮廓怎么样?

fig, ax = plt.subplots()
l1 = ax.plot(y1)
ax.plot(y1 - 1, ls=':', c=l1[0].get_color())
ax.plot(y1 + 1, ls=':', c=l1[0].get_color())

l2 = ax.plot(y2)
ax.plot(y2 - 1, ls=':', c=l2[0].get_color())
ax.plot(y2 + 1, ls=':', c=l2[0].get_color())

输出:

© www.soinside.com 2019 - 2024. All rights reserved.