1717import numpy as np
1818
1919# sphinx_gallery_thumbnail_number = 3
20- import matplotlib as mpl
2120
2221# %%
2322#
2928# area where points can be specified in terms of x-y coordinates (or theta-r
3029# in a polar plot, x-y-z in a 3D plot, etc.). The simplest way of
3130# 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:
3333
34- fig , ax = plt .subplots () # Create a figure containing a single Axes.
34+ fig , ax = plt .subplots () # Create a figure containing a single Axes.
3535ax .plot ([1 , 2 , 3 , 4 ], [1 , 4 , 2 , 3 ]) # Plot some data on the Axes.
36+ plt .show () # Show the figure.
3637
3738# %%
3839#
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 .
4243#
4344# .. _figure_parts:
4445#
5455#
5556# The **whole** figure. The Figure keeps
5657# 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
5859# even nested subfigures.
5960#
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::
6163#
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
6466# fig, axs = plt.subplots(2, 2) # a figure with a 2x2 grid of Axes
6567# # a figure with one Axes on the left, and two on the right:
6668# fig, axs = plt.subplot_mosaic([['left', 'right_top'],
6769# ['left', 'right_bottom']])
6870#
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.
7374#
74- # For more on Figures, see :ref:`figure-intro`.
75+ # For more on Figures, including panning and zooming, see :ref:`figure-intro`.
7576#
7677# :class:`~matplotlib.axes.Axes`
7778# ------------------------------
8687# :meth:`~matplotlib.axes.Axes.set_xlabel`), and a y-label set via
8788# :meth:`~matplotlib.axes.Axes.set_ylabel`).
8889#
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.).
9393#
9494# :class:`~matplotlib.axis.Axis`
9595# ------------------------------
@@ -446,13 +446,14 @@ def my_plotter(ax, data1, data2, param_dict):
446446# well as floating point numbers. These get special locators and formatters
447447# as appropriate. For dates:
448448
449+ from matplotlib .dates import ConciseDateFormatter
450+
449451fig , ax = plt .subplots (figsize = (5 , 2.7 ), layout = 'constrained' )
450452dates = np .arange (np .datetime64 ('2021-11-15' ), np .datetime64 ('2021-12-25' ),
451453 np .timedelta64 (1 , 'h' ))
452454data = np .cumsum (np .random .randn (len (dates )))
453455ax .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 ()))
456457
457458# %%
458459# For more information see the date examples
@@ -506,6 +507,8 @@ def my_plotter(ax, data1, data2, param_dict):
506507# Often we want to have a third dimension in a plot represented by colors in
507508# a colormap. Matplotlib has a number of plot types that do this:
508509
510+ from matplotlib .colors import LogNorm
511+
509512X , Y = np .meshgrid (np .linspace (- 3 , 3 , 128 ), np .linspace (- 3 , 3 , 128 ))
510513Z = (1 - X / 2 + X ** 5 + Y ** 3 ) * np .exp (- X ** 2 - Y ** 2 )
511514
@@ -518,8 +521,7 @@ def my_plotter(ax, data1, data2, param_dict):
518521fig .colorbar (co , ax = axs [0 , 1 ])
519522axs [0 , 1 ].set_title ('contourf()' )
520523
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 ))
523525fig .colorbar (pc , ax = axs [1 , 0 ], extend = 'both' )
524526axs [1 , 0 ].set_title ('imshow() with LogNorm()' )
525527
0 commit comments