69
69
import matplotlib .pyplot as plt
70
70
71
71
fig = plt .figure ()
72
- fig .suptitle ('bold figure suptitle' , fontsize = 14 , fontweight = 'bold' )
73
-
74
72
ax = fig .add_subplot (111 )
75
73
fig .subplots_adjust (top = 0.85 )
74
+
75
+ # Set titles for the figure and the subplot respectively
76
+ fig .suptitle ('bold figure suptitle' , fontsize = 14 , fontweight = 'bold' )
76
77
ax .set_title ('axes title' )
77
78
78
79
ax .set_xlabel ('xlabel' )
79
80
ax .set_ylabel ('ylabel' )
80
81
82
+ # Set both x- and y-axis limits to [0, 10] instead of default [0, 1]
83
+ ax .axis ([0 , 10 , 0 , 10 ])
84
+
81
85
ax .text (3 , 8 , 'boxed italics text in data coords' , style = 'italic' ,
82
86
bbox = {'facecolor' : 'red' , 'alpha' : 0.5 , 'pad' : 10 })
83
87
90
94
transform = ax .transAxes ,
91
95
color = 'green' , fontsize = 15 )
92
96
93
-
94
97
ax .plot ([2 ], [1 ], 'o' )
95
98
ax .annotate ('annotate' , xy = (2 , 1 ), xytext = (3 , 4 ),
96
99
arrowprops = dict (facecolor = 'black' , shrink = 0.05 ))
97
100
98
- ax .axis ([0 , 10 , 0 , 10 ])
99
-
100
101
plt .show ()
101
102
102
103
###############################################################################
157
158
fig , ax = plt .subplots (figsize = (5 , 3 ))
158
159
fig .subplots_adjust (bottom = 0.15 , left = 0.2 )
159
160
ax .plot (x1 , y1 )
160
- ax .set_xlabel ('time [s]' , position = (0. , 1e6 ),
161
- horizontalalignment = 'left' )
161
+ ax .set_xlabel ('time [s]' , position = (0. , 1e6 ), horizontalalignment = 'left' )
162
162
ax .set_ylabel ('Damped oscillation [V]' )
163
163
164
164
plt .show ()
226
226
# ====================
227
227
#
228
228
# Placing ticks and ticklabels is a very tricky aspect of making a figure.
229
- # Matplotlib does the best it can automatically, but it also offers a very
230
- # flexible framework for determining the choices for tick locations, and
231
- # how they are labelled.
229
+ # Matplotlib does its best to accomplish the task automatically, but it also
230
+ # offers a very flexible framework for determining the choices for tick
231
+ # locations, and how they are labelled.
232
232
#
233
233
# Terminology
234
234
# ~~~~~~~~~~~
235
235
#
236
- # *Axes* have an `matplotlib.axis` object for the ``ax.xaxis``
237
- # and ``ax.yaxis`` that
238
- # contain the information about how the labels in the axis are laid out.
236
+ # *Axes* have a `matplotlib.axis` object for the ``ax.xaxis`` and ``ax.yaxis``
237
+ # that contain the information about how the labels in the axis are laid out.
239
238
#
240
239
# The axis API is explained in detail in the documentation to
241
240
# `~matplotlib.axis`.
242
241
#
243
- # An Axis object has major and minor ticks. The Axis has a
242
+ # An Axis object has major and minor ticks. The Axis has
244
243
# `matplotlib.xaxis.set_major_locator` and
245
244
# `matplotlib.xaxis.set_minor_locator` methods that use the data being plotted
246
245
# to determine
247
246
# the location of major and minor ticks. There are also
248
247
# `matplotlib.xaxis.set_major_formatter` and
249
- # `matplotlib.xaxis.set_minor_formatters ` methods that format the tick labels.
248
+ # `matplotlib.xaxis.set_minor_formatter ` methods that format the tick labels.
250
249
#
251
250
# Simple ticks
252
251
# ~~~~~~~~~~~~
253
252
#
254
253
# It often is convenient to simply define the
255
254
# tick values, and sometimes the tick labels, overriding the default
256
- # locators and formatters. This is discouraged because it breaks itneractive
255
+ # locators and formatters. This is discouraged because it breaks interactive
257
256
# navigation of the plot. It also can reset the axis limits: note that
258
257
# the second plot has the ticks we asked for, including ones that are
259
258
# well outside the automatic view limits.
285
284
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
286
285
#
287
286
# Instead of making a list of all the tickalbels, we could have
288
- # used a `matplotlib.ticker.FormatStrFormatter` and passed it to the
289
- # ``ax.xaxis``
287
+ # used `matplotlib.ticker.StrMethodFormatter` (new-style ``str.format()``
288
+ # format string) or `matplotlib.ticker.FormatStrFormatter` (old-style '%'
289
+ # format string) and passed it to the ``ax.xaxis``.
290
290
291
291
fig , axs = plt .subplots (2 , 1 , figsize = (5 , 3 ), tight_layout = True )
292
292
axs [0 ].plot (x1 , y1 )
@@ -362,6 +362,7 @@ def formatoddticks(x, pos):
362
362
else :
363
363
return ''
364
364
365
+
365
366
fig , ax = plt .subplots (figsize = (5 , 3 ), tight_layout = True )
366
367
ax .plot (x1 , y1 )
367
368
formatter = matplotlib .ticker .FuncFormatter (formatoddticks )
@@ -383,17 +384,17 @@ def formatoddticks(x, pos):
383
384
#
384
385
# A simple example is as follows. Note how we have to rotate the
385
386
# tick labels so that they don't over-run each other.
387
+
386
388
import datetime
387
389
388
390
fig , ax = plt .subplots (figsize = (5 , 3 ), tight_layout = True )
389
391
base = datetime .datetime (2017 , 1 , 1 , 0 , 0 , 1 )
390
- time = [base + datetime .timedelta (days = x ) for x in range (len (y1 ))]
392
+ time = [base + datetime .timedelta (days = x ) for x in range (len (x1 ))]
391
393
392
394
ax .plot (time , y1 )
393
395
ax .tick_params (axis = 'x' , rotation = 70 )
394
396
plt .show ()
395
397
396
-
397
398
##############################################################################
398
399
# We can pass a format
399
400
# to `matplotlib.dates.DateFormatter`. Also note that the 29th and the
0 commit comments