matplotlib fill_between facecolor alpha与edgecolor alpha之间?

时间:2018-07-24 00:20:47

标签: python matplotlib

是否可以为facecoloredgecolor指定不同的字母?还是有一种方法可以绘制在图例中也可用的具有非alpha边缘颜色的alpha填充区域?

这不是我想要的... enter image description here

axs.fill_between(xvalues, tupper_w, tlower_w, facecolor='dimgray', edgecolor='dimgray', alpha=0.25, label='$measured\quad\sigma$')
axs.fill_between(xvalues, pupper_w, plower_w, facecolor='orange', edgecolor='orange', alpha=0.25, label='$predicted\quad\sigma$')
axs.plot(xvalues, tcurvesavg_w, color='dimgray', label='$\overline{measured}$', ls='--')
axs.plot(xvalues, pcurvesavg_w, color='orange', label='$\overline{predicted}$', ls='--')


这就是我想要的(但带有正确的图例): enter image description here

axs.fill_between(xvalues, tupper, tlower, facecolor='dimgray', alpha=0.25, label='$measured\quad\sigma$')
axs.fill_between(xvalues, pupper, plower, facecolor='orange', alpha=0.25, label='$predicted\quad\sigma$')
axs.plot(xvalues, tupper, color='dimgray', lw=0.5)
axs.plot(xvalues, tcurvesavg, color='dimgray', label='$\overline{measured}$', ls='--')
axs.plot(xvalues, tlower, color='dimgray', lw=0.5)
axs.plot(xvalues, pupper, color='orange', lw=0.5)
axs.plot(xvalues, pcurvesavg, color='orange', label='$\overline{predicted}$', ls='--')
axs.plot(xvalues, plower, color='orange', lw=0.5)

2 个答案:

答案 0 :(得分:1)

立即查看fill_between alpha和常规fill_between documentation,它似乎不受支持。 legend documentation似乎没有提供绘制任何一个边界后的选项。

在第二个代码段中,如果您可以弄清楚如何使绘图和填充函数具有单个句柄,则图例应自动格式化。类似于以下内容(改编自this similar, but not quite duplicate StackExchangePost):

import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
import numpy as np

xvalues = np.linspace(0,1,11)
tcurvesavg = np.linspace(0,1,11)
p1, = plt.plot(xvalues, tcurvesavg , c='r')  # notice the comma!
p2 = plt.fill_between(xvalues, tcurvesavg -0.2, tcurvesavg +0.2, color='r', alpha=0.5)
plt.legend(((p1,p2),), ('Entry',))
plt.show()

(作为大多数matplotlib问题的非自动解决方法,另存为svg(类似于this post),并在Inkscape之类的矢量图形程序中添加边框。您不应该失去分辨率,并且仍然可以放入报告等)

答案 1 :(得分:0)

您不能通过alpha参数指定不同的alpha值。不过,您可以使用一个Alpha通道(例如,facecoloredgecolor来定义每个{用于不透明度为40%的红色

facecolor=(1,0,0,.4)

然后将其直接应用于图例中。

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0.0, 2, 0.01)
y1 = np.sin(2*np.pi*x)
y2 = 1.2*np.sin(4*np.pi*x)-.9

fig, ax = plt.subplots()

ax.fill_between(x, y1, y1+.5, facecolor=(1,0,0,.4), edgecolor=(0,0,0,.5), label="Label 1")
ax.fill_between(x, y2, y2-.5, facecolor=(0,0,1,.4), edgecolor=(0,0,0,.5), label="Label 1")

ax.legend()
plt.show()

enter image description here