|
5 | 5 |
|
6 | 6 | Demo fill plot.
|
7 | 7 | """
|
| 8 | + |
| 9 | +############################################################################### |
| 10 | +# First, the most basic fill plot a user can make with matplotlib: |
8 | 11 | import numpy as np
|
9 | 12 | import matplotlib.pyplot as plt
|
10 | 13 |
|
11 |
| -x = np.linspace(0, 1, 500) |
12 |
| -y = np.sin(4 * np.pi * x) * np.exp(-5 * x) |
13 | 14 |
|
14 |
| -############################################################################### |
15 |
| -# First, the most basic fill plot a user can make with matplotlib: |
| 15 | +x = [0, 0, 1, 2, 2] |
| 16 | +y = [0, 1, 2, 1, 0] |
16 | 17 |
|
17 | 18 | fig, ax = plt.subplots()
|
18 |
| - |
19 | 19 | ax.fill(x, y, zorder=10)
|
20 | 20 | ax.grid(True, zorder=5)
|
21 | 21 |
|
22 |
| -x = np.linspace(0, 2 * np.pi, 500) |
23 |
| -y1 = np.sin(x) |
24 |
| -y2 = np.sin(3 * x) |
| 22 | + |
25 | 23 |
|
26 | 24 | ###############################################################################
|
27 | 25 | # Next, a few more optional features:
|
|
30 | 28 | # * Setting the fill color.
|
31 | 29 | # * Setting the opacity (alpha value).
|
32 | 30 |
|
| 31 | + |
| 32 | +x = np.linspace(0, 1.5 * np.pi, 500) |
| 33 | +y1 = np.sin(x) |
| 34 | +y2 = np.sin(3 * x) |
| 35 | + |
33 | 36 | fig, ax = plt.subplots()
|
| 37 | + |
34 | 38 | ax.fill(x, y1, 'b', x, y2, 'r', alpha=0.3)
|
| 39 | + |
| 40 | +# Also outline the region we've filled in |
| 41 | +ax.plot(x, y1, c='b', alpha=0.8) |
| 42 | +ax.plot(x, y2, c='r', alpha=0.8) |
| 43 | +ax.plot([x[0], x[-1]], [y1[0], y1[-1]], c='b', alpha=0.8) |
| 44 | +ax.plot([x[0], x[-1]], [y2[0], y2[-1]], c='r', alpha=0.8) |
| 45 | + |
| 46 | + |
35 | 47 | plt.show()
|
0 commit comments