How to draw transparent lines where the color gets stronger when they overlap?

When you draw a bunch of transparent lines in matplotlib like this, you get a nice effect; when they overlap, they are a little darker.

from pylab import * for _ in xrange(1000) : plot(np.random.randn(2),np.random.randn(2),alpha=0.1,color='k') show() 

It looks like this:

many intersecting lines

But if you draw one long line, like this one, which overlays itself in this way, the line does not "interact with itself." It looks like this:

one line overlapping itself many times

I would like to draw one curve that overlaps itself, so the more it overlays itself, the darker it gets. If I use a loop to break a curve and draw each individual segment separately, I get what I want, but I also get ugly and unacceptable artifacts where line segments meet, making the curve look like a dashed or dashed line. this is:

the best effort so far

Is there a good way to draw a curve so that it gets darker when it overlaps with itself, but you don't get artifacts as just described?

+5
source share
1 answer

When using a loop to break a curve and select each line segment separately, you can try using the solid_capstyle argument for plot . Projecting is used by default, but you can try using "butt" and see if it helps.

 plt.plot(x,y, alpha=0.1, c="k", solid_capstyle="butt") 

This may slightly reduce the effect.

 import matplotlib.pyplot as plt import numpy as np def squiggle_xy(a, b, c, d, i=np.arange(0.0, 2*np.pi, 0.05)): return np.sin(i*a)*np.cos(i*b), np.sin(i*c)*np.cos(i*d) x,y = squiggle_xy(2.5, 2, 1, 3) fig, ax = plt.subplots(ncols=2, figsize=(6,3)) ax[0].set_title("solid_capstyle=\"projecting\"") ax[1].set_title("solid_capstyle=\"butt\"") for i in range(len(x)-1): print x[i:i+2] ax[0].plot(x[i:i+2], y[i:i+2], alpha=0.1, lw=10, solid_capstyle="projecting", c="b") ax[1].plot(x[i:i+2], y[i:i+2], alpha=0.1, lw=10, solid_capstyle="butt", c="b") plt.show() 

enter image description here

See this question for a good explanation of solid_capstyle .

+3
source

Source: https://habr.com/ru/post/1263182/


All Articles