File: whats_new_3.7.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 (451 lines) | stat: -rw-r--r-- 14,792 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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
=============================================
What's new in Matplotlib 3.7.0 (Feb 13, 2023)
=============================================

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

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

.. toctree::
   :maxdepth: 4

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


``hatch`` parameter for pie
---------------------------

`~matplotlib.axes.Axes.pie` now accepts a *hatch* keyword that takes as input
a hatch or list of hatches:

.. plot::
    :include-source: true
    :alt: Two pie charts, identified as ax1 and ax2, both have a small blue slice, a medium orange slice, and a large green slice. ax1 has a dot hatching on the small slice, a small open circle hatching on the medium slice, and a large open circle hatching on the large slice. ax2 has the same large open circle with a dot hatch on every slice.

    fig, (ax1, ax2) = plt.subplots(ncols=2)
    x = [10, 30, 60]

    ax1.pie(x, hatch=['.', 'o', 'O'])
    ax2.pie(x, hatch='.O')

    ax1.set_title("hatch=['.', 'o', 'O']")
    ax2.set_title("hatch='.O'")


Polar plot errors drawn in polar coordinates
--------------------------------------------
Caps and error lines are now drawn with respect to polar coordinates,
when plotting errorbars on polar plots.

.. figure:: /gallery/pie_and_polar_charts/images/sphx_glr_polar_error_caps_001.png
   :target: ../../gallery/pie_and_polar_charts/polar_error_caps.html



Additional format string options in `~matplotlib.axes.Axes.bar_label`
---------------------------------------------------------------------

The ``fmt`` argument of `~matplotlib.axes.Axes.bar_label` now accepts
{}-style format strings:

.. plot::
    :include-source: true

    import matplotlib.pyplot as plt

    fruit_names = ['Coffee', 'Salted Caramel', 'Pistachio']
    fruit_counts = [4000, 2000, 7000]

    fig, ax = plt.subplots()
    bar_container = ax.bar(fruit_names, fruit_counts)
    ax.set(ylabel='pints sold', title='Gelato sales by flavor', ylim=(0, 8000))
    ax.bar_label(bar_container, fmt='{:,.0f}')

It also accepts callables:

.. plot::
    :include-source: true

    animal_names = ['Lion', 'Gazelle', 'Cheetah']
    mph_speed = [50, 60, 75]

    fig, ax = plt.subplots()
    bar_container = ax.bar(animal_names, mph_speed)
    ax.set(ylabel='speed in MPH', title='Running speeds', ylim=(0, 80))
    ax.bar_label(
        bar_container, fmt=lambda x: '{:.1f} km/h'.format(x * 1.61)
    )



``ellipse`` boxstyle option for annotations
-------------------------------------------

The ``'ellipse'`` option for boxstyle can now be used to create annotations
with an elliptical outline. It can be used as a closed curve shape for
longer texts instead of the ``'circle'`` boxstyle which can get quite big.

.. plot::
    :include-source: true

    import matplotlib.pyplot as plt
    fig, ax = plt.subplots(figsize=(5, 5))
    t = ax.text(0.5, 0.5, "elliptical box",
            ha="center", size=15,
            bbox=dict(boxstyle="ellipse,pad=0.3"))


The *extent* of ``imshow`` can now be expressed with units
----------------------------------------------------------
The *extent* parameter of `~.axes.Axes.imshow` and  `~.AxesImage.set_extent`
can now be expressed with units.

.. plot::
    :include-source: true

    import matplotlib.pyplot as plt
    import numpy as np

    fig, ax = plt.subplots(layout='constrained')
    date_first = np.datetime64('2020-01-01', 'D')
    date_last = np.datetime64('2020-01-11', 'D')

    arr = [[i+j for i in range(10)] for j in range(10)]

    ax.imshow(arr, origin='lower', extent=[0, 10, date_first, date_last])

    plt.show()

Reversed order of legend entries
--------------------------------
The order of legend entries can now be reversed by passing ``reverse=True`` to
`~.Axes.legend`.


``pcolormesh`` accepts RGB(A) colors
------------------------------------

The `~.Axes.pcolormesh` method can now handle explicit colors
specified with RGB(A) values. To specify colors, the array must be 3D
with a shape of ``(M, N, [3, 4])``.

.. plot::
    :include-source: true

    import matplotlib.pyplot as plt
    import numpy as np

    colors = np.linspace(0, 1, 90).reshape((5, 6, 3))
    plt.pcolormesh(colors)
    plt.show()




View current appearance settings for ticks, tick labels, and gridlines
----------------------------------------------------------------------

The new `~matplotlib.axis.Axis.get_tick_params` method can be used to
retrieve the appearance settings that will be applied to any
additional ticks, tick labels, and gridlines added to the plot:

