Skip to content

Commit

Permalink
Simplify the LineCollection example
Browse files Browse the repository at this point in the history
Inspired by but independent of matplotlib#27871.

This example is much simpler, but conveys the same LineCollection
aspects - with the exception of not using masked arrays. But I argue
that this first example should be simple and using masked arrays rather
distracts from the core aspects of LineCollection.
  • Loading branch information
timhoffm committed Mar 6, 2024
1 parent 70b1e80 commit a086a26
Showing 1 changed file with 15 additions and 25 deletions.
40 changes: 15 additions & 25 deletions galleries/examples/shapes_and_collections/line_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,33 @@
Plotting multiple lines with a LineCollection
=============================================
Matplotlib can efficiently draw multiple lines at once using a
`~.LineCollection`, as showcased below.
Matplotlib can efficiently draw multiple lines at once using a `~.LineCollection`.
"""

import matplotlib.pyplot as plt
import numpy as np

from matplotlib.collections import LineCollection

x = np.arange(100)
# Here are many sets of y to plot vs. x
ys = x[:50, np.newaxis] + x[np.newaxis, :]
colors = ['violet', 'blue', 'green', 'yellow', 'orange', 'red']

segs = np.zeros((50, 100, 2))
segs[:, :, 1] = ys
segs[:, :, 0] = x
# create a list of half-circles with varying radii
theta = np.linspace(0, np.pi, 36)
radii = np.linspace(4, 5, num=len(colors))
arcs = [np.column_stack([r * np.cos(theta), r * np.sin(theta)]) for r in radii]

# Mask some values to test masked array support:
segs = np.ma.masked_where((segs > 50) & (segs < 60), segs)
fig, ax = plt.subplots(figsize=(6, 3))

# We need to set the plot limits, they will not autoscale
fig, ax = plt.subplots()
ax.set_xlim(x.min(), x.max())
ax.set_ylim(ys.min(), ys.max())
# create a LineCollection with the half-circles
# its properties can be set per line by passing a sequence (here used for *colors*)
# or they can be set for all lines by passing a scalar (here used for *linewidths*)
line_collection = LineCollection(arcs, colors=colors, linewidths=4)
ax.add_collection(line_collection)

# *colors* is sequence of rgba tuples.
# *linestyle* is a string or dash tuple. Legal string values are
# solid|dashed|dashdot|dotted. The dash tuple is (offset, onoffseq) where
# onoffseq is an even length tuple of on and off ink in points. If linestyle
# is omitted, 'solid' is used.
# See `matplotlib.collections.LineCollection` for more information.
colors = plt.rcParams['axes.prop_cycle'].by_key()['color']
# set the axes limits manually because Collections do not take part in autoscaling
ax.set_xlim(-6, 6)
ax.set_ylim(0, 6)

line_segments = LineCollection(segs, linewidths=(0.5, 1, 1.5, 2),
colors=colors, linestyle='solid')
ax.add_collection(line_segments)
ax.set_title('Line collection with masked arrays')
plt.show()

# %%
Expand Down

0 comments on commit a086a26

Please sign in to comment.