88Text in Matplotlib
99==================
1010
11- Introduction to plotting and working with text in Matplotlib.
12-
1311Matplotlib has extensive text support, including support for
1412mathematical expressions, truetype support for raster and
1513vector outputs, newline separated text with arbitrary
124122fig , ax = plt .subplots (figsize = (5 , 3 ))
125123fig .subplots_adjust (bottom = 0.15 , left = 0.2 )
126124ax .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) ' )
129127
130128plt .show ()
131129
137135fig , ax = plt .subplots (figsize = (5 , 3 ))
138136fig .subplots_adjust (bottom = 0.15 , left = 0.2 )
139137ax .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) ' )
142140
143141plt .show ()
144142
145143# %%
146144# If you want to move the labels, you can specify the *labelpad* keyword
147145# argument, where the value is points (1/72", the same unit used to specify
148- # fontsizes ).
146+ # font sizes ).
149147
150148fig , ax = plt .subplots (figsize = (5 , 3 ))
151149fig .subplots_adjust (bottom = 0.15 , left = 0.2 )
152150ax .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 )
155153
156154plt .show ()
157155
158156# %%
159- # Or , the labels accept all the `.Text` keyword arguments, including
157+ # Alternatively , the labels accept all the `.Text` keyword arguments, including
160158# *position*, via which we can manually specify the label positions. Here we
161159# put the xlabel to the far left of the axis. Note, that the y-coordinate of
162160# this position has no effect - to adjust the y-position we need to use the
165163fig , ax = plt .subplots (figsize = (5 , 3 ))
166164fig .subplots_adjust (bottom = 0.15 , left = 0.2 )
167165ax .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) ' )
170168
171169plt .show ()
172170
173171# %%
174172# All the labelling in this tutorial can be changed by manipulating the
175173# `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`.
177175
178176from matplotlib .font_manager import FontProperties
179177
182180fig , ax = plt .subplots (figsize = (5 , 3 ))
183181fig .subplots_adjust (bottom = 0.15 , left = 0.2 )
184182ax .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 )
187185
188186plt .show ()
189187
194192fig , ax = plt .subplots (figsize = (5 , 3 ))
195193fig .subplots_adjust (bottom = 0.2 , left = 0.2 )
196194ax .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) $' )
199197plt .show ()
200198
201199
204202# ======
205203#
206204# 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") .
209207
210208fig , axs = plt .subplots (3 , 1 , figsize = (5 , 6 ), tight_layout = True )
211209locs = ['center' , 'left' , 'right' ]
212210for ax , loc in zip (axs , locs ):
213211 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 )
215213plt .show ()
216214
217215# %%
237235# Terminology
238236# ^^^^^^^^^^^
239237#
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
241239# ``ax.yaxis`` that contain the information about how the labels in the axis
242240# are laid out.
243241#
255253#
256254# It is often convenient to simply define the
257255# 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
261259# well outside the automatic view limits.
262260
263261fig , axs = plt .subplots (2 , 1 , figsize = (5 , 3 ), tight_layout = True )
283281plt .show ()
284282
285283# %%
286- # Tick Locators and Formatters
284+ # Tick locators and formatters
287285# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
288286#
289287# Instead of making a list of all the ticklabels, we could have
317315
318316# %%
319317# 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,
323321# as would 20, 40, 60 or 0.2, 0.4, 0.6. However, 3, 6, 9 would not be
324322# acceptable because 3 doesn't appear in the list of steps.
325323#
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
328326# ticklabel is taken into account, but the length of the tick string
329327# is not (because it's not yet known.) In the bottom row, the
330328# ticklabels are quite large, so we set ``nbins=4`` to make the
@@ -382,11 +380,11 @@ def formatoddticks(x, pos):
382380# Matplotlib can accept `datetime.datetime` and `numpy.datetime64`
383381# objects as plotting arguments. Dates and times require special
384382# 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 ,
386384# defined in the `matplotlib.dates` module.
387385#
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 .
390388
391389import datetime
392390
@@ -399,11 +397,10 @@ def formatoddticks(x, pos):
399397plt .show ()
400398
401399# %%
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.
407404
408405import matplotlib .dates as mdates
409406
@@ -418,9 +415,9 @@ def formatoddticks(x, pos):
418415plt .show ()
419416
420417# %%
421- # Legends and Annotations
418+ # Legends and annotations
422419# =======================
423420#
424- # - Legends: :ref:`legend_guide`
425- # - Annotations: :ref:`annotations`
421+ # - :ref:`legend_guide`
422+ # - :ref:`annotations`
426423#
0 commit comments