.. code-block:: pycon

    >>> import matplotlib.pyplot as plt

    >>> fig, ax = plt.subplots()
    >>> ax.yaxis.set_tick_params(labelsize=30, labelcolor='red',
    ...                          direction='out', which='major')
    >>> ax.yaxis.get_tick_params(which='major')
    {'direction': 'out',
    'left': True,
    'right': False,
    'labelleft': True,
    'labelright': False,
    'gridOn': False,
    'labelsize': 30,
    'labelcolor': 'red'}
    >>> ax.yaxis.get_tick_params(which='minor')
    {'left': True,
    'right': False,
    'labelleft': True,
    'labelright': False,
    'gridOn': False}



Style files can be imported from third-party packages
-----------------------------------------------------

Third-party packages can now distribute style files that are globally available
as follows.  Assume that a package is importable as ``import mypackage``, with
a ``mypackage/__init__.py`` module.  Then a ``mypackage/presentation.mplstyle``
style sheet can be used as ``plt.style.use("mypackage.presentation")``.

The implementation does not actually import ``mypackage``, making this process
safe against possible import-time side effects.  Subpackages (e.g.
``dotted.package.name``) are also supported.


Improvements to 3D Plotting
===========================


3D plot pan and zoom buttons
----------------------------

The pan and zoom buttons in the toolbar of 3D plots are now enabled.
Unselect both to rotate the plot. When the zoom button is pressed,
zoom in by using the left mouse button to draw a bounding box, and
out by using the right mouse button to draw the box. When zooming a
3D plot, the current view aspect ratios are kept fixed.


*adjustable* keyword argument for setting equal aspect ratios in 3D
-------------------------------------------------------------------

While setting equal aspect ratios for 3D plots, users can choose to modify
either the data limits or the bounding box in parity with 2D Axes.

.. plot::
    :include-source: true

    import matplotlib.pyplot as plt
    import numpy as np
    from itertools import combinations, product

    aspects = ('auto', 'equal', 'equalxy', 'equalyz', 'equalxz')
    fig, axs = plt.subplots(1, len(aspects), subplot_kw={'projection': '3d'},
                            figsize=(12, 6))

    # Draw rectangular cuboid with side lengths [4, 3, 5]
    r = [0, 1]
    scale = np.array([4, 3, 5])
    pts = combinations(np.array(list(product(r, r, r))), 2)
    for start, end in pts:
        if np.sum(np.abs(start - end)) == r[1] - r[0]:
            for ax in axs:
                ax.plot3D(*zip(start*scale, end*scale), color='C0')

    # Set the aspect ratios
    for i, ax in enumerate(axs):
        ax.set_aspect(aspects[i], adjustable='datalim')
        # Alternatively: ax.set_aspect(aspects[i], adjustable='box')
        # which will change the box aspect ratio instead of axis data limits.
        ax.set_title(f"set_aspect('{aspects[i]}')")

    plt.show()


``Poly3DCollection`` supports shading
-------------------------------------

It is now possible to shade a `.Poly3DCollection`. This is useful if the
polygons are obtained from e.g. a 3D model.

.. plot::
    :include-source: true

    import numpy as np
    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d.art3d import Poly3DCollection

    # Define 3D shape
    block = np.array([
        [[1, 1, 0],
         [1, 0, 0],
         [0, 1, 0]],
        [[1, 1, 0],
         [1, 1, 1],
         [1, 0, 0]],
        [[1, 1, 0],
         [1, 1, 1],
         [0, 1, 0]],
        [[1, 0, 0],
         [1, 1, 1],
         [0, 1, 0]]
    ])

    ax = plt.subplot(projection='3d')
    pc = Poly3DCollection(block, facecolors='b', shade=True)
    ax.add_collection(pc)
    plt.show()



rcParam for 3D pane color
-------------------------

The rcParams :rc:`axes3d.xaxis.panecolor`, :rc:`axes3d.yaxis.panecolor`,
:rc:`axes3d.zaxis.panecolor` can be used to change the color of the background
panes in 3D plots. Note that it is often beneficial to give them slightly
different shades to obtain a "3D effect" and to make them slightly transparent
(alpha < 1).

.. plot::
    :include-source: true

    import matplotlib.pyplot as plt
    with plt.rc_context({'axes3d.xaxis.panecolor': (0.9, 0.0, 0.0, 0.5),
                         'axes3d.yaxis.panecolor': (0.7, 0.0, 0.0, 0.5),
                         'axes3d.zaxis.panecolor': (0.8, 0.0, 0.0, 0.5)}):
        fig = plt.figure()
        fig.add_subplot(projection='3d')




Figure and Axes Layout
======================

``colorbar`` now has a *location* keyword argument
--------------------------------------------------

