|
| 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.ticker import MultipleLocator, FuncFormatter |
| 6 | + |
| 7 | +np.random.seed(123) |
| 8 | + |
| 9 | +X = np.linspace(0.5, 3.5, 100) |
| 10 | +Y1 = 3+np.cos(X) |
| 11 | +Y2 = 1+np.cos(1+X/0.75)/2 |
| 12 | +Y3 = np.random.uniform(Y1, Y2, len(X)) |
| 13 | + |
| 14 | +fig = plt.figure(figsize=(8, 8), facecolor="w") |
| 15 | +ax = fig.add_subplot(1, 1, 1, aspect=1) |
| 16 | + |
| 17 | + |
| 18 | +def minor_tick(x, pos): |
| 19 | + if not x % 1.0: |
| 20 | + return "" |
| 21 | + return "%.2f" % x |
| 22 | + |
| 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)) |
| 28 | + |
| 29 | +ax.set_xlim(0, 4) |
| 30 | +ax.set_ylim(0, 4) |
| 31 | + |
| 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') |
| 36 | + |
| 37 | +ax.grid(linestyle="--", linewidth=0.5, color='.25', zorder=-10) |
| 38 | + |
| 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') |
| 42 | + |
| 43 | +ax.set_title("Anatomy of a figure", fontsize=20) |
| 44 | +ax.set_xlabel("X axis label") |
| 45 | +ax.set_ylabel("Y axis label") |
| 46 | + |
| 47 | +ax.legend(frameon=False) |
| 48 | + |
| 49 | + |
| 50 | +def circle(x, y, radius=0.15): |
| 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) |
| 57 | + |
| 58 | + |
| 59 | +def text(x, y, text): |
| 60 | + ax.text(x, y, text, backgroundcolor="white", |
| 61 | + ha='center', va='top', weight='bold', color='blue') |
| 62 | + |
| 63 | + |
| 64 | +# Minor tick |
| 65 | +circle(0.50, -.05) |
| 66 | +text(0.50, -0.25, "Minor tick label") |
| 67 | + |
| 68 | +# Major tick |
| 69 | +circle(4.00, 2.00) |
| 70 | +text(4.00, 1.80, "Major tick") |
| 71 | + |
| 72 | +# Minor tick |
| 73 | +circle(0.25, 4.00) |
| 74 | +text(0.25, 3.80, "Minor tick") |
| 75 | + |
| 76 | +# Major tick label |
| 77 | +circle(-0.05, 3.00) |
| 78 | +text(-0.05, 2.80, "Major tick label") |
| 79 | + |
| 80 | +# X Label |
| 81 | +circle(1.80, -0.22) |
| 82 | +text(1.80, -0.4, "X axis label") |
| 83 | + |
| 84 | +# Y Label |
| 85 | +circle(-0.20, 1.80) |
| 86 | +text(-0.20, 1.6, "Y axis label") |
| 87 | + |
| 88 | +# Title |
| 89 | +circle(1.60, 4.10) |
| 90 | +text(1.60, 3.9, "Title") |
| 91 | + |
| 92 | +# Blue plot |
| 93 | +circle(1.75, 2.80) |
| 94 | +text(1.75, 2.60, "Line\n(line plot)") |
| 95 | + |
| 96 | +# Red plot |
| 97 | +circle(1.20, 0.60) |
| 98 | +text(1.20, 0.40, "Line\n(line plot)") |
| 99 | + |
| 100 | +# Scatter plot |
| 101 | +circle(3.20, 1.75) |
| 102 | +text(3.20, 1.55, "Markers\n(scatter plot)") |
| 103 | + |
| 104 | +# Grid |
| 105 | +circle(3.00, 3.00) |
| 106 | +text(3.00, 2.80, "Grid") |
| 107 | + |
| 108 | +# Legend |
| 109 | +circle(3.70, 3.75) |
| 110 | +text(3.70, 3.55, "Legend") |
| 111 | + |
| 112 | +# Axes |
| 113 | +circle(0.5, 0.5) |
| 114 | +text(0.5, 0.3, "Axes") |
| 115 | + |
| 116 | +# Figure |
| 117 | +circle(-0.3, 0.65) |
| 118 | +text(-0.3, 0.45, "Figure") |
| 119 | + |
| 120 | +color = 'blue' |
| 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') |
| 137 | + |
| 138 | +plt.show() |
0 commit comments