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.13
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,42 +46,45 @@ 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 )
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 )
54
65
55
- ax .legend (loc = "upper right" )
66
+ ax .legend (loc = "upper right" , fontsize = 14 )
67
+
68
+ # Annotate the figure
56
69
57
70
58
71
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 )
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'
6D47
, 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
85
# Minor tick
73
- circle (0.50 , - 0.10 )
74
- text (0.50 , - 0.32 , "Minor tick label" )
86
+ circle (3.25 , - 0.10 )
87
+ text (3.25 , - 0.32 , "Minor tick label" )
75
88
76
89
# Major tick
77
90
circle (- 0.03 , 4.00 )
@@ -86,60 +99,55 @@ def text(x, y, text):
86
99
text (- 0.15 , 2.80 , "Major tick label" )
87
100
88
101
# X Label
89
- circle (1.80 , - 0.27 )
90
- text (1.80 , - 0.45 , "X axis label " )
102
+ circle (1.90 , - 0.27 )
103
+ text (1.90 , - 0.47 , "xlabel " )
91
104
92
105
# Y Label
93
- circle (- 0.27 , 1.80 )
94
- text (- 0.27 , 1.6 , "Y axis label " )
106
+ circle (- 0.27 , 1.75 )
107
+ text (- 0.27 , 1.55 , "ylabel " )
95
108
96
109
# Title
97
- circle (1.60 , 4.13 )
98
- text (1.60 , 3.93 , "Title" )
110
+ circle (1.58 , 4.13 )
111
+ text (1.58 , 3.93 , "Title" )
99
112
100
113
# Blue plot
101
114
circle (1.75 , 2.80 )
102
115
text (1.75 , 2.60 , "Line\n (line plot)" )
103
116
104
- # Red plot
105
- circle (1.20 , 0.60 )
106
- text (1.20 , 0.40 , "Line\n (line plot)" )
107
-
108
117
# Scatter plot
109
- circle (3.20 , 1.75 )
110
- text (3.20 , 1.55 , "Markers\n (scatter plot)" )
118
+ circle (2.25 , 1.54 )
119
+ text (2.25 , 1.34 , "Markers\n (scatter plot)" )
111
120
112
121
# Grid
113
122
circle (3.00 , 3.00 )
114
123
text (3.00 , 2.80 , "Grid" )
115
124
116
125
# Legend
117
- circle (3.70 , 3.80 )
118
- text (3.70 , 3.60 , "Legend" )
126
+ circle (3.60 , 3.65 )
127
+ text (3.60 , 3.45 , "Legend" )
119
128
120
129
# Axes
121
130
circle (0.5 , 0.5 )
122
131
text (0.5 , 0.3 , "Axes" )
123
132
124
133
# 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' )
134
+ circle (4.185 , 4.3 )
135
+ text (4.185 , 4.1 , "Figure" )
136
+
137
+ # Axis
138
+ circle (0.65 , 0.01 )
139
+ text (0.65 , - 0.2 , "x Axis" )
140
+
141
+ # Axis
142
+ circle (0 , 0.7 )
143
+ text (0 , 0.7 - 0.2 , "y Axis" )
144
+
145
+ # Spine
146
+ circle (4.0 , 0.7 )
147
+ text (4.0 , 0.7 - 0.2 , "Spine" )
148
+
149
+ fig .add_artist (Rectangle ((0 , 0 ), width = 1 , height = 1 , facecolor = 'none' ,
150
+ edgecolor = '0.5' , linewidth = 10 ))
143
151
144
152
plt .show ()
145
153
0 commit comments