1""" 2=================== 3Anatomy of a figure 4=================== 5 6This figure shows the name of several matplotlib elements composing a figure 7""" 8 9import numpy as np 10import matplotlib.pyplot as plt 11from matplotlib.ticker import AutoMinorLocator, MultipleLocator, FuncFormatter 12 13np.random.seed(19680801) 14 15X = np.linspace(0.5, 3.5, 100) 16Y1 = 3+np.cos(X) 17Y2 = 1+np.cos(1+X/0.75)/2 18Y3 = np.random.uniform(Y1, Y2, len(X)) 19 20fig = plt.figure(figsize=(8, 8)) 21ax = fig.add_subplot(1, 1, 1, aspect=1) 22 23 24def minor_tick(x, pos): 25 if not x % 1.0: 26 return "" 27 return "%.2f" % x 28 29ax.xaxis.set_major_locator(MultipleLocator(1.000)) 30ax.xaxis.set_minor_locator(AutoMinorLocator(4)) 31ax.yaxis.set_major_locator(MultipleLocator(1.000)) 32ax.yaxis.set_minor_locator(AutoMinorLocator(4)) 33ax.xaxis.set_minor_formatter(FuncFormatter(minor_tick)) 34 35ax.set_xlim(0, 4) 36ax.set_ylim(0, 4) 37 38ax.tick_params(which='major', width=1.0) 39ax.tick_params(which='major', length=10) 40ax.tick_params(which='minor', width=1.0, labelsize=10) 41ax.tick_params(which='minor', length=5, labelsize=10, labelcolor='0.25') 42 43ax.grid(linestyle="--", linewidth=0.5, color='.25', zorder=-10) 44 45ax.plot(X, Y1, c=(0.25, 0.25, 1.00), lw=2, label="Blue signal", zorder=10) 46ax.plot(X, Y2, c=(1.00, 0.25, 0.25), lw=2, label="Red signal") 47ax.plot(X, Y3, linewidth=0, 48 marker='o', markerfacecolor='w', markeredgecolor='k') 49 50ax.set_title("Anatomy of a figure", fontsize=20, verticalalignment='bottom') 51ax.set_xlabel("X axis label") 52ax.set_ylabel("Y axis label") 53 54ax.legend() 55 56 57def circle(x, y, radius=0.15): 58 from matplotlib.patches import Circle 59 from matplotlib.patheffects import withStroke 60 circle = Circle((x, y), radius, clip_on=False, zorder=10, linewidth=1, 61 edgecolor='black', facecolor=(0, 0, 0, .0125), 62 path_effects=[withStroke(linewidth=5, foreground='w')]) 63 ax.add_artist(circle) 64 65 66def text(x, y, text): 67 ax.text(x, y, text, backgroundcolor="white", 68 ha='center', va='top', weight='bold', color='blue') 69 70 71# Minor tick 72circle(0.50, -0.10) 73text(0.50, -0.32, "Minor tick label") 74 75# Major tick 76circle(-0.03, 4.00) 77text(0.03, 3.80, "Major tick") 78 79# Minor tick 80circle(0.00, 3.50) 81text(0.00, 3.30, "Minor tick") 82 83# Major tick label 84circle(-0.15, 3.00) 85text(-0.15, 2.80, "Major tick label") 86 87# X Label 88circle(1.80, -0.27) 89text(1.80, -0.45, "X axis label") 90 91# Y Label 92circle(-0.27, 1.80) 93text(-0.27, 1.6, "Y axis label") 94 95# Title 96circle(1.60, 4.13) 97text(1.60, 3.93, "Title") 98 99# Blue plot 100circle(1.75, 2.80) 101text(1.75, 2.60, "Line\n(line plot)") 102 103# Red plot 104circle(1.20, 0.60) 105text(1.20, 0.40, "Line\n(line plot)") 106 107# Scatter plot 108circle(3.20, 1.75) 109text(3.20, 1.55, "Markers\n(scatter plot)") 110 111# Grid 112circle(3.00, 3.00) 113text(3.00, 2.80, "Grid") 114 115# Legend 116circle(3.70, 3.80) 117text(3.70, 3.60, "Legend") 118 119# Axes 120circle(0.5, 0.5) 121text(0.5, 0.3, "Axes") 122 123# Figure 124circle(-0.3, 0.65) 125text(-0.3, 0.45, "Figure") 126 127color = 'blue' 128ax.annotate('Spines', xy=(4.0, 0.35), xycoords='data', 129 xytext=(3.3, 0.5), textcoords='data', 130 weight='bold', color=color, 131 arrowprops=dict(arrowstyle='->', 132 connectionstyle="arc3", 133 color=color)) 134 135ax.annotate('', xy=(3.15, 0.0), xycoords='data', 136 xytext=(3.45, 0.45), textcoords='data', 137 weight='bold', color=color, 138 arrowprops=dict(arrowstyle='->', 139 connectionstyle="arc3", 140 color=color)) 141 142ax.text(4.0, -0.4, "Made with http://matplotlib.org", 143 fontsize=10, ha="right", color='.5') 144 145plt.show() 146