File: whats_new_3.9.0.rst

package info (click to toggle)
matplotlib 3.10.1%2Bdfsg1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 78,340 kB
  • sloc: python: 147,118; cpp: 62,988; objc: 1,679; ansic: 1,426; javascript: 786; makefile: 92; sh: 53
file content (409 lines) | stat: -rw-r--r-- 14,106 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
=============================================
What's new in Matplotlib 3.9.0 (May 15, 2024)
=============================================

For a list of all of the issues and pull requests since the last revision, see the
:ref:`github-stats-3-9-0`.

.. contents:: Table of Contents
   :depth: 4

.. toctree::
   :maxdepth: 4

Plotting and Annotation improvements
====================================

``Axes.inset_axes`` is no longer experimental
---------------------------------------------

`.Axes.inset_axes` is considered stable for use.

Legend support for Boxplot
--------------------------

Boxplots now support a *label* parameter to create legend entries. Legend labels can be
passed as a list of strings to label multiple boxes in a single `.Axes.boxplot` call:

.. plot::
    :include-source:
    :alt: Example of creating 3 boxplots and assigning legend labels as a sequence.

    np.random.seed(19680801)
    fruit_weights = [
        np.random.normal(130, 10, size=100),
        np.random.normal(125, 20, size=100),
        np.random.normal(120, 30, size=100),
    ]
    labels = ['peaches', 'oranges', 'tomatoes']
    colors = ['peachpuff', 'orange', 'tomato']

    fig, ax = plt.subplots()
    ax.set_ylabel('fruit weight (g)')

    bplot = ax.boxplot(fruit_weights,
                       patch_artist=True,  # fill with color
                       label=labels)

    # fill with colors
    for patch, color in zip(bplot['boxes'], colors):
        patch.set_facecolor(color)

    ax.set_xticks([])
    ax.legend()


Or as a single string to each individual `.Axes.boxplot`:

.. plot::
    :include-source:
    :alt: Example of creating 2 boxplots and assigning each legend label as a string.

    fig, ax = plt.subplots()

    data_A = np.random.random((100, 3))
    data_B = np.random.random((100, 3)) + 0.2
    pos = np.arange(3)

    ax.boxplot(data_A, positions=pos - 0.2, patch_artist=True, label='Box A',
               boxprops={'facecolor': 'steelblue'})
    ax.boxplot(data_B, positions=pos + 0.2, patch_artist=True, label='Box B',
               boxprops={'facecolor': 'lightblue'})

    ax.legend()

Percent sign in pie labels auto-escaped with ``usetex=True``
------------------------------------------------------------

It is common, with `.Axes.pie`, to specify labels that include a percent sign (``%``),
which denotes a comment for LaTeX. When enabling LaTeX with :rc:`text.usetex` or passing
``textprops={"usetex": True}``, this used to cause the percent sign to disappear.

Now, the percent sign is automatically escaped (by adding a preceding backslash) so that
it appears regardless of the ``usetex`` setting. If you have pre-escaped the percent
sign, this will be detected, and remain as is.

``hatch`` parameter for stackplot
---------------------------------

The `~.Axes.stackplot` *hatch* parameter now accepts a list of strings describing
hatching styles that will be applied sequentially to the layers in the stack:

.. plot::
    :include-source:
    :alt: Two charts, identified as ax1 and ax2, showing "stackplots", i.e. one-dimensional distributions of data stacked on top of one another. The first plot, ax1 has cross-hatching on all slices, having been given a single string as the "hatch" argument. The second plot, ax2 has different styles of hatching on each slice - diagonal hatching in opposite directions on the first two slices, cross-hatching on the third slice, and open circles on the fourth.

    fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(10,5))

    cols = 10
    rows = 4
    data = (
    np.reshape(np.arange(0, cols, 1), (1, -1)) ** 2
    + np.reshape(np.arange(0, rows), (-1, 1))
    + np.random.random((rows, cols))*5
    )
    x = range(data.shape[1])
    ax1.stackplot(x, data, hatch="x")
    ax2.stackplot(x, data, hatch=["//","\\","x","o"])

    ax1.set_title("hatch='x'")
    ax2.set_title("hatch=['//','\\\\','x','o']")

    plt.show()

Add option to plot only one half of violin plot
-----------------------------------------------

Setting the parameter *side* to 'low' or 'high' allows to only plot one half of the
`.Axes.violinplot`.

.. plot::
    :include-source:
    :alt: Three copies of a vertical violin plot; first in blue showing the default of both sides, followed by an orange copy that only shows the "low" (or left, in this case) side, and finally a green copy that only shows the "high" (or right) side.

    # Fake data with reproducible random state.
    np.random.seed(19680801)
    data = np.random.normal(0, 8, size=100)

    fig, ax = plt.subplots()

    ax.violinplot(data, [0], showmeans=True, showextrema=True)
    ax.violinplot(data, [1], showmeans=True, showextrema=True, side='low')
    ax.violinplot(data, [2], showmeans=True, showextrema=True, side='high')

    ax.set_title('Violin Sides Example')
    ax.set_xticks([0, 1, 2], ['Default', 'side="low"', 'side="high"'])
    ax.set_yticklabels([])

