|
| 1 | +# This figure shows the name of several matplotlib elements composing a figure |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import matplotlib.pyplot as plt |
| 5 | +from matplotlib.patches import Circle |
| 6 | +from matplotlib.ticker import MultipleLocator, FuncFormatter |
| 7 | + |
| 8 | +np.random.seed(123) |
| 9 | + |
| 10 | +X = np.linspace(0.5, 3.5, 100) |
| 11 | +Y1 = 3+np.cos(X) |
| 12 | +Y2 = 1+np.cos(1+X/0.75)/2 |
| 13 | +Y3 = np.random.uniform(Y1, Y2, len(X)) |
| 14 | + |
| 15 | +plt.figure(figsize=(8, 8), facecolor="w") |
| 16 | +ax = plt.subplot(111, aspect=1) |
| 17 | + |
| 18 | + |
| 19 | +def minor_tick(x, pos): |
| 20 | + if not x % 1.0: |
| 21 | + return "" |
| 22 | + return "%.2f" % x |
| 23 | + |
| 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)) |
| 29 | + |
| 30 | +plt.xlim(0, 4) |
| 31 | +plt.ylim(0, 4) |
| 32 | + |
| 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') |
| 37 | + |
| 38 | +plt.grid(linestyle="--", linewidth=0.5, color='.25', zorder=-10) |
| 39 | + |
| 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') |
| 43 | + |
| 44 | +plt.title("Anatomy of a figure", fontsize=20) |
| 45 | +plt.xlabel("X axis label") |
| 46 | +plt.ylabel("Y axis label") |
| 47 | + |
| 48 | +plt.legend(frameon=False) |
| 49 | + |
| 50 | + |
| 51 | +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) |
| 62 | + |
| 63 | + |
| 64 | +def text(x, y, text): |
| 65 | + plt.text(x, y, text, backgroundcolor="white", |
| 66 | + ha='center', va='top', weight='bold', color='blue') |
| 67 | + |
| 68 | + |
| 69 | +# Minor tick |
| 70 | +circle(0.50, -.05) |
| 71 | +text(0.50, -0.25, "Minor tick label") |
| 72 | + |
| 73 | +# Major tick |
| 74 | +circle(4.00, 2.00) |
| 75 | +text(4.00, 1.80, "Major tick") |
| 76 | + |
| 77 | +# Minor tick |
| 78 | +circle(0.25, 4.00) |
| 79 | +text(0.25, 3.80, "Minor tick") |
| 80 | + |
| 81 | +# Major tick label |
| 82 | +circle(-0.05, 3.00) |
| 83 | +text(-0.05, 2.80, "Major tick label") |
| 84 | + |
| 85 | +# X Label |
| 86 | +circle(1.80, -0.22) |
| 87 | +text(1.80, -0.4, "X axis label") |
| 88 | + |
| 89 | +# Y Label |
| 90 | +circle(-0.20, 1.80) |
| 91 | +text(-0.20, 1.6, "Y axis label") |
| 92 | + |
| 93 | +# Title |
| 94 | +circle(1.60, 4.10) |
| 95 | +text(1.60, 3.9, "Title") |
| 96 | + |
| 97 | +# Blue plot |
| 98 | +circle(1.75, 2.80) |
| 99 | +text(1.75, 2.60, "Line\n(line plot)") |
| 100 | + |
| 101 | +# Red plot |
| 102 | +circle(1.20, 0.60) |
| 103 | +text(1.20, 0.40, "Line\n(line plot)") |
| 104 | + |
| 105 | +# Scatter plot |
| 106 | +circle(3.20, 1.75) |
| 107 | +text(3.20, 1.55, "Markers\n(scatter plot)") |
| 108 | + |
| 109 | +# Grid |
| 110 | +circle(3.00, 3.00) |
| 111 | +text(3.00, 2.80, "Grid") |
| 112 | + |
| 113 | +# Legend |
| 114 | +circle(3.70, 3.75) |
| 115 | +text(3.70, 3.55, "Legend") |
| 116 | + |
| 117 | +# Axis |
| 118 | +circle(0.5, 0.5) |
| 119 | +text(0.5, 0.3, "Axis") |
| 120 | + |
| 121 | +# Plot |
| 122 | +circle(-0.3, 0.65) |
| 123 | +text(-0.3, 0.45, "Plot") |
| 124 | + |
| 125 | +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') |
| 142 | + |
| 143 | +plt.savefig("anatomy.pdf") |
| 144 | +plt.savefig("anatomy.png", dpi=150) |
| 145 | +plt.show() |
0 commit comments