Python matplotlib 两个图之间的交集以获得其中一个的颜色

Python matplotlib intersection between 2 plots to get the colour of only one of them

我在一个 matplotlib 图上用相同的颜色绘制了 10,000 多条线。我使用 alpha=0.1 来提高透明度。

for i in range(len(x)):
    plt.plt(x[i], y[i], color='b', alpha=0.1)

在线条之间的交点处,颜色随着线条的颜色“加起来”而变深。

如何使交点的颜色与一条线相同?

(路口太多,我不想找)

您可以创建 更浅 的颜色来代替使用 alpha 的透明度。您可以按照 this answer 中的说明进行操作:您可以定义一个函数来传递两个参数:

  • 您要使用的颜色名称,例如'blue'
  • 表示颜色明度的数值;
    • 0全黑
    • 1 种自然色('blue' 原样)
    • 2总白

在您的情况下,您可以使用值 1.9 以保持颜色非常浅。

完整代码

import matplotlib.pyplot as plt
import numpy as np


N = 10
x = np.linspace(0, 1, N)


def adjust_lightness(color, amount=0.5):
    import matplotlib.colors as mc
    import colorsys
    try:
        c = mc.cnames[color]
    except:
        c = color
    c = colorsys.rgb_to_hls(*mc.to_rgb(c))
    return colorsys.hls_to_rgb(c[0], max(0, min(1, amount * c[1])), c[2])



fig, ax = plt.subplots()

for i in range(20):
    y = np.random.randn(N)
    ax.plot(x, y, color = adjust_lightness('blue', 1.9), linewidth = 3)

plt.show()