|
2 | 2 |
|
3 | 3 | import numpy as np |
4 | 4 | import matplotlib.pyplot as plt |
5 | | -from matplotlib.patches import Circle |
6 | 5 | from matplotlib.ticker import MultipleLocator, FuncFormatter |
7 | 6 |
|
8 | 7 | np.random.seed(123) |
|
12 | 11 | Y2 = 1+np.cos(1+X/0.75)/2 |
13 | 12 | Y3 = np.random.uniform(Y1, Y2, len(X)) |
14 | 13 |
|
15 | | -plt.figure(figsize=(8, 8), facecolor="w") |
16 | | -ax = plt.subplot(1, 1, 1, aspect=1) |
| 14 | +fig = plt.figure(figsize=(8, 8), facecolor="w") |
| 15 | +ax = fig.add_subplot(1, 1, 1, aspect=1) |
17 | 16 |
|
18 | 17 |
|
19 | 18 | def minor_tick(x, pos): |
20 | 19 | if not x % 1.0: |
21 | 20 | return "" |
22 | 21 | return "%.2f" % x |
23 | 22 |
|
24 | | -plt.axes().xaxis.set_major_locator(MultipleLocator(1.000)) |
25 | | -plt.axes().xaxis.set_minor_locator(MultipleLocator(0.250)) |
26 | | -plt.axes().yaxis.set_major_locator(MultipleLocator(1.000)) |
27 | | -plt.axes().yaxis.set_minor_locator(MultipleLocator(0.250)) |
28 | | -plt.axes().xaxis.set_minor_formatter(FuncFormatter(minor_tick)) |
| 23 | +ax.xaxis.set_major_locator(MultipleLocator(1.000)) |
| 24 | +ax.xaxis.set_minor_locator(MultipleLocator(0.250)) |
| 25 | +ax.yaxis.set_major_locator(MultipleLocator(1.000)) |
| 26 | +ax.yaxis.set_minor_locator(MultipleLocator(0.250)) |
| 27 | +ax.xaxis.set_minor_formatter(FuncFormatter(minor_tick)) |
29 | 28 |
|
30 | | -plt.xlim(0, 4) |
31 | | -plt.ylim(0, 4) |
| 29 | +ax.set_xlim(0, 4) |
| 30 | +ax.set_ylim(0, 4) |
32 | 31 |
|
33 | | -plt.tick_params(which='major', width=1.0) |
34 | | -plt.tick_params(which='major', length=10) |
35 | | -plt.tick_params(which='minor', width=1.0, labelsize=10) |
36 | | -plt.tick_params(which='minor', length=5, labelsize=10, labelcolor='0.25') |
| 32 | +ax.tick_params(which='major', width=1.0) |
| 33 | +ax.tick_params(which='major', length=10) |
| 34 | +ax.tick_params(which='minor', width=1.0, labelsize=10) |
| 35 | +ax.tick_params(which='minor', length=5, labelsize=10, labelcolor='0.25') |
37 | 36 |
|
38 | | -plt.grid(linestyle="--", linewidth=0.5, color='.25', zorder=-10) |
| 37 | +ax.grid(linestyle="--", linewidth=0.5, color='.25', zorder=-10) |
39 | 38 |
|
40 | | -plt.plot(X, Y1, c=(0.25, 0.25, 1.00), lw=2, label="Blue signal", zorder=10) |
41 | | -plt.plot(X, Y2, c=(1.00, 0.25, 0.25), lw=2, label="Red signal") |
42 | | -plt.scatter(X, Y3, c='w') |
| 39 | +ax.plot(X, Y1, c=(0.25, 0.25, 1.00), lw=2, label="Blue signal", zorder=10) |
| 40 | +ax.plot(X, Y2, c=(1.00, 0.25, 0.25), lw=2, label="Red signal") |
| 41 | +ax.scatter(X, Y3, c='w') |
43 | 42 |
|
44 | | -plt.title("Anatomy of a figure", fontsize=20) |
45 | | -plt.xlabel("X axis label") |
46 | | -plt.ylabel("Y axis label") |
| 43 | +ax.set_title("Anatomy of a figure", fontsize=20) |
| 44 | +ax.set_xlabel("X axis label") |
| 45 | +ax.set_ylabel("Y axis label") |
47 | 46 |
|
48 | | -plt.legend(frameon=False) |
| 47 | +ax.legend(frameon=False) |
49 | 48 |
|
50 | 49 |
|
51 | 50 | def circle(x, y, radius=0.15): |
52 | | - center = x, y |
53 | | - circle = Circle(center, radius, clip_on=False, zorder=10, |
54 | | - edgecolor='white', facecolor='none', linewidth=5.0) |
55 | | - plt.axes().add_artist(circle) |
56 | | - circle = Circle(center, radius, clip_on=False, zorder=20, |
57 | | - edgecolor='none', facecolor='black', alpha=.025) |
58 | | - plt.axes().add_artist(circle) |
59 | | - circle = Circle(center, radius, clip_on=False, zorder=30, |
60 | | - edgecolor='black', facecolor='none', linewidth=1.0) |
61 | | - plt.axes().add_artist(circle) |
| 51 | + from matplotlib.patches import Circle |
| 52 | + from matplotlib.patheffects import withStroke |
| 53 | + circle = Circle((x, y), radius, clip_on=False, zorder=10, linewidth=1, |
| 54 | + edgecolor='black', facecolor=(0, 0, 0, .0125), |
| 55 | + path_effects=[withStroke(linewidth=5, foreground='w')]) |
| 56 | + ax.add_artist(circle) |
62 | 57 |
|
63 | 58 |
|
64 | 59 | def text(x, y, text): |
65 | | - plt.text(x, y, text, backgroundcolor="white", |
66 | | - ha='center', va='top', weight='bold', color='blue') |
| 60 | + ax.text(x, y, text, backgroundcolor="white", |
| 61 | + ha='center', va='top', weight='bold', color='blue') |
67 | 62 |
|
68 | 63 |
|
69 | 64 | # Minor tick |
@@ -123,21 +118,21 @@ def text(x, y, text): |
123 | 118 | text(-0.3, 0.45, "Figure") |
124 | 119 |
|
125 | 120 | color = 'blue' |
126 | | -plt.annotate('Spines', xy=(4.0, 0.35), xycoords='data', |
127 | | - xytext=(3.3, 0.5), textcoords='data', |
128 | | - weight='bold', color=color, |
129 | | - arrowprops=dict(arrowstyle='->', |
130 | | - connectionstyle="arc3", |
131 | | - color=color)) |
132 | | - |
133 | | -plt.annotate('', xy=(3.15, 0.0), xycoords='data', |
134 | | - xytext=(3.45, 0.45), textcoords='data', |
135 | | - weight='bold', color=color, |
136 | | - arrowprops=dict(arrowstyle='->', |
137 | | - connectionstyle="arc3", |
138 | | - color=color)) |
139 | | - |
140 | | -plt.text(4.0, -0.4, "Made with http://matplotlib.org", |
141 | | - fontsize=10, ha="right", color='.5') |
| 121 | +ax.annotate('Spines', xy=(4.0, 0.35), xycoords='data', |
| 122 | + xytext=(3.3, 0.5), textcoords='data', |
| 123 | + weight='bold', color=color, |
| 124 | + arrowprops=dict(arrowstyle='->', |
| 125 | + connectionstyle="arc3", |
| 126 | + color=color)) |
| 127 | + |
| 128 | +ax.annotate('', xy=(3.15, 0.0), xycoords='data', |
| 129 | + xytext=(3.45, 0.45), textcoords='data', |
| 130 | + weight='bold', color=color, |
| 131 | + arrowprops=dict(arrowstyle='->', |
| 132 | + connectionstyle="arc3", |
| 133 | + color=color)) |
| 134 | + |
| 135 | +ax.text(4.0, -0.4, "Made with http://matplotlib.org", |
| 136 | + fontsize=10, ha="right", color='.5') |
142 | 137 |
|
143 | 138 | plt.show() |
0 commit comments