99
1010import numpy as np
1111import matplotlib .pyplot as plt
12- from matplotlib .patches import Circle , Rectangle
12+ from matplotlib .patches import Circle
1313from matplotlib .patheffects import withStroke
1414from matplotlib .ticker import AutoMinorLocator , MultipleLocator
1515
16- royal_blue = "#002082"
1716royal_blue = [0 , 20 / 256 , 82 / 256 ]
1817
18+
1919# make the figure
2020
2121np .random .seed (19680801 )
2525Y2 = 1 + np .cos (1 + X / 0.75 )/ 2
2626Y3 = np .random .uniform (Y1 , Y2 , len (X ))
2727
28- fig = plt .figure (figsize = (8 , 8 ), facecolor = '1' )
28+ fig = plt .figure (figsize = (8 , 8 ))
2929marg = 0.15
30- ax = fig .add_axes ([marg , marg , 1 - 1.8 * marg , 1 - 1.8 * marg ], aspect = 1 ,
31- facecolor = '1' )
32-
33-
34- def minor_tick (x , pos ):
35- if not x % 1.0 :
36- return ""
37- return f"{ x :.2f} "
30+ ax = fig .add_axes ([marg , marg , 1 - 1.8 * marg , 1 - 1.8 * marg ], aspect = 1 )
3831
3932ax .xaxis .set_major_locator (MultipleLocator (1.000 ))
4033ax .xaxis .set_minor_locator (AutoMinorLocator (4 ))
4134ax .yaxis .set_major_locator (MultipleLocator (1.000 ))
4235ax .yaxis .set_minor_locator (AutoMinorLocator (4 ))
43- # FuncFormatter is created and used automatically
44- ax .xaxis .set_minor_formatter (minor_tick )
36+ ax .xaxis .set_minor_formatter ("{x:.2f}" )
4537
4638ax .set_xlim (0 , 4 )
4739ax .set_ylim (0 , 4 )
4840
49- ax .tick_params (which = 'major' , width = 1.0 , labelsize = 14 )
50- ax .tick_params (which = 'major' , length = 10 , labelsize = 14 )
51- ax .tick_params (which = 'minor' , width = 1.0 , labelsize = 10 )
52- ax .tick_params (which = 'minor' , length = 5 , labelsize = 10 , labelcolor = '0.25' )
41+ ax .tick_params (which = 'major' , width = 1.0 , length = 10 , labelsize = 14 )
42+ ax .tick_params (which = 'minor' , width = 1.0 , length = 5 , labelsize = 10 ,
43+ labelcolor = '0.25' )
5344
5445ax .grid (linestyle = "--" , linewidth = 0.5 , color = '.25' , zorder = - 10 )
5546
@@ -65,89 +56,46 @@ def minor_tick(x, pos):
6556
6657ax .legend (loc = "upper right" , fontsize = 14 )
6758
68- # Annotate the figure
6959
60+ # Annotate the figure
7061
71- def just_circle (x , y , radius = 0.15 ):
62+ def annotate (x , y , text , code , radius = 0.15 ):
63+ # Circle marker
7264 c = Circle ((x , y ), radius , clip_on = False , zorder = 10 , linewidth = 2.5 ,
7365 edgecolor = royal_blue + [0.6 ], facecolor = 'none' ,
7466 path_effects = [withStroke (linewidth = 7 , foreground = (1 , 1 , 1 , 1 ))])
7567 ax .add_artist (c )
7668
77-
78- def text (x , y , text ):
79- ax .text (x , y , text , zorder = 100 ,
69+ ax .text (x , y - 0.2 , text , zorder = 100 ,
8070 ha = 'center' , va = 'top' , weight = 'bold' , color = royal_blue ,
8171 style = 'italic' , fontfamily = 'monospace' ,
8272 path_effects = [withStroke (linewidth = 7 , foreground = (1 , 1 , 1 , 1 ))])
8373
84-
85- def code (x , y , text ):
86- ax .text (x , y , text , zorder = 100 ,
74+ ax .text (x , y - 0.33 , code , zorder = 100 ,
8775 ha = 'center' , va = 'top' , weight = 'normal' , color = '0.0' ,
8876 fontfamily = 'Courier New' , fontsize = 'medium' ,
8977 path_effects = [withStroke (linewidth = 7 , foreground = (1 , 1 , 1 , 1 ))])
9078
9179
92- def circle (x , y , txt , cde , radius = 0.15 ):
93- just_circle (x , y , radius = radius )
94- text (x , y - 0.2 , txt )
95- code (x , y - 0.33 , cde )
96-
97- # Minor tick label
98- circle (3.25 , - 0.10 , "Minor tick label" ,
99- "ax.xaxis.set_minor_formatter" )
100-
101- # Major tick
102- circle (- 0.03 , 1.05 , "Major tick" , "ax.yaxis.set_major_locator" )
103-
104- # Minor tick
105- y = 3.75
106- circle (0.00 , 3.75 , "Minor tick" , "ax.yaxis.set_minor_locator" )
107-
108- # Major tick label
109- circle (- 0.15 , 3.00 , "Major tick label" , "ax.yaxis.set_major_formatter" )
110-
111- # X Label
112- circle (1.90 , - 0.32 , "xlabel" , "ax.set_xlabel" )
113-
114- # Y Label
115- circle (- 0.27 , 1.68 , "ylabel" , "ax.set_ylabel" )
116-
117- # Title
118- circle (1.58 , 4.13 , "Title" , "ax.set_title" )
119-
120- # Blue plot
121- circle (1.75 , 2.80 , "Line" , "ax.plot" )
122-
123- # Scatter plot
124- circle (2.25 , 1.54 , "Markers" , "ax.scatter" )
125-
126- # Grid
127- circle (3.00 , 3.00 , "Grid" , "ax.grid" )
128-
129- # Legend
130- circle (3.60 , 3.65 , "Legend" , "ax.legend" )
131-
132- # Axes
133- circle (2.5 , 0.55 , "Axes" , "fig.subplots" )
134-
135- # Figure
136- circle (4.185 , 4.3 , "Figure" , "plt.figure" )
137-
138- # x Axis
139- circle (0.65 , 0.01 , "x Axis" , "ax.xaxis" )
140-
141- # y Axis
142- circle (0 , 0.44 , "y Axis" , "ax.yaxis" )
143-
144- # Spine
145- circle (4.0 , 0.7 , "Spine" , "ax.spines" )
146-
147- # frame around figure...
148- fig .add_artist (Rectangle ((0 , 0 ), width = 1 , height = 1 , facecolor = 'none' ,
149- edgecolor = '0.5' , linewidth = 10 ))
150-
80+ annotate (3.25 , - 0.10 , "Minor tick label" , "ax.xaxis.set_minor_formatter" )
81+ annotate (- 0.03 , 1.05 , "Major tick" , "ax.yaxis.set_major_locator" )
82+ annotate (0.00 , 3.75 , "Minor tick" , "ax.yaxis.set_minor_locator" )
83+ annotate (- 0.15 , 3.00 , "Major tick label" , "ax.yaxis.set_major_formatter" )
84+ annotate (1.90 , - 0.32 , "xlabel" , "ax.set_xlabel" )
85+ annotate (- 0.27 , 1.68 , "ylabel" , "ax.set_ylabel" )
86+ annotate (1.58 , 4.13 , "Title" , "ax.set_title" )
87+ annotate (1.75 , 2.80 , "Line" , "ax.plot" )
88+ annotate (2.25 , 1.54 , "Markers" , "ax.scatter" )
89+ annotate (3.00 , 3.00 , "Grid" , "ax.grid" )
90+ annotate (3.60 , 3.65 , "Legend" , "ax.legend" )
91+ annotate (2.5 , 0.55 , "Axes" , "fig.subplots" )
92+ annotate (4.185 , 4.3 , "Figure" , "plt.figure" )
93+ annotate (0.65 , 0.01 , "x Axis" , "ax.xaxis" )
94+ annotate (0 , 0.44 , "y Axis" , "ax.yaxis" )
95+ annotate (4.0 , 0.7 , "Spine" , "ax.spines" )
96+
97+ # frame around figure
98+ fig .patch .set (linewidth = 10 , edgecolor = '0.5' )
15199plt .show ()
152100
153101
0 commit comments