|
1 | 1 | """
|
2 |
| -=============== |
3 |
| -Line Collection |
4 |
| -=============== |
| 2 | +============================================= |
| 3 | +Plotting multiple lines with a LineCollection |
| 4 | +============================================= |
5 | 5 |
|
6 |
| -Plotting lines with Matplotlib. |
7 |
| -
|
8 |
| -`~matplotlib.collections.LineCollection` allows one to plot multiple |
9 |
| -lines on a figure. Below we show off some of its properties. |
| 6 | +Matplotlib can efficiently draw multiple lines at once using a |
| 7 | +`~.LineCollection`, as showcased below. |
10 | 8 | """
|
11 | 9 |
|
12 | 10 | import matplotlib.pyplot as plt
|
13 | 11 | from matplotlib.collections import LineCollection
|
14 |
| -from matplotlib import colors as mcolors |
15 | 12 |
|
16 | 13 | import numpy as np
|
17 | 14 |
|
18 |
| -# In order to efficiently plot many lines in a single set of axes, |
19 |
| -# Matplotlib has the ability to add the lines all at once. Here is a |
20 |
| -# simple example showing how it is done. |
21 | 15 |
|
22 | 16 | x = np.arange(100)
|
23 | 17 | # Here are many sets of y to plot vs. x
|
|
30 | 24 | # Mask some values to test masked array support:
|
31 | 25 | segs = np.ma.masked_where((segs > 50) & (segs < 60), segs)
|
32 | 26 |
|
33 |
| -# We need to set the plot limits. |
| 27 | +# We need to set the plot limits, they will not autoscale |
34 | 28 | fig, ax = plt.subplots()
|
35 | 29 | ax.set_xlim(x.min(), x.max())
|
36 | 30 | ax.set_ylim(ys.min(), ys.max())
|
|
41 | 35 | # onoffseq is an even length tuple of on and off ink in points. If linestyle
|
42 | 36 | # is omitted, 'solid' is used.
|
43 | 37 | # See `matplotlib.collections.LineCollection` for more information.
|
44 |
| -colors = [mcolors.to_rgba(c) |
45 |
| - for c in plt.rcParams['axes.prop_cycle'].by_key()['color']] |
| 38 | +colors = plt.rcParams['axes.prop_cycle'].by_key()['color'] |
46 | 39 |
|
47 | 40 | line_segments = LineCollection(segs, linewidths=(0.5, 1, 1.5, 2),
|
48 | 41 | colors=colors, linestyle='solid')
|
|
51 | 44 | plt.show()
|
52 | 45 |
|
53 | 46 | ###############################################################################
|
54 |
| -# In order to efficiently plot many lines in a single set of axes, |
55 |
| -# Matplotlib has the ability to add the lines all at once. Here is a |
56 |
| -# simple example showing how it is done. |
| 47 | +# In the following example, instead of passing a list of colors |
| 48 | +# (``colors=colors``), we pass an array of values (``array=x``) that get |
| 49 | +# colormapped. |
57 | 50 |
|
58 | 51 | N = 50
|
59 | 52 | x = np.arange(N)
|
60 |
| -# Here are many sets of y to plot vs. x |
61 |
| -ys = [x + i for i in x] |
| 53 | +ys = [x + i for i in x] # Many sets of y to plot vs. x |
| 54 | +segs = [np.column_stack([x, y]) for y in ys] |
62 | 55 |
|
63 |
| -# We need to set the plot limits, they will not autoscale |
64 | 56 | fig, ax = plt.subplots()
|
65 | 57 | ax.set_xlim(np.min(x), np.max(x))
|
66 | 58 | ax.set_ylim(np.min(ys), np.max(ys))
|
67 | 59 |
|
68 |
| -# colors is sequence of rgba tuples |
69 |
| -# linestyle is a string or dash tuple. Legal string values are |
70 |
| -# solid|dashed|dashdot|dotted. The dash tuple is (offset, onoffseq) |
71 |
| -# where onoffseq is an even length tuple of on and off ink in points. |
72 |
| -# If linestyle is omitted, 'solid' is used |
73 |
| -# See `matplotlib.collections.LineCollection` for more information |
74 |
| - |
75 |
| -# Make a sequence of (x, y) pairs. |
76 |
| -line_segments = LineCollection([np.column_stack([x, y]) for y in ys], |
| 60 | +line_segments = LineCollection(segs, array=x, |
77 | 61 | linewidths=(0.5, 1, 1.5, 2),
|
78 | 62 | linestyles='solid')
|
79 |
| -line_segments.set_array(x) |
80 | 63 | ax.add_collection(line_segments)
|
81 | 64 | axcb = fig.colorbar(line_segments)
|
82 | 65 | axcb.set_label('Line Number')
|
|
0 commit comments