8
8
Text in Matplotlib
9
9
==================
10
10
11
- Introduction to plotting and working with text in Matplotlib.
12
-
13
11
Matplotlib has extensive text support, including support for
14
12
mathematical expressions, truetype support for raster and
15
13
vector outputs, newline separated text with arbitrary
124
122
fig , ax = plt .subplots (figsize = (5 , 3 ))
125
123
fig .subplots_adjust (bottom = 0.15 , left = 0.2 )
126
124
ax .plot (x1 , y1 )
127
- ax .set_xlabel ('Time [s] ' )
128
- ax .set_ylabel ('Damped oscillation [V] ' )
125
+ ax .set_xlabel ('Time (s) ' )
126
+ ax .set_ylabel ('Damped oscillation (V) ' )
129
127
130
128
plt .show ()
131
129
137
135
fig , ax = plt .subplots (figsize = (5 , 3 ))
138
136
fig .subplots_adjust (bottom = 0.15 , left = 0.2 )
139
137
ax .plot (x1 , y1 * 10000 )
140
- ax .set_xlabel ('Time [s] ' )
141
- ax .set_ylabel ('Damped oscillation [V] ' )
138
+ ax .set_xlabel ('Time (s) ' )
139
+ ax .set_ylabel ('Damped oscillation (V) ' )
142
140
143
141
plt .show ()
144
142
145
143
# %%
146
144
# If you want to move the labels, you can specify the *labelpad* keyword
147
145
# argument, where the value is points (1/72", the same unit used to specify
148
- # fontsizes ).
146
+ # font sizes ).
149
147
150
148
fig , ax = plt .subplots (figsize = (5 , 3 ))
151
149
fig .subplots_adjust (bottom = 0.15 , left = 0.2 )
152
150
ax .plot (x1 , y1 * 10000 )
153
- ax .set_xlabel ('Time [s] ' )
154
- ax .set_ylabel ('Damped oscillation [V] ' , labelpad = 18 )
151
+ ax .set_xlabel ('Time (s) ' )
152
+ ax .set_ylabel ('Damped oscillation (V) ' , labelpad = 18 )
155
153
156
154
plt .show ()
157
155
158
156
# %%
159
- # Or , the labels accept all the `.Text` keyword arguments, including
157
+ # Alternatively , the labels accept all the `.Text` keyword arguments, including
160
158
# *position*, via which we can manually specify the label positions. Here we
161
159
# put the xlabel to the far left of the axis. Note, that the y-coordinate of
162
160
# this position has no effect - to adjust the y-position we need to use the
165
163
fig , ax = plt .subplots (figsize = (5 , 3 ))
166
164
fig .subplots_adjust (bottom = 0.15 , left = 0.2 )
167
165
ax .plot (x1 , y1 )
168
- ax .set_xlabel ('Time [s] ' , position = (0. , 1e6 ), horizontalalignment = 'left' )
169
- ax .set_ylabel ('Damped oscillation [V] ' )
166
+ ax .set_xlabel ('Time (s) ' , position = (0. , 1e6 ), horizontalalignment = 'left' )
167
+ ax .set_ylabel ('Damped oscillation (V) ' )
170
168
171
169
plt .show ()
172
170
173
171
# %%
174
172
# All the labelling in this tutorial can be changed by manipulating the
175
173
# `matplotlib.font_manager.FontProperties` method, or by named keyword
176
- # arguments to `~matplotlib.axes.Axes.set_xlabel`
174
+ # arguments to `~matplotlib.axes.Axes.set_xlabel`.
177
175
178
176
from matplotlib .font_manager import FontProperties
179
177
182
180
fig , ax = plt .subplots (figsize = (5 , 3 ))
183
181
fig .subplots_adjust (bottom = 0.15 , left = 0.2 )
184
182
ax .plot (x1 , y1 )
185
- ax .set_xlabel ('Time [s] ' , fontsize = 'large' , fontweight = 'bold' )
186
- ax .set_ylabel ('Damped oscillation [V] ' , fontproperties = font )
183
+ ax .set_xlabel ('Time (s) ' , fontsize = 'large' , fontweight = 'bold' )
184
+ ax .set_ylabel ('Damped oscillation (V) ' , fontproperties = font )
187
185
188
186
plt .show ()
189
187
194
192
fig , ax = plt .subplots (figsize = (5 , 3 ))
195
193
fig .subplots_adjust (bottom = 0.2 , left = 0.2 )
196
194
ax .plot (x1 , np .cumsum (y1 ** 2 ))
197
- ax .set_xlabel ('Time [s] \n This was a long experiment' )
198
- ax .set_ylabel (r'$\int\ Y^2\ dt\ \ [ V^2 s] $' )
195
+ ax .set_xlabel ('Time (s) \n This was a long experiment' )
196
+ ax .set_ylabel (r'$\int\ Y^2\ dt\ \ ( V^2 s) $' )
199
197
plt .show ()
200
198
201
199
204
202
# ======
205
203
#
206
204
# Subplot titles are set in much the same way as labels, but there is
207
- # the *loc* keyword arguments that can change the position and justification
208
- # from the default value of ``loc= center`` .
205
+ # the *loc* keyword argument that can change the position and justification
206
+ # ( the default value is " center") .
209
207
210
208
fig , axs = plt .subplots (3 , 1 , figsize = (5 , 6 ), tight_layout = True )
211
209
locs = ['center' , 'left' , 'right' ]
212
210
for ax , loc in zip (axs , locs ):
213
211
ax .plot (x1 , y1 )
214
- ax .set_title ('Title with loc at ' + loc , loc = loc )
212
+ ax .set_title ('Title with loc at ' + loc , loc = loc )
215
213
plt .show ()
216
214
217
215
# %%
237
235
# Terminology
238
236
# ^^^^^^^^^^^
239
237
#
240
- # *Axes* have an `matplotlib.axis.Axis` object for the ``ax.xaxis`` and
238
+ # *Axes* have a `matplotlib.axis.Axis` object for the ``ax.xaxis`` and
241
239
# ``ax.yaxis`` that contain the information about how the labels in the axis
242
240
# are laid out.
243
241
#
255
253
#
256
254
# It is often convenient to simply define the
257
255
# tick values, and sometimes the tick labels, overriding the default
258
- # locators and formatters. This is discouraged because it breaks interactive
259
- # navigation of the plot. It also can reset the axis limits: note that
260
- # the second plot has the ticks we asked for, including ones that are
256
+ # locators and formatters. However, this is discouraged because it breaks
257
+ # interactive navigation of the plot. It also can reset the axis limits: note
258
+ # that the second plot has the ticks we asked for, including ones that are
261
259
# well outside the automatic view limits.
262
260
263
261
fig , axs = plt .subplots (2 , 1 , figsize = (5 , 3 ), tight_layout = True )
283
281
plt .show ()
284
282
285
283
# %%
286
- # Tick Locators and Formatters
284
+ # Tick locators and formatters
287
285
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
288
286
#
289
287
# Instead of making a list of all the ticklabels, we could have
317
315
318
316
# %%
319
317
# The default formatter is the `matplotlib.ticker.MaxNLocator` called as
320
- # ``ticker.MaxNLocator(self, nbins='auto', steps=[1, 2, 2.5, 5, 10])``
321
- # The * steps* keyword contains a list of multiples that can be used for
322
- # tick values. i.e. in this case, 2, 4, 6 would be acceptable ticks,
318
+ # ``ticker.MaxNLocator(self, nbins='auto', steps=[1, 2, 2.5, 5, 10])``.
319
+ # The `` steps`` argument contains a list of multiples that can be used for
320
+ # tick values. In this case, 2, 4, 6 would be acceptable ticks,
323
321
# as would 20, 40, 60 or 0.2, 0.4, 0.6. However, 3, 6, 9 would not be
324
322
# acceptable because 3 doesn't appear in the list of steps.
325
323
#
326
- # ``nbins=auto`` uses an algorithm to determine how many ticks will
327
- # be acceptable based on how long the axis is. The fontsize of the
324
+ # Setting ``nbins=auto`` uses an algorithm to determine how many ticks will
325
+ # be acceptable based on the axis length. The fontsize of the
328
326
# ticklabel is taken into account, but the length of the tick string
329
327
# is not (because it's not yet known.) In the bottom row, the
330
328
# ticklabels are quite large, so we set ``nbins=4`` to make the
@@ -382,11 +380,11 @@ def formatoddticks(x, pos):
382
380
# Matplotlib can accept `datetime.datetime` and `numpy.datetime64`
383
381
# objects as plotting arguments. Dates and times require special
384
382
# formatting, which can often benefit from manual intervention. In
385
- # order to help, dates have special Locators and Formatters ,
383
+ # order to help, dates have special locators and formatters ,
386
384
# defined in the `matplotlib.dates` module.
387
385
#
388
- # A simple example is as follows. Note how we have to rotate the
389
- # tick labels so that they don't over-run each other .
386
+ # The following simple example illustrates this concept. Note how we
387
+ # rotate the tick labels so that they don't overlap .
390
388
391
389
import datetime
392
390
@@ -399,11 +397,10 @@ def formatoddticks(x, pos):
399
397
plt .show ()
400
398
401
399
# %%
402
- # We can pass a format to `matplotlib.dates.DateFormatter`. Also note that the
403
- # 29th and the next month are very close together. We can fix this by using
404
- # the `.dates.DayLocator` class, which allows us to specify a list of days of
405
- # the month to use. Similar formatters are listed in the `matplotlib.dates`
406
- # module.
400
+ # We can pass a format to `matplotlib.dates.DateFormatter`. If two tick labels
401
+ # are very close together, we can use the `.dates.DayLocator` class, which
402
+ # allows us to specify a list of days of the month to use. Similar formatters
403
+ # are listed in the `matplotlib.dates` module.
407
404
408
405
import matplotlib .dates as mdates
409
406
@@ -418,9 +415,9 @@ def formatoddticks(x, pos):
418
415
plt .show ()
419
416
420
417
# %%
421
- # Legends and Annotations
418
+ # Legends and annotations
422
419
# =======================
423
420
#
424
- # - Legends: :ref:`legend_guide`
425
- # - Annotations: :ref:`annotations`
421
+ # - :ref:`legend_guide`
422
+ # - :ref:`annotations`
426
423
#
0 commit comments