Can't make a line non-transparent

Hi, I’m using the LineCollection object to make the line multicolored. But as you can see, for some reason it’s not fully opaque (both parts of the line are visible at a point of intersection). I set alpha to 1.0, but it doesn’t help. The line consists of about 100,000 points.

Thanks in advance for any help or suggestions.

Here’s the code:

x = np.array(x)
y = np.array(y)

# Create a list of line segments
points = np.array([x, y]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)

# Create a LineCollection object with multicolored line segments
# (C is a list of colors that looks like [(0.1, 0.2, 0.3), (0.2, 0.3, 0.4)...]
lc = LineCollection(segments, colors=C, linewidths=25, alpha=1.0)

# Create a figure and axis object
fig, ax = plt.subplots(figsize=(12, 12))

# Set the background color of the plot
fig.set_facecolor('white')

# Add the LineCollection to the axis
ax.add_collection(lc)

# Set the axis limits
x_range = x.max() - x.min()
y_range = y.max() - y.min()
R = 0.05
ax.set_xlim(x.min() - x_range * R, x.max() + x_range * R)
ax.set_ylim(y.min() - y_range * R, y.max() + y_range * R)

ax.set_aspect('equal', 'datalim')
ax = plt.gca()

plt.axis('off')

# Show the plot
plt.show()

Please make a self-contained example that fails. We can’t really debug if we don’t know what is actually in C.

LineCollection draws this as large number of very short lines. If the angle between them is too high there are going to be wedge shaped gaps between them. See
Connect stream lines if no varying width or color by gokberkgunes · Pull Request #25112 · matplotlib/matplotlib · GitHub and the links back to the original issues for a discussion and alternate solutions.

Ok, that makes sense. That’s probably the issue. It seems like the pull request only recently got merged, so seems like I need to wait for a release.

That PR won’t help you unfortunately, it is adding a “fast path” when all the line segments are the same color to use a single line to avoid this. If each of your segments is extremely short and extremely wide and are each a different color you are going to have this artifact given the way we render the line collection.

1 Like