11"""
22.. redirect-from:: /users/customizing
33
4+ =====================================================
45Customizing Matplotlib with style sheets and rcParams
56=====================================================
67
78Tips for customizing the properties and default styles of Matplotlib.
89
9- Using style sheets
10- ------------------
10+ There are three ways to customize Matplotlib:
11+
12+ 1. :ref:`Setting rcParams at runtime<customizing-with-dynamic-rc-settings>`.
13+ 2. :ref:`Using style sheets<customizing-with-style-sheets>`.
14+ 3. :ref:`Changing your matplotlibrc file<customizing-with-matplotlibrc-files>`.
15+
16+ Setting rcParams at runtime takes precedence over style sheets, style
17+ sheets take precedence over :file:`matplotlibrc` files.
18+
19+ .. _customizing-with-dynamic-rc-settings:
1120
12- The :mod:`.style` package adds support for easy-to-switch plotting
13- "styles" with the same parameters as a :ref:`matplotlib rc
14- <customizing-with-matplotlibrc-files>` file (which is read at startup to
15- configure Matplotlib).
21+ Runtime rc settings
22+ ===================
1623
17- There are a number of pre-defined styles :doc:`provided by Matplotlib
18- </gallery/style_sheets/style_sheets_reference>`. For
19- example, there's a pre-defined style called "ggplot", which emulates the
20- aesthetics of ggplot_ (a popular plotting package for R_). To use this style,
21- just add:
24+ You can dynamically change the default rc (runtime configuration)
25+ settings in a python script or interactively from the python shell. All
26+ rc settings are stored in a dictionary-like variable called
27+ :data:`matplotlib.rcParams`, which is global to the matplotlib package.
28+ See `matplotlib.rcParams` for a full list of configurable rcParams.
29+ rcParams can be modified directly, for example:
2230"""
2331
2432import numpy as np
2533import matplotlib .pyplot as plt
2634import matplotlib as mpl
2735from cycler import cycler
28- plt .style .use ('ggplot' )
36+ mpl .rcParams ['lines.linewidth' ] = 2
37+ mpl .rcParams ['lines.linestyle' ] = '--'
2938data = np .random .randn (50 )
39+ plt .plot (data )
40+
41+ ###############################################################################
42+ # Note, that in order to change the usual `~.Axes.plot` color you have to
43+ # change the *prop_cycle* property of *axes*:
44+
45+ mpl .rcParams ['axes.prop_cycle' ] = cycler (color = ['r' , 'g' , 'b' , 'y' ])
46+ plt .plot (data ) # first color is red
47+
48+ ###############################################################################
49+ # Matplotlib also provides a couple of convenience functions for modifying rc
50+ # settings. `matplotlib.rc` can be used to modify multiple
51+ # settings in a single group at once, using keyword arguments:
52+
53+ mpl .rc ('lines' , linewidth = 4 , linestyle = '-.' )
54+ plt .plot (data )
55+
56+ ###############################################################################
57+ # Temporary rc settings
58+ # ---------------------
59+ #
60+ # The :data:`matplotlib.rcParams` object can also be changed temporarily using
61+ # the `matplotlib.rc_context` context manager:
62+
63+ with mpl .rc_context ({'lines.linewidth' : 2 , 'lines.linestyle' : ':' }):
64+ plt .plot (data )
65+
66+ ###############################################################################
67+ # `matplotlib.rc_context` can also be used as a decorator to modify the
68+ # defaults within a function:
69+
70+
71+ @mpl .rc_context ({'lines.linewidth' : 3 , 'lines.linestyle' : '-' })
72+ def plotting_function ():
73+ plt .plot (data )
74+
75+ plotting_function ()
76+
77+ ###############################################################################
78+ # `matplotlib.rcdefaults` will restore the standard Matplotlib
79+ # default settings.
80+ #
81+ # There is some degree of validation when setting the values of rcParams, see
82+ # :mod:`matplotlib.rcsetup` for details.
83+
84+ ###############################################################################
85+ # .. _customizing-with-style-sheets:
86+ #
87+ # Using style sheets
88+ # ==================
89+ #
90+ # Another way to change the visual appearance of plots is to set the
91+ # rcParams in a so-called style sheet and import that style sheet with
92+ # `matplotlib.style.use`. In this way you can switch easily between
93+ # different styles by simply changing the imported style sheet. A style
94+ # sheets looks the same as a :ref:`matplotlibrc<matplotlibrc-sample>`
95+ # file, but in a style sheet you can only set rcParams that are related
96+ # to the actual style of a plot. Other rcParams, like *backend*, will be
97+ # ignored. :file:`matplotlibrc` files support all rcParams. The
98+ # rationale behind this is to make style sheets portable between
99+ # different machines without having to worry about dependencies which
100+ # might or might not be installed on another machine. For a full list of
101+ # rcParams see `matplotlib.rcParams`. For a list of rcParams that are
102+ # ignored in style sheets see `matplotlib.style.use`.
103+ #
104+ # There are a number of pre-defined styles :doc:`provided by Matplotlib
105+ # </gallery/style_sheets/style_sheets_reference>`. For
106+ # example, there's a pre-defined style called "ggplot", which emulates the
107+ # aesthetics of ggplot_ (a popular plotting package for R_). To use this
108+ # style, add:
109+
110+ plt .style .use ('ggplot' )
30111
31112###############################################################################
32113# To list all available styles, use:
104185plt .show ()
105186
106187###############################################################################
107- # .. _matplotlib-rcparams:
108- #
109- # Matplotlib rcParams
110- # ===================
111- #
112- # .. _customizing-with-dynamic-rc-settings:
113- #
114- # Dynamic rc settings
115- # -------------------
116- #
117- # You can also dynamically change the default rc settings in a python script or
118- # interactively from the python shell. All of the rc settings are stored in a
119- # dictionary-like variable called :data:`matplotlib.rcParams`, which is global to
120- # the matplotlib package. rcParams can be modified directly, for example:
121-
122- mpl .rcParams ['lines.linewidth' ] = 2
123- mpl .rcParams ['lines.linestyle' ] = '--'
124- plt .plot (data )
125-
126- ###############################################################################
127- # Note, that in order to change the usual `~.Axes.plot` color you have to
128- # change the *prop_cycle* property of *axes*:
129-
130- mpl .rcParams ['axes.prop_cycle' ] = cycler (color = ['r' , 'g' , 'b' , 'y' ])
131- plt .plot (data ) # first color is red
132-
133- ###############################################################################
134- # Matplotlib also provides a couple of convenience functions for modifying rc
135- # settings. `matplotlib.rc` can be used to modify multiple
136- # settings in a single group at once, using keyword arguments:
137-
138- mpl .rc ('lines' , linewidth = 4 , linestyle = '-.' )
139- plt .plot (data )
140-
141- ###############################################################################
142- # Temporary rc settings
143- # ---------------------
144- #
145- # The :data:`matplotlib.rcParams` object can also be changed temporarily using
146- # the `matplotlib.rc_context` context manager:
147-
148- with mpl .rc_context ({'lines.linewidth' : 2 , 'lines.linestyle' : ':' }):
149- plt .plot (data )
150-
151- ###############################################################################
152- # `matplotlib.rc_context` can also be used as a decorator to modify the
153- # defaults within a function:
154-
155-
156- @mpl .rc_context ({'lines.linewidth' : 3 , 'lines.linestyle' : '-' })
157- def plotting_function ():
158- plt .plot (data )
159-
160- plotting_function ()
161-
162- ###############################################################################
163- # `matplotlib.rcdefaults` will restore the standard Matplotlib
164- # default settings.
165- #
166- # There is some degree of validation when setting the values of rcParams, see
167- # :mod:`matplotlib.rcsetup` for details.
168- #
169188# .. _customizing-with-matplotlibrc-files:
170189#
171190# The :file:`matplotlibrc` file
172- # -----------------------------
191+ # =============================
173192#
174193# Matplotlib uses :file:`matplotlibrc` configuration files to customize all
175194# kinds of properties, which we call 'rc settings' or 'rc parameters'. You can
176195# control the defaults of almost every property in Matplotlib: figure size and
177196# DPI, line width, color and style, axes, axis and grid properties, text and
178- # font properties and so on. When a URL or path is not specified with a call to
179- # ``style.use('<path>/<style-name>.mplstyle')``, Matplotlib looks for
180- # :file:`matplotlibrc` in four locations, in the following order:
197+ # font properties and so on. The :file:`matplotlibrc` is read at startup to
198+ # configure Matplotlib. Matplotlib looks for :file:`matplotlibrc` in four
199+ # locations, in the following order:
181200#
182201# 1. :file:`matplotlibrc` in the current working directory, usually used for
183202# specific customizations that you do not want to apply elsewhere.
@@ -204,8 +223,12 @@ def plotting_function():
204223# your customizations to be saved, please move this file to your
205224# user-specific matplotlib directory.
206225#
207- # Once a :file:`matplotlibrc` file has been found, it will *not* search any of
208- # the other paths.
226+ # Once a :file:`matplotlibrc` file has been found, it will *not* search
227+ # any of the other paths. When a
228+ # :ref:`style sheet<customizing-with-style-sheets>` is given with
229+ # ``style.use('<path>/<style-name>.mplstyle')``, settings specified in
230+ # the style sheet take precedence over settings in the
231+ # :file:`matplotlibrc` file.
209232#
210233# To display where the currently active :file:`matplotlibrc` file was
211234# loaded from, one can do the following::
@@ -214,12 +237,13 @@ def plotting_function():
214237# >>> matplotlib.matplotlib_fname()
215238# '/home/foo/.config/matplotlib/matplotlibrc'
216239#
217- # See below for a sample :ref:`matplotlibrc file<matplotlibrc-sample>`.
240+ # See below for a sample :ref:`matplotlibrc file<matplotlibrc-sample>`
241+ # and see `matplotlib.rcParams` for a full list of configurable rcParams.
218242#
219243# .. _matplotlibrc-sample:
220244#
221- # A sample matplotlibrc file
222- # ~~~~~~~~~~~~~~~~~~~~~~~~~~
245+ # The default :file:` matplotlibrc` file
246+ # -------------------------------------
223247#
224248# .. literalinclude:: ../../../lib/matplotlib/mpl-data/matplotlibrc
225249#
0 commit comments