Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Improve formatting of "Anatomy of a figure" #22206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified doc/_static/anatomy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
146 changes: 49 additions & 97 deletions examples/showcase/anatomy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Circle, Rectangle
from matplotlib.patches import Circle
from matplotlib.patheffects import withStroke
from matplotlib.ticker import AutoMinorLocator, MultipleLocator

royal_blue = "#002082"
royal_blue = [0, 20/256, 82/256]


# make the figure

np.random.seed(19680801)
Expand All @@ -25,31 +25,21 @@
Y2 = 1+np.cos(1+X/0.75)/2
Y3 = np.random.uniform(Y1, Y2, len(X))

fig = plt.figure(figsize=(8, 8), facecolor='1')
marg = 0.15
ax = fig.add_axes([marg, marg, 1-1.8*marg, 1-1.8*marg], aspect=1,
facecolor='1')


def minor_tick(x, pos):
if not x % 1.0:
return ""
return f"{x:.2f}"
fig = plt.figure(figsize=(7.5, 7.5))
ax = fig.add_axes([0.2, 0.17, 0.68, 0.7], aspect=1)

ax.xaxis.set_major_locator(MultipleLocator(1.000))
ax.xaxis.set_minor_locator(AutoMinorLocator(4))
ax.yaxis.set_major_locator(MultipleLocator(1.000))
ax.yaxis.set_minor_locator(AutoMinorLocator(4))
# FuncFormatter is created and used automatically
ax.xaxis.set_minor_formatter(minor_tick)
ax.xaxis.set_minor_formatter("{x:.2f}")

ax.set_xlim(0, 4)
ax.set_ylim(0, 4)

ax.tick_params(which='major', width=1.0, labelsize=14)
ax.tick_params(which='major', length=10, labelsize=14)
ax.tick_params(which='minor', width=1.0, labelsize=10)
ax.tick_params(which='minor', length=5, labelsize=10, labelcolor='0.25')
ax.tick_params(which='major', width=1.0, length=10, labelsize=14)
ax.tick_params(which='minor', width=1.0, length=5, labelsize=10,
labelcolor='0.25')

ax.grid(linestyle="--", linewidth=0.5, color='.25', zorder=-10)

Expand All @@ -62,92 +52,54 @@ def minor_tick(x, pos):
ax.set_title("Anatomy of a figure", fontsize=20, verticalalignment='bottom')
ax.set_xlabel("x Axis label", fontsize=14)
ax.set_ylabel("y Axis label", fontsize=14)

ax.legend(loc="upper right", fontsize=14)

# Annotate the figure

# Annotate the figure

def just_circle(x, y, radius=0.15):
c = Circle((x, y), radius, clip_on=False, zorder=10, linewidth=2.5,
def annotate(x, y, text, code):
# Circle marker
c = Circle((x, y), radius=0.15, clip_on=False, zorder=10, linewidth=2.5,
edgecolor=royal_blue + [0.6], facecolor='none',
path_effects=[withStroke(linewidth=7, foreground=(1, 1, 1, 1))])
path_effects=[withStroke(linewidth=7, foreground='white')])
ax.add_artist(c)


def text(x, y, text):
ax.text(x, y, text, zorder=100,
ha='center', va='top', weight='bold', color=royal_blue,
style='italic', fontfamily='monospace',
path_effects=[withStroke(linewidth=7, foreground=(1, 1, 1, 1))])


def code(x, y, text):
ax.text(x, y, text, zorder=100,
ha='center', va='top', weight='normal', color='0.0',
fontfamily='Courier New', fontsize='medium',
path_effects=[withStroke(linewidth=7, foreground=(1, 1, 1, 1))])


def circle(x, y, txt, cde, radius=0.15):
just_circle(x, y, radius=radius)
text(x, y-0.2, txt)
code(x, y-0.33, cde)

# Minor tick label
circle(3.25, -0.10, "Minor tick label",
"ax.xaxis.set_minor_formatter")

# Major tick
circle(-0.03, 1.05, "Major tick", "ax.yaxis.set_major_locator")

# Minor tick
y = 3.75
circle(0.00, 3.75, "Minor tick", "ax.yaxis.set_minor_locator")

# Major tick label
circle(-0.15, 3.00, "Major tick label", "ax.yaxis.set_major_formatter")

# X Label
circle(1.90, -0.32, "xlabel", "ax.set_xlabel")

# Y Label
circle(-0.27, 1.68, "ylabel", "ax.set_ylabel")

# Title
circle(1.58, 4.13, "Title", "ax.set_title")

# Blue plot
circle(1.75, 2.80, "Line", "ax.plot")

# Scatter plot
circle(2.25, 1.54, "Markers", "ax.scatter")

# Grid
circle(3.00, 3.00, "Grid", "ax.grid")

# Legend
circle(3.60, 3.65, "Legend", "ax.legend")

# Axes
circle(2.5, 0.55, "Axes", "fig.subplots")

# Figure
circle(4.185, 4.3, "Figure", "plt.figure")

# x Axis
circle(0.65, 0.01, "x Axis", "ax.xaxis")

# y Axis
circle(0, 0.44, "y Axis", "ax.yaxis")

# Spine
circle(4.0, 0.7, "Spine", "ax.spines")

# frame around figure...
fig.add_artist(Rectangle((0, 0), width=1, height=1, facecolor='none',
edgecolor='0.5', linewidth=10))

# use path_effects as a background for the texts
# draw the path_effects and the colored text separately so that the
# path_effects cannot clip other texts
for path_effects in [[withStroke(linewidth=7, foreground='white')], []]:
color = 'white' if path_effects else royal_blue
ax.text(x, y-0.2, text, zorder=100,
ha='center', va='top', weight='bold', color=color,
style='italic', fontfamily='Courier New',
path_effects=path_effects)

color = 'white' if path_effects else 'black'
ax.text(x, y-0.33, code, zorder=100,
ha='center', va='top', weight='normal', color=color,
fontfamily='monospace', fontsize='medium',
path_effects=path_effects)


annotate(3.5, -0.13, "Minor tick label", "ax.xaxis.set_minor_formatter")
annotate(-0.03, 1.0, "Major tick", "ax.yaxis.set_major_locator")
annotate(0.00, 3.75, "Minor tick", "ax.yaxis.set_minor_locator")
annotate(-0.15, 3.00, "Major tick label", "ax.yaxis.set_major_formatter")
annotate(1.68, -0.39, "xlabel", "ax.set_xlabel")
annotate(-0.38, 1.67, "ylabel", "ax.set_ylabel")
annotate(1.52, 4.15, "Title", "ax.set_title")
annotate(1.75, 2.80, "Line", "ax.plot")
annotate(2.25, 1.54, "Markers", "ax.scatter")
annotate(3.00, 3.00, "Grid", "ax.grid")
annotate(3.60, 3.58, "Legend", "ax.legend")
annotate(2.5, 0.55, "Axes", "fig.subplots")
annotate(4, 4.5, "Figure", "plt.figure")
annotate(0.65, 0.01, "x Axis", "ax.xaxis")
annotate(0, 0.36, "y Axis", "ax.yaxis")
annotate(4.0, 0.7, "Spine", "ax.spines")

# frame around figure
fig.patch.set(linewidth=4, edgecolor='0.5')
plt.show()


Expand Down