Plot multiple lines using a LineCollection
Matplotlib can efficiently draw multiple lines at once using a LineCollection.
Matplotlib can efficiently draw multiple lines at once using a LineCollection.

Instead of passing a list of colors (colors=colors), we can alternatively use
colormapping. The lines are then color-coded based on an additional array of values
passed to the array parameter. In the below example, we color the lines based on
their radius by passing array=radii.
num_arcs = 15
theta = np.linspace(0, np.pi, 36)
radii = np.linspace(4, 5.5, num=num_arcs)
arcs = [np.column_stack([r * np.cos(theta), r * np.sin(theta)]) for r in radii]
fig, ax = plt.subplots(figsize=(6.4, 3))
# set axes limits manually because Collections do not take part in autoscaling
ax.set_xlim(-6, 6)
ax.set_ylim(0, 6)
ax.set_aspect("equal") # to make the arcs look circular
# create a LineCollection with the half-circles and color mapping
line_collection = LineCollection(arcs, array=radii, cmap="rainbow")
ax.add_collection(line_collection)
fig.colorbar(line_collection, label="Radius")
ax.set_title("Line Collection with mapped colors")
plt.show()

Total running time of the script: (0 minutes 1.067 seconds)