Search notes:

Python library: matplotlib

Installation

The following attempt to install matplotlib …
$ python3 -m pip install -U matplotlib
… resulted in these warnings:
WARNING: The scripts f2py, f2py3 and f2py3.9 are installed in '/home/rene/.local/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
WARNING: The scripts fonttools, pyftmerge, pyftsubset and ttx are installed in '/home/rene/.local/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.

Plotting a simple graph

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-np.pi, np.pi, 300)
y = np.sin(x)

plt.plot(x , y, 'r-', label='sin(x)') # r- draws a red line
plt.ylim(-1.25, 1.25)
plt.legend()
plt.show()
Github repository about-python, path: /libraries/matplotlib/sin.py

Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.

When I experienced this error on Debian, installing tkinter solved the problem.

Module members

_add_data_doc()
_api Module
atexit Module
bezier Module
cbook Module
checkdep_usetex()
_check_versions()
_c_internal_utils Module
_cm Module
cm Module
_cm_listed Module
_color_data Module
colormaps ?
colors Module
color_sequences ?
contextlib Module
cycler()
defaultParams dict object
default_test_modules list object
_deprecated_ignore_map dict object
_deprecated_map dict object
_deprecated_remain_as_none dict object
_docstring Module
_ensure_handler ?
_enums Module
_ExecInfo matplotlib._ExecInfo class
ExecutableNotFoundError matplotlib.ExecutableNotFoundError class
_fontconfig_pattern Module
ft2font Module
functools Module
get_backend()
get_cachedir()
get_configdir()
_get_config_or_cache_dir()
get_data_path()
_get_executable_info ?
_get_ssl_context ?
_get_version()
_get_xdg_cache_dir()
_get_xdg_config_dir()
importlib Module
_init_tests()
inspect Module
interactive()
is_interactive()
is_url()
_label_from_arg()
locale Module
_log ?
_logged_cached()
logging Module
MatplotlibDeprecationWarning matplotlib._api.deprecation.MatplotlibDeprecationWarning class
matplotlib_fname()
MutableMapping ?
namedtuple()
numpy Module
_open_file_or_url()
os Module
Parameter inspect.Parameter class
_parse_to_version_info()
parse_version()
Path pathlib.Path class
_path Module
path Module
pprint Module
_preprocess_data()
rc() A convenience function that allows to set multiple runtime configurations (=rc) at once. See also rcParams.
rc_context() A context manager to override values of rcParams temporaily (with mpl.rc_context({'lines.linewidth': 4}): …). rc_context can also be used as a decorator to set rc defaults for a given function.
rcdefaults()
rc_file()
rc_file_defaults()
RcParams ?
rcParams An instance of matplotlib.RcParams which is a dictionary like object used for run-time (=rc) configuration (for example mpl.rcParams['lines.lineWidth'] = 3). See also rc() and rc_context and the list of rcParams.
rc_params()
rcParamsDefault ?
rc_params_from_file()
_rc_params_in_file()
rcParamsOrig ?
rcsetup Module
re Module
_replacer()
sanitize_sequence()
scale Module
set_loglevel()
shutil Module
subprocess Module
sys Module
tempfile Module
test()
ticker Module
transforms Module
use()
validate_backend()
_version Module
_VersionInfo matplotlib._VersionInfo class
warnings Module

TODO

matplotlib.pyplot is a collection of functions that make matplotlib work like MATLAB.
Data to be plotted is expected to be provided as numpy.array or numpy.ma.masked_array objects, or objects that can be passed to numpy.asarray().
There are two interfaces:
When using the OO style (?):
Matplotlib accepts TeX equation expressions in any text expression.
Matplotlib uses the Pillow library to load image data.

Axes class

The Axes class is central to matplotlib because it defines the plotting area for data to be drawn.

Standard use

import matplotlib.pyplot as plt
import numpy             as np

#
#     Create a figure …
#
fig = plt.figure()

#
#     … use the figure to create one or more axes …
#
ax  = fig.add_subplot(2, 1, 1) # two rows, one column, first plot

#
#    … and use the axes to create the Artist primitives.
#

ax.…()

fig.show()
plt.waitforbuttonpress() # Really necessary?
Variant: create figure and axes in one go:
fig, ax = plt.subplots()

Coordinates

The default coordinate system for figures: 0,0 is bottom-left, 1,1 is top-right.
The coordinate system can be changed by setting the transform property of an Artist object.
See also the transFigure property of the Figure object.

styles/rcParams

Plots can be styled using rcParams. rcParams can be set using the following methods, in order of decreasing precedence:
  • Setting matplotlib.rcParams at runtime
  • Using style sheets
  • Using a matplotlibrc file
Available styles can be found with
print(plt.style.available)

See also

The Jupyter Notebook magic function %matplotlib
Seaborn is a (Python) Data visualization library based on matplotlib.

Index