Skip to content

Commit 1817f3b

Browse files
committed
DOC: add units to user/explain [ci doc]
1 parent dd2dc24 commit 1817f3b

File tree

1 file changed

+29
-25
lines changed

1 file changed

+29
-25
lines changed

galleries/users_explain/axes/axes_units.py

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
===============
2424
2525
If ``x`` and/or ``y`` are a list of `datetime` or an array of
26-
`numpy.datetime64`, Matplotlib has a built in converter that will convert the
27-
datetime to a float, and add locators and formatters to the axis that are
28-
appropriate for dates.
26+
`numpy.datetime64`, Matplotlib has a built-in converter that will convert the
27+
datetime to a float, and add tick locators and formatters to the axis that are
28+
appropriate for dates. See `matplotlib.dates`.
2929
3030
In the following example, the x-axis gains a converter that converts from
3131
`numpy.datetime64` to float, and a locator that put ticks at the beginning of
@@ -49,8 +49,8 @@
4949
# Note that if we try to plot a float on the x-axis, it will be plotted in
5050
# units of days since the "epoch" for the converter, in this case 1970-01-01
5151
# (see :ref:`date-format`). So when we plot the value 0, the ticks start at
52-
# 1970-01-01, and note that the locator now chooses every two years for a tick
53-
# instead of every month:
52+
# 1970-01-01. (The locator also now chooses every two years for a tick instead
53+
# of every month):
5454

5555
fig, ax = plt.subplots(figsize=(5.4, 2), layout='constrained')
5656
time = np.arange('1980-01-01', '1980-06-25', dtype='datetime64[D]')
@@ -60,7 +60,6 @@
6060
ax.plot(0, 0, 'd')
6161
ax.text(0, 0, ' Float x=0', rotation=45)
6262

63-
6463
# %%
6564
#
6665
# We can customize the locator and the formatter; see :ref:`date-locators` and
@@ -80,11 +79,10 @@
8079
# %%
8180
#
8281
# The default locator is the `~.dates.AutoDateLocator`, and the default
83-
# Formatter `~.dates.AutoDateFormatter`. There is also a "concise"
84-
# formatter/locator that gives a more compact labelling, and can be set via
85-
# rcParams. Note how instead of the redundant "Jan" label at the start of the
86-
# year, "1980" is used instead. See :ref:`date_concise_formatter` for more
87-
# examples.
82+
# Formatter `~.dates.AutoDateFormatter`. There are also "concise" formatter
83+
# and locators that give a more compact labelling, and can be set via rcParams.
84+
# Note how instead of the redundant "Jan" label at the start of the year,
85+
# "1980" is used instead. See :ref:`date_concise_formatter` for more examples.
8886

8987
plt.rcParams['date.converter'] = 'concise'
9088

@@ -95,9 +93,10 @@
9593

9694
# %%
9795
#
98-
# We can set the limits on the axis either by passing the appropriate dates in
99-
# or by passing a floating point value in the proper units of floating days
100-
# since the epoch. We can get this value from `~.dates.date2num`.
96+
# We can set the limits on the axis either by passing the appropriate dates as
97+
# limits, or by passing a floating-point value in the proper units of days
98+
# since the epoch. If we need it, we can get this value from
99+
# `~.dates.date2num`.
101100

102101
fig, axs = plt.subplots(2, 1, figsize=(5.4, 3), layout='constrained')
103102
for ax in axs.flat:
@@ -135,15 +134,16 @@
135134
#
136135
# Note that the "categories" are plotted in the order that they are first
137136
# specified and that subsequent plotting in a different order will not affect
138-
# the original order. Further, new additions will be added on the end:
137+
# the original order. Further, new additions will be added on the end (see
138+
# "pear" below):
139139

140140
fig, ax = plt.subplots(figsize=(5, 3), layout='constrained')
141141
ax.bar(names, values)
142142

143143
# plot in a different order:
144144
ax.scatter(['lemon', 'apple'], [7, 12])
145145

146-
# add a new category, and out of order:
146+
# add a new category, "pear", and put the other categories in a different order:
147147
ax.plot(['pear', 'orange', 'apple', 'lemon'], [13, 10, 7, 12], color='C1')
148148

149149

@@ -154,8 +154,11 @@
154154
# specified.
155155
#
156156
# The category converter maps from categories to integers, starting at zero. So
157-
# data can also be manually added to the axis using a float. However, note
158-
# that a float that is not a category will not get a label.
157+
# data can also be manually added to the axis using a float. Note that if a
158+
# float is passed in that does not have a "category" associated with it, the
159+
# data point can still be plotted, but a tick will not be created. In the
160+
# following, we plot data at 4.0 and 2.5, but no tick is added there because
161+
# those are not categories.
159162

160163
fig, ax = plt.subplots(figsize=(5, 3), layout='constrained')
161164
ax.bar(names, values)
@@ -220,7 +223,7 @@
220223
# ======================================================
221224
#
222225
# Sometimes it is helpful to be able to debug what Matplotlib is using to
223-
# convert the incoming data: we can do that by querying the ``converter``
226+
# convert the incoming data. We can do that by querying the ``converter``
224227
# property on the axis. We can also query the formatters and locators using
225228
# `~.axis.Axis.get_major_locator` and `~.axis.Axis.get_major_formatter`.
226229
#
@@ -256,11 +259,11 @@
256259

257260
# %%
258261
#
259-
# General unit support
260-
# ====================
262+
# More about "unit" support
263+
# =========================
261264
#
262-
# The support for dates and categories is part of "units" support that is
263-
# built into Matplotlib. This is described at `.matplotlib.units` and in the #
265+
# The support for dates and categories is part of "units" support that is built
266+
# into Matplotlib. This is described at `.matplotlib.units` and in the
264267
# :ref:`basic_units` example.
265268
#
266269
# Unit support works by querying the type of data passed to the plotting
@@ -274,5 +277,6 @@
274277

275278
# %%
276279
#
277-
# Downstream libraries like pandas, astropy, and pint all can add their own
278-
# converters to Matplotlib.
280+
# Downstream libraries like `pandas <https://pandas.pydata.org>`_,
281+
# `astropy <https://www.astropy.org>`, `pint <https://pint.readthedocs.io>`
282+
# and others supply their own converters that can be used with Matplotlib.

0 commit comments

Comments
 (0)