``axhline`` and ``axhspan`` on polar axes
-----------------------------------------

... now draw circles and circular arcs (`~.Axes.axhline`) or annuli and wedges
(`~.Axes.axhspan`).

.. plot::
    :include-source:
    :alt: A sample polar plot, that contains an axhline at radius 1, an axhspan annulus between radius 0.8 and 0.9, and an axhspan wedge between radius 0.6 and 0.7 and 288° and 324°.

    fig = plt.figure()
    ax = fig.add_subplot(projection="polar")
    ax.set_rlim(0, 1.2)

    ax.axhline(1, c="C0", alpha=.5)
    ax.axhspan(.8, .9, fc="C1", alpha=.5)
    ax.axhspan(.6, .7, .8, .9, fc="C2", alpha=.5)

Subplot titles can now be automatically aligned
-----------------------------------------------

Subplot axes titles can be misaligned vertically if tick labels or xlabels are placed at
the top of one subplot. The new `~.Figure.align_titles` method on the `.Figure` class
will now align the titles vertically.

.. plot::
    :include-source:
    :alt: A figure with two Axes side-by-side, the second of which with ticks on top. The Axes titles and x-labels appear unaligned with each other due to these ticks.

    fig, axs = plt.subplots(1, 2, layout='constrained')

    axs[0].plot(np.arange(0, 1e6, 1000))
    axs[0].set_title('Title 0')
    axs[0].set_xlabel('XLabel 0')

    axs[1].plot(np.arange(1, 0, -0.1) * 2000, np.arange(1, 0, -0.1))
    axs[1].set_title('Title 1')
    axs[1].set_xlabel('XLabel 1')
    axs[1].xaxis.tick_top()
    axs[1].tick_params(axis='x', rotation=55)

.. plot::
    :include-source:
    :alt: A figure with two Axes side-by-side, the second of which with ticks on top. Unlike the previous figure, the Axes titles and x-labels appear aligned.

    fig, axs = plt.subplots(1, 2, layout='constrained')

    axs[0].plot(np.arange(0, 1e6, 1000))
    axs[0].set_title('Title 0')
    axs[0].set_xlabel('XLabel 0')

    axs[1].plot(np.arange(1, 0, -0.1) * 2000, np.arange(1, 0, -0.1))
    axs[1].set_title('Title 1')
    axs[1].set_xlabel('XLabel 1')
    axs[1].xaxis.tick_top()
    axs[1].tick_params(axis='x', rotation=55)

    fig.align_labels()
    fig.align_titles()

``axisartist`` can now be used together with standard ``Formatters``
--------------------------------------------------------------------

... instead of being limited to axisartist-specific ones.

Toggle minorticks on Axis
-------------------------

Minor ticks on an `~matplotlib.axis.Axis` can be displayed or removed using
`~matplotlib.axis.Axis.minorticks_on` and `~matplotlib.axis.Axis.minorticks_off`; e.g.,
``ax.xaxis.minorticks_on()``. See also `~matplotlib.axes.Axes.minorticks_on`.

``StrMethodFormatter`` now respects ``axes.unicode_minus``
----------------------------------------------------------

When formatting negative values, `.StrMethodFormatter` will now use unicode minus signs
if :rc:`axes.unicode_minus` is set.

    >>> from matplotlib.ticker import StrMethodFormatter
    >>> with plt.rc_context({'axes.unicode_minus': False}):
    ...     formatter = StrMethodFormatter('{x}')
    ...     print(formatter.format_data(-10))
    -10

    >>> with plt.rc_context({'axes.unicode_minus': True}):
    ...     formatter = StrMethodFormatter('{x}')
    ...     print(formatter.format_data(-10))
    −10

Figure, Axes, and Legend Layout
===============================

Subfigures now have controllable zorders
----------------------------------------

Previously, setting the zorder of a subfigure had no effect, and those were plotted on
top of any figure-level artists (i.e for example on top of fig-level legends). Now,
subfigures behave like any other artists, and their zorder can be controlled, with
default a zorder of 0.

.. plot::
    :include-source:
    :alt: Example on controlling the zorder of a subfigure

    x = np.linspace(1, 10, 10)
    y1, y2 = x, -x
    fig = plt.figure(constrained_layout=True)
    subfigs = fig.subfigures(nrows=1, ncols=2)
    for subfig in subfigs:
        axarr = subfig.subplots(2, 1)
        for ax in axarr.flatten():
            (l1,) = ax.plot(x, y1, label="line1")
            (l2,) = ax.plot(x, y2, label="line2")
    subfigs[0].set_zorder(6)
    l = fig.legend(handles=[l1, l2], loc="upper center", ncol=2)

