17
17
import numpy as np
18
18
19
19
# sphinx_gallery_thumbnail_number = 3
20
- import matplotlib as mpl
21
20
22
21
# %%
23
22
#
29
28
# area where points can be specified in terms of x-y coordinates (or theta-r
30
29
# in a polar plot, x-y-z in a 3D plot, etc.). The simplest way of
31
30
# creating a Figure with an Axes is using `.pyplot.subplots`. We can then use
32
- # `.Axes.plot` to draw some data on the Axes:
31
+ # `.Axes.plot` to draw some data on the Axes, and `~.pyplot.show` to display
32
+ # the figure:
33
33
34
- fig , ax = plt .subplots () # Create a figure containing a single Axes.
34
+ fig , ax = plt .subplots () # Create a figure containing a single Axes.
35
35
ax .plot ([1 , 2 , 3 , 4 ], [1 , 4 , 2 , 3 ]) # Plot some data on the Axes.
36
+ plt .show () # Show the figure.
36
37
37
38
# %%
38
39
#
39
- # Note that to get this Figure to display, you may have to call ``plt.show()``,
40
- # depending on your backend. For more details of Figures and backends, see
41
- # :ref:`figure-intro` .
40
+ # Depending on the environment you are working in, ``plt.show()`` can be left
41
+ # out. This is for example the case with Jupyter notebooks, which
42
+ # automatically show all figures created in a code cell .
42
43
#
43
44
# .. _figure_parts:
44
45
#
54
55
#
55
56
# The **whole** figure. The Figure keeps
56
57
# track of all the child :class:`~matplotlib.axes.Axes`, a group of
57
- # 'special' Artists (titles, figure legends, colorbars, etc), and
58
+ # 'special' Artists (titles, figure legends, colorbars, etc. ), and
58
59
# even nested subfigures.
59
60
#
60
- # The easiest way to create a new Figure is with pyplot::
61
+ # Typically, you'll create a new Figure through one of the following
62
+ # functions::
61
63
#
62
- # fig = plt.figure() # an empty figure with no Axes
63
- # fig, ax = plt.subplots() # a figure with a single Axes
64
+ # fig = plt.figure() # an empty figure with no Axes
65
+ # fig, ax = plt.subplots() # a figure with a single Axes
64
66
# fig, axs = plt.subplots(2, 2) # a figure with a 2x2 grid of Axes
65
67
# # a figure with one Axes on the left, and two on the right:
66
68
# fig, axs = plt.subplot_mosaic([['left', 'right_top'],
67
69
# ['left', 'right_bottom']])
68
70
#
69
- # It is often convenient to create the Axes together with the Figure, but you
70
- # can also manually add Axes later on. Note that many
71
- # :ref:`Matplotlib backends <backends>` support zooming and
72
- # panning on figure windows.
71
+ # `~.pyplot.subplots()` and `~.pyplot.subplot_mosaic` are convenience functions
72
+ # that additionally create Axes objects inside the Figure, but you can also
73
+ # manually add Axes later on.
73
74
#
74
- # For more on Figures, see :ref:`figure-intro`.
75
+ # For more on Figures, including panning and zooming, see :ref:`figure-intro`.
75
76
#
76
77
# :class:`~matplotlib.axes.Axes`
77
78
# ------------------------------
86
87
# :meth:`~matplotlib.axes.Axes.set_xlabel`), and a y-label set via
87
88
# :meth:`~matplotlib.axes.Axes.set_ylabel`).
88
89
#
89
- # The :class:`~.axes.Axes` class and its member functions are the primary
90
- # entry point to working with the OOP interface, and have most of the
91
- # plotting methods defined on them (e.g. ``ax.plot()``, shown above, uses
92
- # the `~.Axes.plot` method)
90
+ # The `~.axes.Axes` methods are the primary interface for configuring
91
+ # most parts of your plot (adding data, controlling axis scales and
92
+ # limits, adding labels etc.).
93
93
#
94
94
# :class:`~matplotlib.axis.Axis`
95
95
# ------------------------------
@@ -446,13 +446,14 @@ def my_plotter(ax, data1, data2, param_dict):
446
446
# well as floating point numbers. These get special locators and formatters
447
447
# as appropriate. For dates:
448
448
449
+ from matplotlib .dates import ConciseDateFormatter
450
+
449
451
fig , ax = plt .subplots (figsize = (5 , 2.7 ), layout = 'constrained' )
450
452
dates = np .arange (np .datetime64 ('2021-11-15' ), np .datetime64 ('2021-12-25' ),
451
453
np .timedelta64 (1 , 'h' ))
452
454
data = np .cumsum (np .random .randn (len (dates )))
453
455
ax .plot (dates , data )
454
- cdf = mpl .dates .ConciseDateFormatter (ax .xaxis .get_major_locator ())
455
- ax .xaxis .set_major_formatter (cdf )
456
+ ax .xaxis .set_major_formatter (ConciseDateFormatter (ax .xaxis .get_major_locator ()))
456
457
457
458
# %%
458
459
# For more information see the date examples
@@ -506,6 +507,8 @@ def my_plotter(ax, data1, data2, param_dict):
506
507
# Often we want to have a third dimension in a plot represented by colors in
507
508
# a colormap. Matplotlib has a number of plot types that do this:
508
509
510
+ from matplotlib .colors import LogNorm
511
+
509
512
X , Y = np .meshgrid (np .linspace (- 3 , 3 , 128 ), np .linspace (- 3 , 3 , 128 ))
510
513
Z = (1 - X / 2 + X ** 5 + Y ** 3 ) * np .exp (- X ** 2 - Y ** 2 )
511
514
@@ -518,8 +521,7 @@ def my_plotter(ax, data1, data2, param_dict):
518
521
fig .colorbar (co , ax = axs [0 , 1 ])
519
522
axs [0 , 1 ].set_title ('contourf()' )
520
523
521
- pc = axs [1 , 0 ].imshow (Z ** 2 * 100 , cmap = 'plasma' ,
522
- norm = mpl .colors .LogNorm (vmin = 0.01 , vmax = 100 ))
524
+ pc = axs [1 , 0 ].imshow (Z ** 2 * 100 , cmap = 'plasma' , norm = LogNorm (vmin = 0.01 , vmax = 100 ))
523
525
fig .colorbar (pc , ax = axs [1 , 0 ], extend = 'both' )
524
526
axs [1 , 0 ].set_title ('imshow() with LogNorm()' )
525
527
0 commit comments