Created
June 3, 2021 19:37
-
-
Save williamtellme123/688d3b048a160602f11cb9f06fca97d7 to your computer and use it in GitHub Desktop.
Anatomy of A Figure: State-Based
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
from matplotlib.ticker import AutoMinorLocator | |
np.random.seed(19680801) | |
X = np.linspace(0.5, 3.5, 100) | |
Y1 = 3 + np.cos(X) | |
Y2 = 1 + np.cos(1 + X / 0.75) / 2 | |
Y3 = np.random.uniform(Y1, Y2, len(X)) | |
# Implicity create one axes accessing with plt.() | |
fig = plt.figure(figsize=(8, 8)) # creates | |
def minor_tick(x, pos): | |
if not x % 1.0: | |
return "" | |
return f"{x:.2f}" | |
# Tick formatting | |
# These axes methods are run on plt.gca() "get current access" | |
plt.gca().xaxis.set_minor_locator(AutoMinorLocator(4)) | |
plt.gca().xaxis.set_minor_formatter(minor_tick) | |
# Additional tick parameters | |
plt.minorticks_on() # explicity turn them on | |
plt.tick_params(which="major", width=1.0, length=10) | |
plt.tick_params(which="minor", width=1.0, length=5, labelsize=10, labelbottom=True, labelcolor="C0") | |
# How many ticks appear on axis | |
plt.xticks(np.arange(5)) | |
plt.yticks(np.arange(5)) | |
# How far the plot shows data on the x axis (may exceed ticks) | |
plt.xlim(0, 4) | |
plt.ylim(0, 4) | |
# Actual plotting | |
plt.title("Anatomy of a Figure: State-Based", fontsize=20, verticalalignment="bottom") | |
plt.plot(X, Y1, c=(0.25, 0.25, 1.00), lw=2, label="Blue signal", zorder=10) | |
plt.plot(X, Y2, c=(1.00, 0.25, 0.25), lw=2, label="Red signal") | |
plt.plot(X, Y3, linewidth=0, marker="o", markerfacecolor="w", markeredgecolor="k") | |
plt.xlabel("X axis label") | |
plt.ylabel("Y axis label") | |
plt.legend() | |
plt.grid() | |
plt.tight_layout() | |
# Helper functions | |
def circle(x, y, radius=0.03): | |
from matplotlib.patches import Circle | |
circle = Circle( | |
(x, y), | |
radius, | |
clip_on=False, | |
zorder=10, | |
linewidth=1, | |
edgecolor="black", | |
facecolor=(0, 0, 0, 0.0125) | |
) | |
fig.add_artist(circle) | |
def text(x, y, text): | |
plt.text( | |
x, | |
y, | |
text, | |
backgroundcolor="white", | |
ha="center", | |
va="top", | |
weight="bold", | |
color="blue", | |
) | |
# Minor tick label | |
circle(.19, 0.07) | |
text(0.50, -0.23, "Minor tick label") | |
# Major tick | |
circle(.080, .93) | |
text(0.03, 3.81, "Major tick") | |
# Minor tick | |
circle(0.080, .849) | |
text(0.03, 3.43, "Minor tick") | |
# Major tick label | |
circle(.055, .725) | |
text(-0.07, 2.82, "Major tick label") | |
# X Label | |
circle(.49, 0.03) | |
text(1.87, -0.41, "X axis label") | |
# Y Label | |
circle(.025, .48) | |
text(-0.24, 1.68, "Y axis label") | |
# Title | |
circle(.399, .97) | |
text(1.43, 3.97, "Title") | |
# Blue plot | |
circle(.467, .685) | |
text(1.73, 2.62, "Line\n(line plot)") | |
# Red plot | |
circle(0.35, 0.212) | |
text(1.215, 0.41, "Line\n(line plot)") | |
# Scatter plot | |
circle(.80, .455) | |
text(3.23, 1.58, "Markers\n(scatter plot)") | |
# Grid | |
circle(.753, .722) | |
text(3.01, 2.83, "Grid") | |
# Legend | |
circle(.918, .898) | |
text(3.74, 3.66, "Legend") | |
# Axes | |
circle(0.16, 0.22) | |
text(0.370, 0.47, "Axes") | |
# Figure | |
circle(.023, .24) | |
text(-0.24, 0.55, "Figure") | |
plt.annotate( | |
"", | |
xy=(3.15, 0.00), | |
xytext=(3.4, 0.55), | |
arrowprops=dict(arrowstyle='->',connectionstyle="arc3",color="blue"), | |
size=12, | |
color="blue", | |
weight="bold" | |
) | |
plt.annotate( | |
"Spines", | |
xy=(3.98, 0.42), | |
xytext=(3.24, 0.63), | |
arrowprops=dict(arrowstyle='->',connectionstyle="arc3",color="blue"), | |
size=12, | |
color="blue", | |
weight="bold") | |
plt.text(3.91, -0.35, | |
"Made with https://matplotlib.org", | |
size=10, | |
ha="right", | |
color=".5") | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment