我想在 Matlab 中绘制一个带有阴影区域的函数,表明它的不确定性(例如,置信区间)。这可以通过使用fill创建色块的功能来实现。例如
x = linspace(0, 2*pi, 100);
f = cos(x);
fUp = cos(x) + 1;
fLow = cos(x) - 1;
x2 = [x, fliplr(x)];
plot(x, f, 'k')
hold on
fill(x2, [f, fliplr(fUp)], 0.7 * ones(1, 3), 'linestyle', 'none', 'facealpha', 0.4);
fill(x2, [fLow, fliplr(f)], 0.7 * ones(1, 3), 'linestyle', 'none', 'facealpha', 0.4);
Run Code Online (Sandbox Code Playgroud)
这会在函数fLow和之间创建一个灰色阴影区域fUp,f中间用黑色实线表示,如下图所示。
我现在希望当我们接近置信区间的下限(或上限)时,阴影区域会降低其颜色。特别是,当接近它的边界时,我希望阴影区域变得越来越亮。有没有办法做到这一点?我正在做两个单独的补丁,因为我认为这可能是我的目的所必需的。
在 Python 中,迭代列表时无法修改列表。例如,下面我无法修改list_1,并且结果print将是[0, 1, 2, 3, 4]。在第二种情况下,我循环遍历类实例列表,并调用该set_n方法修改列表中的实例,同时迭代它。确实,意志print给予[4, 4, 4, 4, 4]。
这两个案例有何不同,为什么?
# First case: modify list of integers
list_1 = [0, 1, 2, 3, 4]
for l in list_1:
l += 1
print(list_1)
# Second case: modify list of class instances
class Foo:
def __init__(self, n):
self.n = n
def set_n(self, n):
self.n = n
list_2 = [Foo(3)] * 5
for l in list_2:
l.set_n(4)
print([l.n for …Run Code Online (Sandbox Code Playgroud)