1

I'm plotting more than 10,000 lines with the same colour on a single matplotlib plot. I use alpha=0.1 for transparency.

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

In the intersections between the lines, the colour becomes darker as the colours of the lines "add up".

How can I make the intersections the same colour of a single line?

(I prefer not to find the intersections as there are so many).

Zephyr
  • 11,891
  • 53
  • 45
  • 80
user287263
  • 41
  • 4

1 Answers1

1

You could create a lighter color in place of using alpha for transparency. You can do that as explained in this answer: you can define a function to which to pass two parameters:

  • the name of the color you want to use, 'blue' for example
  • a value which indicates the lightness of the color;
    • 0 total black
    • 1 natural color ('blue' as is)
    • 2 total white

In your case you can use a value of 1.9 in order to keep the color very light.

Complete Code

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()

enter image description here

Zephyr
  • 11,891
  • 53
  • 45
  • 80