|
1 |
| -from pylab import * |
| 1 | +import matplotlib.pyplot as plt |
| 2 | +import numpy as np |
2 | 3 | from matplotlib.collections import LineCollection
|
3 | 4 |
|
4 | 5 | # In order to efficiently plot many lines in a single set of axes,
|
5 | 6 | # Matplotlib has the ability to add the lines all at once. Here is a
|
6 | 7 | # simple example showing how it is done.
|
7 | 8 |
|
8 | 9 | N = 50
|
9 |
| -x = arange(N) |
| 10 | +x = np.arange(N) |
10 | 11 | # Here are many sets of y to plot vs x
|
11 | 12 | ys = [x + i for i in x]
|
12 | 13 |
|
13 | 14 | # We need to set the plot limits, they will not autoscale
|
14 |
| -ax = axes() |
15 |
| -ax.set_xlim((amin(x), amax(x))) |
16 |
| -ax.set_ylim((amin(amin(ys)), amax(amax(ys)))) |
| 15 | +ax = plt.axes() |
| 16 | +ax.set_xlim((np.amin(x), np.amax(x))) |
| 17 | +ax.set_ylim((np.amin(np.amin(ys)), np.amax(np.amax(ys)))) |
17 | 18 |
|
18 | 19 | # colors is sequence of rgba tuples
|
19 | 20 | # linestyle is a string or dash tuple. Legal string values are
|
20 | 21 | # solid|dashed|dashdot|dotted. The dash tuple is (offset, onoffseq)
|
21 | 22 | # where onoffseq is an even length tuple of on and off ink in points.
|
22 | 23 | # If linestyle is omitted, 'solid' is used
|
23 | 24 | # See matplotlib.collections.LineCollection for more information
|
24 |
| -line_segments = LineCollection([list(zip(x, y)) for y in ys], # Make a sequence of x,y pairs |
| 25 | + |
| 26 | +# Make a sequence of x,y pairs |
| 27 | +line_segments = LineCollection([list(zip(x, y)) for y in ys], |
25 | 28 | linewidths=(0.5, 1, 1.5, 2),
|
26 | 29 | linestyles='solid')
|
27 | 30 | line_segments.set_array(x)
|
28 | 31 | ax.add_collection(line_segments)
|
29 |
| -fig = gcf() |
| 32 | +fig = plt.gcf() |
30 | 33 | axcb = fig.colorbar(line_segments)
|
31 | 34 | axcb.set_label('Line Number')
|
32 | 35 | ax.set_title('Line Collection with mapped colors')
|
33 |
| -sci(line_segments) # This allows interactive changing of the colormap. |
34 |
| -show() |
| 36 | +plt.sci(line_segments) # This allows interactive changing of the colormap. |
| 37 | +plt.show() |
0 commit comments