Getters for xmargin, ymargin and zmargin
----------------------------------------

`.Axes.get_xmargin`, `.Axes.get_ymargin` and `.Axes3D.get_zmargin` methods have been
added to return the margin values set by `.Axes.set_xmargin`, `.Axes.set_ymargin` and
`.Axes3D.set_zmargin`, respectively.

Mathtext improvements
=====================

``mathtext`` documentation improvements
---------------------------------------

The documentation is updated to take information directly from the parser. This means
that (almost) all supported symbols, operators, etc. are shown at :ref:`mathtext`.

``mathtext`` spacing corrections
--------------------------------

As consequence of the updated documentation, the spacing on a number of relational and
operator symbols were correctly classified and therefore will be spaced properly.

Widget Improvements
===================

Check and Radio Button widgets support clearing
-----------------------------------------------

The `.CheckButtons` and `.RadioButtons` widgets now support clearing their state by
calling their ``.clear`` method. Note that it is not possible to have no selected radio
buttons, so the selected option at construction time is selected.

3D plotting improvements
========================

Setting 3D axis limits now set the limits exactly
-------------------------------------------------

Previously, setting the limits of a 3D axis would always add a small margin to the
limits. Limits are now set exactly by default. The newly introduced rcparam
``axes3d.automargin`` can be used to revert to the old behavior where margin is
automatically added.

.. plot::
    :include-source:
    :alt: Example of the new behavior of 3D axis limits, and how setting the rcParam reverts to the old behavior.

    fig, axs = plt.subplots(1, 2, subplot_kw={'projection': '3d'})

    plt.rcParams['axes3d.automargin'] = True
    axs[0].set(xlim=(0, 1), ylim=(0, 1), zlim=(0, 1), title='Old Behavior')

    plt.rcParams['axes3d.automargin'] = False  # the default in 3.9.0
    axs[1].set(xlim=(0, 1), ylim=(0, 1), zlim=(0, 1), title='New Behavior')

Other improvements
==================

BackendRegistry
---------------

New :class:`~matplotlib.backends.registry.BackendRegistry` class is the single source of
truth for available backends. The singleton instance is
``matplotlib.backends.backend_registry``. It is used internally by Matplotlib, and also
IPython (and therefore Jupyter) starting with IPython 8.24.0.

There are three sources of backends: built-in (source code is within the Matplotlib
repository), explicit ``module://some.backend`` syntax (backend is obtained by loading
the module), or via an entry point (self-registering backend in an external package).

To obtain a list of all registered backends use:

    >>> from matplotlib.backends import backend_registry
    >>> backend_registry.list_all()

Add ``widths``, ``heights`` and ``angles`` setter to ``EllipseCollection``
--------------------------------------------------------------------------

The ``widths``, ``heights`` and ``angles`` values of the
`~matplotlib.collections.EllipseCollection` can now be changed after the collection has
been created.

.. plot::
    :include-source:

    from matplotlib.collections import EllipseCollection

    rng = np.random.default_rng(0)

    widths = (2, )
    heights = (3, )
    angles = (45, )
    offsets = rng.random((10, 2)) * 10

    fig, ax = plt.subplots()

    ec = EllipseCollection(
        widths=widths,
        heights=heights,
        angles=angles,
        offsets=offsets,
        units='x',
        offset_transform=ax.transData,
        )

    ax.add_collection(ec)
    ax.set_xlim(-2, 12)
    ax.set_ylim(-2, 12)

    new_widths = rng.random((10, 2)) * 2
    new_heights = rng.random((10, 2)) * 3
    new_angles = rng.random((10, 2)) * 180

    ec.set(widths=new_widths, heights=new_heights, angles=new_angles)

``image.interpolation_stage`` rcParam
-------------------------------------

This new rcParam controls whether image interpolation occurs in "data" space or in
"rgba" space.

Arrow patch position is now modifiable
--------------------------------------

A setter method has been added that allows updating the position of the `.patches.Arrow`
object without requiring a full re-draw.

.. plot::
    :include-source:
    :alt: Example of changing the position of the arrow with the new ``set_data`` method.

    from matplotlib import animation
    from matplotlib.patches import Arrow

    fig, ax = plt.subplots()
    ax.set_xlim(0, 10)
    ax.set_ylim(0, 10)

    a = Arrow(2, 0, 0, 10)
    ax.add_patch(a)


    # code for modifying the arrow
    def update(i):
        a.set_data(x=.5, dx=i, dy=6, width=2)


    ani = animation.FuncAnimation(fig, update, frames=15, interval=90, blit=False)

    plt.show()

NonUniformImage now has mouseover support
-----------------------------------------

When mousing over a `~matplotlib.image.NonUniformImage`, the data values are now
displayed.