33Usage Guide
44***********
55
6- This tutorial covers some basic usage patterns and best- practices to
6+ This tutorial covers some basic usage patterns and best practices to
77help you get started with Matplotlib.
88"""
99
1616# A simple example
1717# ================
1818#
19- # Matplotlib graphs your data on `~.figure.Figure`\s (i.e ., windows, Jupyter
20- # widgets, etc.), each of which can contain one or more `~.axes.Axes` (i.e. , an
19+ # Matplotlib graphs your data on `~.figure.Figure`\s (e.g ., windows, Jupyter
20+ # widgets, etc.), each of which can contain one or more `~.axes.Axes`, an
2121# area where points can be specified in terms of x-y coordinates, or theta-r
22- # in a polar plot, or x-y-z in a 3D plot, etc.) . The simplest way of
22+ # in a polar plot, x-y-z in a 3D plot, etc. The simplest way of
2323# creating a figure with an axes is using `.pyplot.subplots`. We can then use
2424# `.Axes.plot` to draw some data on the axes:
2525
5050# Parts of a Figure
5151# =================
5252#
53- # Now, let's have a deeper look at the components of a Matplotlib figure.
53+ # Here is a more detailed layout of the components of a Matplotlib figure.
5454#
5555# .. image:: ../../_static/anatomy.png
5656#
5757# :class:`~matplotlib.figure.Figure`
5858# ----------------------------------
5959#
6060# The **whole** figure. The figure keeps
61- # track of all the child :class:`~matplotlib.axes.Axes`, a smattering of
61+ # track of all the child :class:`~matplotlib.axes.Axes`, a group of
6262# 'special' artists (titles, figure legends, etc), and the **canvas**.
63- # (Don't worry too much about the canvas, it is crucial as it is the
64- # object that actually does the drawing to get you your plot, but as the
65- # user it is more-or-less invisible to you). A figure can contain any
63+ # (The canvas is not the primary focus. It is crucial as it is the
64+ # object that actually does the drawing to get you your plot, but as
65+ # the user, it is mostly invisible to you). A figure can contain any
6666# number of :class:`~matplotlib.axes.Axes`, but will typically have
6767# at least one.
6868#
7878# :class:`~matplotlib.axes.Axes`
7979# ------------------------------
8080#
81- # This is what you think of as 'a plot', it is the region of the image
81+ # This is what you think of as 'a plot'. It is the region of the image
8282# with the data space. A given figure
8383# can contain many Axes, but a given :class:`~matplotlib.axes.Axes`
8484# object can only be in one :class:`~matplotlib.figure.Figure`. The
9797# :class:`~matplotlib.axis.Axis`
9898# ------------------------------
9999#
100- # These are the number-line-like objects. They take
101- # care of setting the graph limits and generating the ticks (the marks
100+ # These are the objects most similar to a number line.
101+ # They set graph limits and generate ticks (the marks
102102# on the axis) and ticklabels (strings labeling the ticks). The location of
103103# the ticks is determined by a `~matplotlib.ticker.Locator` object and the
104104# ticklabel strings are formatted by a `~matplotlib.ticker.Formatter`. The
108108# :class:`~matplotlib.artist.Artist`
109109# ----------------------------------
110110#
111- # Basically, everything you can see on the figure is an artist (even the
111+ # Basically, everything visible on the figure is an artist (even
112112# `.Figure`, `Axes <.axes.Axes>`, and `~.axis.Axis` objects). This includes
113113# `.Text` objects, `.Line2D` objects, :mod:`.collections` objects, `.Patch`
114- # objects ... (you get the idea). When the figure is rendered, all of the
114+ # objects, etc ... When the figure is rendered, all of the
115115# artists are drawn to the **canvas**. Most Artists are tied to an Axes; such
116116# an Artist cannot be shared by multiple Axes, or moved from one to another.
117117#
121121# =====================================
122122#
123123# All of plotting functions expect `numpy.array` or `numpy.ma.masked_array` as
124- # input. Classes that are 'array-like' such as `pandas` data objects
125- # and `numpy.matrix` may or may not work as intended. It is best to
126- # convert these to `numpy.array` objects prior to plotting.
124+ # input. Classes that are similar to arrays ( 'array-like') such as `pandas`
125+ # data objects and `numpy.matrix` may not work as intended. Common convention
126+ # is to convert these to `numpy.array` objects prior to plotting.
127127#
128128# For example, to convert a `pandas.DataFrame` ::
129129#
149149#
150150# So one can do (OO-style)
151151
152- x = np .linspace (0 , 2 , 100 )
152+ x = np .linspace (0 , 2 , 100 ) # Sample data.
153153
154154# Note that even in the OO-style, we use `.pyplot.figure` to create the figure.
155155fig , ax = plt .subplots () # Create a figure and an axes.
164164###############################################################################
165165# or (pyplot-style)
166166
167- x = np .linspace (0 , 2 , 100 )
167+ x = np .linspace (0 , 2 , 100 ) # Sample data.
168168
169169plt .plot (x , x , label = 'linear' ) # Plot some data on the (implicit) axes.
170170plt .plot (x , x ** 2 , label = 'quadratic' ) # etc.
203203# nowadays and deprecated. It is only mentioned here because you may still
204204# encounter it in the wild.
205205#
206- # Typically one finds oneself making the same plots over and over
207- # again, but with different data sets, which leads to needing to write
208- # specialized functions to do the plotting. The recommended function
209- # signature is something like:
206+ # If you need to make the same plots over and over
207+ # again with different data sets, use the recommended signature function below.
210208
211209
212210def my_plotter (ax , data1 , data2 , param_dict ):
@@ -243,15 +241,14 @@ def my_plotter(ax, data1, data2, param_dict):
243241my_plotter (ax , data1 , data2 , {'marker' : 'x' })
244242
245243###############################################################################
246- # or if you wanted to have 2 sub-plots:
244+ # or if you wanted to have two sub-plots:
247245
248246fig , (ax1 , ax2 ) = plt .subplots (1 , 2 )
249247my_plotter (ax1 , data1 , data2 , {'marker' : 'x' })
250248my_plotter (ax2 , data3 , data4 , {'marker' : 'o' })
251249
252250###############################################################################
253- # For these simple examples this style seems like overkill, however
254- # once the graphs get slightly more complex it pays off.
251+ # These examples provide convenience for more complex graphs.
255252#
256253#
257254# .. _backends:
@@ -267,7 +264,7 @@ def my_plotter(ax, data1, data2, param_dict):
267264# A lot of documentation on the website and in the mailing lists refers
268265# to the "backend" and many new users are confused by this term.
269266# Matplotlib targets many different use cases and output formats. Some
270- # people use Matplotlib interactively from the python shell and have
267+ # people use Matplotlib interactively from the Python shell and have
271268# plotting windows pop up when they type commands. Some people run
272269# `Jupyter <https://jupyter.org>`_ notebooks and draw inline plots for
273270# quick data analysis. Others embed Matplotlib into graphical user
@@ -290,17 +287,17 @@ def my_plotter(ax, data1, data2, param_dict):
290287#
291288# There are three ways to configure your backend:
292289#
293- # 1. The :rc:`backend` parameter in your :file:`matplotlibrc` file
294- # 2. The :envvar:`MPLBACKEND` environment variable
295- # 3. The function :func:`matplotlib.use`
290+ # - The :rc:`backend` parameter in your :file:`matplotlibrc` file
291+ # - The :envvar:`MPLBACKEND` environment variable
292+ # - The function :func:`matplotlib.use`
296293#
297- # A more detailed description is given below .
294+ # Below is a more detailed description.
298295#
299- # If multiple of these are configurations are present, the last one from the
296+ # If there is more than one configuration present, the last one from the
300297# list takes precedence; e.g. calling :func:`matplotlib.use()` will override
301298# the setting in your :file:`matplotlibrc`.
302299#
303- # If no backend is explicitly set, Matplotlib automatically detects a usable
300+ # Without a backend explicitly set, Matplotlib automatically detects a usable
304301# backend based on what is available on your system and on whether a GUI event
305302# loop is already running. On Linux, if the environment variable
306303# :envvar:`DISPLAY` is unset, the "event loop" is identified as "headless",
@@ -368,8 +365,8 @@ def my_plotter(ax, data1, data2, param_dict):
368365# If, however, you want to write graphical user interfaces, or a web
369366# application server
370367# (:doc:`/gallery/user_interfaces/web_application_server_sgskip`), or need a
371- # better understanding of what is going on, read on. To make things a little
372- # more customizable for graphical user interfaces, Matplotlib separates
368+ # better understanding of what is going on, read on. To make things more
369+ # easily customizable for graphical user interfaces, Matplotlib separates
373370# the concept of the renderer (the thing that actually does the drawing)
374371# from the canvas (the place where the drawing goes). The canonical
375372# renderer for user interfaces is ``Agg`` which uses the `Anti-Grain
@@ -378,11 +375,11 @@ def my_plotter(ax, data1, data2, param_dict):
378375# ``macosx`` backends. An alternative renderer is based on the Cairo library,
379376# used by ``Qt5Cairo``, ``Qt4Cairo``, etc.
380377#
381- # For the rendering engines, one can also distinguish between `vector
378+ # For the rendering engines, users can also distinguish between `vector
382379# <https://en.wikipedia.org/wiki/Vector_graphics>`_ or `raster
383380# <https://en.wikipedia.org/wiki/Raster_graphics>`_ renderers. Vector
384381# graphics languages issue drawing commands like "draw a line from this
385- # point to this point" and hence are scale free, and raster backends
382+ # point to this point" and hence are scale free. Raster backends
386383# generate a pixel representation of the line whose accuracy depends on a
387384# DPI setting.
388385#
@@ -406,9 +403,9 @@ def my_plotter(ax, data1, data2, param_dict):
406403# To save plots using the non-interactive backends, use the
407404# ``matplotlib.pyplot.savefig('filename')`` method.
408405#
409- # And here are the user interfaces and renderer combinations supported;
406+ # These are the user interfaces and renderer combinations supported;
410407# these are *interactive backends*, capable of displaying to the screen
411- # and of using appropriate renderers from the table above to write to
408+ # and using appropriate renderers from the table above to write to
412409# a file:
413410#
414411# ========= ================================================================
@@ -441,8 +438,8 @@ def my_plotter(ax, data1, data2, param_dict):
441438# ========= ================================================================
442439#
443440# .. note::
444- # The names of builtin backends case-insensitive; e.g. , 'Qt5Agg' and
445- # 'qt5agg' are equivalent.
441+ # The names of builtin backends are case-insensitive. For example , 'Qt5Agg'
442+ # and 'qt5agg' are equivalent.
446443#
447444# .. _`Anti-Grain Geometry`: http://antigrain.com/
448445# .. _`Portable Document Format`: https://en.wikipedia.org/wiki/Portable_Document_Format
@@ -467,7 +464,7 @@ def my_plotter(ax, data1, data2, param_dict):
467464# ^^^^^^
468465#
469466# The Jupyter widget ecosystem is moving too fast to support directly in
470- # Matplotlib. To install ipympl
467+ # Matplotlib. To install ipympl:
471468#
472469# .. code-block:: bash
473470#
@@ -513,7 +510,7 @@ def my_plotter(ax, data1, data2, param_dict):
513510# and whether a script or shell session continues after a plot
514511# is drawn on the screen, depends on the functions and methods
515512# that are called, and on a state variable that determines whether
516- # Matplotlib is in "interactive mode". The default Boolean value is set
513+ # Matplotlib is in "interactive mode." The default Boolean value is set
517514# by the :file:`matplotlibrc` file, and may be customized like any other
518515# configuration parameter (see :doc:`/tutorials/introductory/customizing`). It
519516# may also be set via :func:`matplotlib.interactive`, and its
@@ -536,7 +533,7 @@ def my_plotter(ax, data1, data2, param_dict):
536533#
537534# .. note::
538535# Interactive mode works with suitable backends in ipython and in
539- # the ordinary python shell, but it does *not* work in the IDLE IDE.
536+ # the ordinary Python shell, but it does *not* work in the IDLE IDE.
540537# If the default backend does not support interactivity, an interactive
541538# backend can be explicitly activated using any of the methods discussed
542539# in `What is a backend?`_.
@@ -545,7 +542,7 @@ def my_plotter(ax, data1, data2, param_dict):
545542# Interactive example
546543# --------------------
547544#
548- # From an ordinary python prompt, or after invoking ipython with no options,
545+ # From an ordinary Python prompt, or after invoking ipython with no options,
549546# try this::
550547#
551548# import matplotlib.pyplot as plt
@@ -559,7 +556,7 @@ def my_plotter(ax, data1, data2, param_dict):
559556# plt.xlabel("index")
560557#
561558# On most interactive backends, the figure window will also be updated if you
562- # change it via the object-oriented interface. E.g. get a reference to the
559+ # change it via the object-oriented interface. That is, get a reference to the
563560# `~matplotlib.axes.Axes` instance, and call a method of that instance::
564561#
565562# ax = plt.gca()
@@ -576,7 +573,7 @@ def my_plotter(ax, data1, data2, param_dict):
576573# Non-interactive example
577574# -----------------------
578575#
579- # Start a fresh session as in the previous example, but now
576+ # Start a new session as per the previous example, but now
580577# turn interactive mode off::
581578#
582579# import matplotlib.pyplot as plt
@@ -591,23 +588,23 @@ def my_plotter(ax, data1, data2, param_dict):
591588#
592589# Now you see the plot, but your terminal command line is
593590# unresponsive; `.pyplot.show()` *blocks* the input
594- # of additional commands until you manually kill the plot
591+ # of additional commands until you manually close the plot
595592# window.
596593#
597- # What good is this--being forced to use a blocking function?
598- # Suppose you need a script that plots the contents of a file
599- # to the screen. You want to look at that plot, and then end
600- # the script. Without some blocking command such as ``show()``, the
601- # script would flash up the plot and then end immediately,
602- # leaving nothing on the screen.
594+ # Using a blocking function has benefits to users. Suppose a user
595+ # needs a script that plots the contents of a file to the screen.
596+ # The user may want to look at that plot, and then end the script.
597+ # Without a blocking command such as ``show()``, the script would
598+ # flash up the plot and then end immediately, leaving nothing on
599+ # the screen.
603600#
604601# In addition, non-interactive mode delays all drawing until
605- # ``show()`` is called; this is more efficient than redrawing
602+ # ``show()`` is called. This is more efficient than redrawing
606603# the plot each time a line in the script adds a new feature.
607604#
608605# Prior to version 1.0, ``show()`` generally could not be called
609606# more than once in a single script (although sometimes one
610- # could get away with it); for version 1.0.1 and above, this
607+ # could get away with it). For version 1.0.1 and above, this
611608# restriction is lifted, so one can write a script like this::
612609#
613610# import numpy as np
@@ -618,7 +615,7 @@ def my_plotter(ax, data1, data2, param_dict):
618615# plt.plot(np.random.rand(10))
619616# plt.show()
620617#
621- # This makes three plots, one at a time. I.e. , the second plot will show up
618+ # This makes three plots, one at a time. That is , the second plot will show up
622619# once the first plot is closed.
623620#
624621# Summary
@@ -644,8 +641,8 @@ def my_plotter(ax, data1, data2, param_dict):
644641# ===========
645642#
646643# Whether exploring data in interactive mode or programmatically
647- # saving lots of plots, rendering performance can be a painful
648- # bottleneck in your pipeline. Matplotlib provides a couple
644+ # saving lots of plots, rendering performance can be a challenging
645+ # bottleneck in your pipeline. Matplotlib provides multiple
649646# ways to greatly reduce rendering time at the cost of a slight
650647# change (to a settable tolerance) in your plot's appearance.
651648# The methods available to reduce rendering time depend on the
@@ -659,7 +656,7 @@ def my_plotter(ax, data1, data2, param_dict):
659656# :rc:`path.simplify` and :rc:`path.simplify_threshold`, which
660657# can be defined e.g. in the :file:`matplotlibrc` file (see
661658# :doc:`/tutorials/introductory/customizing` for more information about
662- # the :file:`matplotlibrc` file). :rc:`path.simplify` is a boolean
659+ # the :file:`matplotlibrc` file). :rc:`path.simplify` is a Boolean
663660# indicating whether or not line segments are simplified at all.
664661# :rc:`path.simplify_threshold` controls how much line segments are simplified;
665662# higher thresholds result in quicker rendering.
@@ -687,14 +684,14 @@ def my_plotter(ax, data1, data2, param_dict):
687684# plt.show()
688685#
689686# Matplotlib currently defaults to a conservative simplification
690- # threshold of ``1/9``. If you want to change your default settings
691- # to use a different value, you can change your :file:`matplotlibrc`
692- # file. Alternatively, you could create a new style for
693- # interactive plotting (with maximal simplification) and another
694- # style for publication quality plotting (with minimal
695- # simplification) and activate them as necessary. See
696- # :doc:`/tutorials/introductory/customizing` for
697- # instructions on how to perform these actions.
687+ # threshold of ``1/9``. To change default settings to use a different
688+ # value, change the :file:`matplotlibrc` file. Alternatively, users
689+ # can create a new style for interactive plotting (with maximal
690+ # simplification) and another style for publication quality plotting
691+ # (with minimal simplification) and activate them as necessary. See
692+ # :doc:`/tutorials/introductory/customizing` for instructions on
693+ # how to perform these actions.
694+ #
698695#
699696# The simplification works by iteratively merging line segments
700697# into a single vector until the next line segment's perpendicular
@@ -732,7 +729,7 @@ def my_plotter(ax, data1, data2, param_dict):
732729#
733730# If you are using the Agg backend (see :ref:`what-is-a-backend`),
734731# then you can make use of :rc:`agg.path.chunksize`
735- # This allows you to specify a chunk size, and any lines with
732+ # This allows users to specify a chunk size, and any lines with
736733# greater than that many vertices will be split into multiple
737734# lines, each of which has no more than ``agg.path.chunksize``
738735# many vertices. (Unless ``agg.path.chunksize`` is zero, in
@@ -779,13 +776,13 @@ def my_plotter(ax, data1, data2, param_dict):
779776# The *fast* style can be used to automatically set
780777# simplification and chunking parameters to reasonable
781778# settings to speed up plotting large amounts of data.
782- # It can be used simply by running ::
779+ # The following code runs it ::
783780#
784781# import matplotlib.style as mplstyle
785782# mplstyle.use('fast')
786783#
787- # It is very lightweight, so it plays nicely with other
788- # styles, just make sure the fast style is applied last
784+ # It is very lightweight, so it works well with other
785+ # styles. Be sure the fast style is applied last
789786# so that other styles do not overwrite the settings::
790787#
791788# mplstyle.use(['dark_background', 'ggplot', 'fast'])
0 commit comments