|
23 | 23 | ===============
|
24 | 24 |
|
25 | 25 | 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`. |
29 | 29 |
|
30 | 30 | In the following example, the x-axis gains a converter that converts from
|
31 | 31 | `numpy.datetime64` to float, and a locator that put ticks at the beginning of
|
|
49 | 49 | # Note that if we try to plot a float on the x-axis, it will be plotted in
|
50 | 50 | # units of days since the "epoch" for the converter, in this case 1970-01-01
|
51 | 51 | # (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): |
54 | 54 |
|
55 | 55 | fig, ax = plt.subplots(figsize=(5.4, 2), layout='constrained')
|
56 | 56 | time = np.arange('1980-01-01', '1980-06-25', dtype='datetime64[D]')
|
|
60 | 60 | ax.plot(0, 0, 'd')
|
61 | 61 | ax.text(0, 0, ' Float x=0', rotation=45)
|
62 | 62 |
|
63 |
| - |
64 | 63 | # %%
|
65 | 64 | #
|
66 | 65 | # We can customize the locator and the formatter; see :ref:`date-locators` and
|
|
80 | 79 | # %%
|
81 | 80 | #
|
82 | 81 | # 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. |
88 | 86 |
|
89 | 87 | plt.rcParams['date.converter'] = 'concise'
|
90 | 88 |
|
|
95 | 93 |
|
96 | 94 | # %%
|
97 | 95 | #
|
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`. |
101 | 100 |
|
102 | 101 | fig, axs = plt.subplots(2, 1, figsize=(5.4, 3), layout='constrained')
|
103 | 102 | for ax in axs.flat:
|
|
135 | 134 | #
|
136 | 135 | # Note that the "categories" are plotted in the order that they are first
|
137 | 136 | # 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): |
139 | 139 |
|
140 | 140 | fig, ax = plt.subplots(figsize=(5, 3), layout='constrained')
|
141 | 141 | ax.bar(names, values)
|
142 | 142 |
|
143 | 143 | # plot in a different order:
|
144 | 144 | ax.scatter(['lemon', 'apple'], [7, 12])
|
145 | 145 |
|
146 |
| -# add a new category, and out of order: |
| 146 | +# add a new category, "pear", and put the other categories in a different order: |
147 | 147 | ax.plot(['pear', 'orange', 'apple', 'lemon'], [13, 10, 7, 12], color='C1')
|
148 | 148 |
|
149 | 149 |
|
|
154 | 154 | # specified.
|
155 | 155 | #
|
156 | 156 | # 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. |
159 | 162 |
|
160 | 163 | fig, ax = plt.subplots(figsize=(5, 3), layout='constrained')
|
161 | 164 | ax.bar(names, values)
|
|
220 | 223 | # ======================================================
|
221 | 224 | #
|
222 | 225 | # 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`` |
224 | 227 | # property on the axis. We can also query the formatters and locators using
|
225 | 228 | # `~.axis.Axis.get_major_locator` and `~.axis.Axis.get_major_formatter`.
|
226 | 229 | #
|
|
256 | 259 |
|
257 | 260 | # %%
|
258 | 261 | #
|
259 |
| -# General unit support |
260 |
| -# ==================== |
| 262 | +# More about "unit" support |
| 263 | +# ========================= |
261 | 264 | #
|
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 |
264 | 267 | # :ref:`basic_units` example.
|
265 | 268 | #
|
266 | 269 | # Unit support works by querying the type of data passed to the plotting
|
|
274 | 277 |
|
275 | 278 | # %%
|
276 | 279 | #
|
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