Matplotlib:曲线重叠时如何防止透明的颜色重叠?

时间:2018-06-19 14:23:12

标签: python matplotlib plot alpha alpha-transparency

例如,我们在此处绘制具有透明颜色的线

import numpy as np
import matplotlib.pyplot as plt

a = np.array([1, 2, 3, 4, 5])
b = 2*a
plt.plot(a, b, 'blue', alpha=0.3)
plt.show()

enter image description here

但是我要绘制同一条线多次,这些线会与自身重叠,因此与自身重叠的次数越多,其颜色就越深。

import numpy as np
import matplotlib.pyplot as plt


a = np.array([1, 2, 3, 4, 5])
b = 2*a
for i in range(3):
    plt.plot(a, b, 'blue', alpha=0.3)
plt.show()

enter image description here

那么我该如何防止颜色重叠?

谢谢大家!

更新:为什么我需要这个?

我正在做公差分析。这意味着,参数会在samll范围内自行更改,我将为每次更改绘制曲线。然后我可以找到最坏的情况。

enter image description here

如果我选择纯色但较浅的颜色。它看起来像:

enter image description here

如您所见,颜色不透明,我无法观察到被其他行覆盖的节点。

更新2:

enter image description here

1 个答案:

答案 0 :(得分:1)

单行不重叠。因此,您可以将多个图连接成一个图。

import numpy as np
import matplotlib.pyplot as plt


a = np.array([1, 2, 3, 4, 5])
b = 2*a

A = np.tile(np.append(a,[np.nan]),3)
B = np.tile(np.append(b,[np.nan]),3)

plt.plot(A, B, 'blue', alpha=0.3)
plt.show()

enter image description here

从本质上讲,这是问题How can I draw transparent lines where the color becomes stronger when they overlap?的反面,在这种情况下,这种影响是不希望的。