The ``colorbar`` method now supports a *location* keyword argument to more
easily position the color bar. This is useful when providing your own inset
axes using the *cax* keyword argument and behaves similar to the case where
axes are not provided (where the *location* keyword is passed through).
*orientation* and *ticklocation* are no longer required as they are
determined by *location*. *ticklocation* can still be provided if the
automatic setting is not preferred. (*orientation* can also be provided but
must be compatible with the *location*.)

An example is:

.. plot::
    :include-source: true

    import matplotlib.pyplot as plt
    import numpy as np
    rng = np.random.default_rng(19680801)
    imdata = rng.random((10, 10))
    fig, ax = plt.subplots(layout='constrained')
    im = ax.imshow(imdata)
    fig.colorbar(im, cax=ax.inset_axes([0, 1.05, 1, 0.05]),
                 location='top')



Figure legends can be placed outside figures using constrained_layout
---------------------------------------------------------------------
Constrained layout will make space for Figure legends if they are specified
by a *loc* keyword argument that starts with the string "outside". The
codes are unique from axes codes, in that "outside upper right" will
make room at the top of the figure for the legend, whereas
"outside right upper" will make room on the right-hand side of the figure.
See :ref:`legend_guide` for details.


Per-subplot keyword arguments  in ``subplot_mosaic``
----------------------------------------------------

It is now possible to pass keyword arguments through to Axes creation in each
specific call to ``add_subplot`` in `.Figure.subplot_mosaic` and
`.pyplot.subplot_mosaic` :

.. plot::
   :include-source: true

   fig, axd = plt.subplot_mosaic(
       "AB;CD",
       per_subplot_kw={
           "A": {"projection": "polar"},
           ("C", "D"): {"xscale": "log"},
           "B": {"projection": "3d"},
       },
   )


This is particularly useful for creating mosaics with mixed projections, but
any keyword arguments can be passed through.


``subplot_mosaic`` no longer provisional
----------------------------------------

The API on `.Figure.subplot_mosaic` and `.pyplot.subplot_mosaic` are now
considered stable and will change under Matplotlib's normal deprecation
process.


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


Custom styling of button widgets
--------------------------------

Additional custom styling of button widgets may be achieved via the
*label_props* and *radio_props* arguments to `.RadioButtons`; and the
*label_props*, *frame_props*, and *check_props* arguments to `.CheckButtons`.

.. plot::
   :include-source: true

   from matplotlib.widgets import CheckButtons, RadioButtons

   fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(5, 2), width_ratios=[1, 2])
   default_rb = RadioButtons(ax[0, 0], ['Apples', 'Oranges'])
   styled_rb = RadioButtons(ax[0, 1], ['Apples', 'Oranges'],
                            label_props={'color': ['red', 'orange'],
                                         'fontsize': [16, 20]},
                            radio_props={'edgecolor': ['red', 'orange'],
                                         'facecolor': ['mistyrose', 'peachpuff']})

   default_cb = CheckButtons(ax[1, 0], ['Apples', 'Oranges'],
                             actives=[True, True])
   styled_cb = CheckButtons(ax[1, 1], ['Apples', 'Oranges'],
                            actives=[True, True],
                            label_props={'color': ['red', 'orange'],
                                         'fontsize': [16, 20]},
                            frame_props={'edgecolor': ['red', 'orange'],
                                         'facecolor': ['mistyrose', 'peachpuff']},
                            check_props={'color': ['darkred', 'darkorange']})

   ax[0, 0].set_title('Default')
   ax[0, 1].set_title('Stylized')


Blitting in Button widgets
--------------------------

The `.Button`, `.CheckButtons`, and `.RadioButtons` widgets now support
blitting for faster rendering, on backends that support it, by passing
``useblit=True`` to the constructor. Blitting is enabled by default on
supported backends.


Other Improvements
==================


Source links can be shown or hidden for each Sphinx plot directive
------------------------------------------------------------------
The :doc:`Sphinx plot directive </api/sphinxext_plot_directive_api>`
(``.. plot::``) now supports a ``:show-source-link:`` option to show or hide
the link to the source code for each plot. The default is set using the
``plot_html_show_source_link`` variable in :file:`conf.py` (which
defaults to True).



Figure hooks
------------

The new :rc:`figure.hooks` provides a mechanism to register
arbitrary customizations on pyplot figures; it is a list of
"dotted.module.name:dotted.callable.name" strings specifying functions
that are called on each figure created by `.pyplot.figure`; these
functions can e.g. attach callbacks or modify the toolbar.  See
:doc:`/gallery/user_interfaces/mplcvd` for an example of toolbar customization.


New & Improved Narrative Documentation
======================================
* Brand new :ref:`Animations <animations>` tutorial.
* New grouped and stacked `bar chart <../../gallery/index.html#lines_bars_and_markers>`_ examples.
* New section for new contributors and reorganized git instructions in the :ref:`contributing guide<contributing>`.
* Restructured :ref:`annotations` tutorial.