6
6
This figure shows the name of several matplotlib elements composing a figure
7
7
"""
8
8
9
+
9
10
import numpy as np
10
11
import matplotlib .pyplot as plt
12
+ from matplotlib .patches import Circle , Rectangle
13
+ from matplotlib .patheffects import withStroke
11
14
from matplotlib .ticker import AutoMinorLocator , MultipleLocator
12
15
16
+ royal_blue = "#002082"
17
+ royal_blue = [0 , 20 / 256 , 82 / 256 ]
18
+
19
+ # make the figure
20
+
13
21
np .random .seed (19680801 )
14
22
15
23
X = np .linspace (0.5 , 3.5 , 100 )
16
24
Y1 = 3 + np .cos (X )
17
25
Y2 = 1 + np .cos (1 + X / 0.75 )/ 2
18
26
Y3 = np .random .uniform (Y1 , Y2 , len (X ))
19
27
20
- fig = plt .figure (figsize = (8 , 8 ))
21
- ax = fig .add_subplot (1 , 1 , 1 , aspect = 1 )
28
+ fig = plt .figure (figsize = (8 , 8 ), facecolor = '1' )
29
+ marg = 0.15
30
+ ax = fig .add_axes ([marg , marg , 1 - 1.8 * marg , 1 - 1.8 * marg ], aspect = 1 ,
31
+ facecolor = '1' )
22
32
23
33
24
34
def minor_tick (x , pos ):
@@ -36,110 +46,107 @@ def minor_tick(x, pos):
36
46
ax .set_xlim (0 , 4 )
37
47
ax .set_ylim (0 , 4 )
38
48
39
- ax .tick_params (which = 'major' , width = 1.0 )
<
8000
td data-grid-cell-id="diff-f9d7b814ebd24dbb697ded360e7adf20b8afc6b241239c8ad5ce7b73bf28c981-40-48-0" data-selected="false" role="gridcell" style="background-color:var(--diffBlob-deletionNum-bgColor, var(--diffBlob-deletion-bgColor-num));text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative left-side">40
- ax .tick_params (which = 'major' , length = 10 )
49
+ ax .tick_params (which = 'major' , width = 1.0 , labelsize = 14 )
50
+ ax .tick_params (which = 'major' , length = 10 , labelsize = 14 )
41
51
ax .tick_params (which = 'minor' , width = 1.0 , labelsize = 10 )
42
52
ax .tick_params (which = 'minor' , length = 5 , labelsize = 10 , labelcolor = '0.25' )
43
53
44
54
ax .grid (linestyle = "--" , linewidth = 0.5 , color = '.25' , zorder = - 10 )
45
55
46
- ax .plot (X , Y1 , c = (0.25 , 0.25 , 1.00 ), lw = 2 , label = "Blue signal" , zorder = 10 )
47
- ax .plot (X , Y2 , c = (1.00 , 0.25 , 0.25 ), lw = 2 , label = "Red signal" )
48
- ax .plot (X , Y3 , linewidth = 0 ,
49
- marker = 'o' , markerfacecolor = 'w' , markeredgecolor = 'k' )
56
+ ax .plot (X , Y1 , c = 'C0' , lw = 2.5 , label = "Blue signal" , zorder = 10 )
57
+ ax .plot (X , Y2 , c = 'C1' , lw = 2.5 , label = "Orange signal" )
58
+ ax .plot (X [::3 ], Y3 [::3 ], linewidth = 0 , markersize = 9 ,
59
+ marker = 's' , markerfacecolor = 'none' , markeredgecolor = 'C4' ,
60
+ markeredgewidth = 2.5 )
50
61
51
62
ax .set_title ("Anatomy of a figure" , fontsize = 20 , verticalalignment = 'bottom' )
52
- ax .set_xlabel ("X axis label" )
53
- ax .set_ylabel ("Y axis label" )
63
+ ax .set_xlabel ("x Axis label" , fontsize = 14 )
64
+ ax .set_ylabel ("y Axis label" , fontsize = 14 )
65
+
66
+ ax .legend (loc = "upper right" , fontsize = 14 )
54
67
55
- ax . legend ( loc = "upper right" )
68
+ # Annotate the figure
56
69
57
70
58
- def circle (x , y , radius = 0.15 ):
59
- from matplotlib .patches import Circle
60
- from matplotlib .patheffects import withStroke
61
- circle = Circle ((x , y ), radius , clip_on = False , zorder = 10 , linewidth = 1 ,
62
- edgecolor = 'black' , facecolor = (0 , 0 , 0 , .0125 ),
63
- path_effects = [withStroke (linewidth = 5 , foreground = 'w' )])
64
- ax .add_artist (circle )
71
+ def just_circle (x , y , radius = 0.15 ):
72
+ c = Circle ((x , y ), radius , clip_on = False , zorder = 10 , linewidth = 2.5 ,
73
+ edgecolor = royal_blue + [0.6 ], facecolor = 'none' ,
74
+ path_effects = [withStroke (linewidth = 7 , foreground = (1 , 1 , 1 , 1 ))])
75
+ ax .add_artist (c )
65
76
66
77
67
78
def text (x , y , text ):
68
- ax .text (x , y , text , backgroundcolor = "white" ,
69
- ha = 'center' , va = 'top' , weight = 'bold' , color = 'blue' )
79
+ ax .text (x , y , text , zorder = 100 ,
80
+ ha = 'center' , va = 'top' , weight = 'bold' , color = royal_blue ,
81
+ style = 'italic' , fontfamily = 'monospace' ,
82
+ path_effects = [withStroke (linewidth = 7 , foreground = (1 , 1 , 1 , 1 ))])
70
83
71
84
72
- # Minor tick
73
- circle (0.50 , - 0.10 )
74
- text (0.50 , - 0.32 , "Minor tick label" )
85
+ def code (x , y , text ):
86
+ ax .text (x , y , text , zorder = 100 ,
87
+ ha = 'center' , va = 'top' , weight = 'normal' , color = '0.0' ,
88
+ fontfamily = 'Courier New' , fontsize = 'medium' ,
89
+ path_effects = [withStroke (linewidth = 7 , foreground = (1 , 1 , 1 , 1 ))])
90
+
91
+
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" )
75
100
76
101
# Major tick
77
- circle (- 0.03 , 4.00 )
78
- text (0.03 , 3.80 , "Major tick" )
102
+ circle (- 0.03 , 1.05 , "Major tick" , "ax.yaxis.set_major_locator" )
79
103
80
104
# Minor tick
81
- circle ( 0.00 , 3.50 )
82
- text (0.00 , 3.30 , "Minor tick" )
105
+ y = 3.75
106
+ circle (0.00 , 3.75 , "Minor tick" , "ax.yaxis.set_minor_locator " )
83
107
84
108
# Major tick label
85
- circle (- 0.15 , 3.00 )
86
- text (- 0.15 , 2.80 , "Major tick label" )
109
+ circle (- 0.15 , 3.00 , "Major tick label" , "ax.yaxis.set_major_formatter" )
<
F438
/tr>87
110
88
111
# X Label
89
- circle (1.80 , - 0.27 )
90
- text (1.80 , - 0.45 , "X axis label" )
112
+ circle (1.90 , - 0.32 , "xlabel" , "ax.set_xlabel" )
91
113
92
114
# Y Label
93
- circle (- 0.27 , 1.80 )
94
- text (- 0.27 , 1.6 , "Y axis label" )
115
+ circle (- 0.27 , 1.68 , "ylabel" , "ax.set_ylabel" )
95
116
96
117
# Title
97
- circle (1.60 , 4.13 )
98
- text (1.60 , 3.93 , "Title" )
118
+ circle (1.58 , 4.13 , "Title" , "ax.set_title" )
99
119
100
120
# Blue plot
101
- circle (1.75 , 2.80 )
102
- text (1.75 , 2.60 , "Line\n (line plot)" )
103
-
104
- # Red plot
105
- circle (1.20 , 0.60 )
106
- text (1.20 , 0.40 , "Line\n (line plot)" )
121
+ circle (1.75 , 2.80 , "Line" , "ax.plot" )
107
122
108
123
# Scatter plot
109
- circle (3.20 , 1.75 )
110
- text (3.20 , 1.55 , "Markers\n (scatter plot)" )
124
+ circle (2.25 , 1.54 , "Markers" , "ax.scatter" )
111
125
112
126
# Grid
113
- circle (3.00 , 3.00 )
114
- text (3.00 , 2.80 , "Grid" )
127
+ circle (3.00 , 3.00 , "Grid" , "ax.grid" )
115
128
116
129
# Legend
117
- circle (3.70 , 3.80 )
118
- text (3.70 , 3.60 , "Legend" )
130
+ circle (3.60 , 3.65 , "Legend" , "ax.legend" )
119
131
120
132
# Axes
121
- circle (0.5 , 0.5 )
122
- text (0.5 , 0.3 , "Axes" )
133
+ circle (2.5 , 0.55 , "Axes" , "fig.subplots" )
123
134
124
135
# Figure
125
- circle (- 0.3 , 0.65 )
126
- text (- 0.3 , 0.45 , "Figure" )
127
-
128
- color = 'blue'
129
- ax .annotate ('Spines' , xy = (4.0 , 0.35 ), xytext = (3.3 , 0.5 ),
130
- weight = 'bold' , color = color ,
131
- arrowprops = dict (arrowstyle = '->' ,
132
- connectionstyle = "arc3" ,
133
- color = color ))
134
-
135
- ax .annotate ('' , xy = (3.15 , 0.0 ), xytext = (3.45 , 0.45 ),
136
- weight = 'bold' , color = color ,
137
- arrowprops = dict (arrowstyle = '->' ,
138
- connectionstyle = "arc3" ,
139
- color = color ))
140
-
141
- ax .text (4.0 , - 0.4 , "Made with https://matplotlib.org" ,
142
- fontsize = 10 , ha = "right" , color = '.5' )
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 ))
143
150
144
151
plt .show ()
145
152
0 commit comments