Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f5ecf5e

Browse files
committed
Add docs for style package
1 parent 5f80ca1 commit f5ecf5e

3 files changed

Lines changed: 108 additions & 0 deletions

File tree

doc/users/beginner.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Beginner's Guide
1313
:maxdepth: 2
1414

1515
pyplot_tutorial.rst
16+
style_sheets.rst
1617
navigation_toolbar.rst
1718
index_text.rst
1819
image_tutorial.rst

doc/users/style_sheets.rst

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
.. _style-sheets
2+
3+
***********************************
4+
Customizing plots with style sheets
5+
***********************************
6+
7+
8+
The ``style`` package adds support for easy-to-switch plotting "styles" with
9+
the same parameters as a matplotlibrc_ file.
10+
11+
There are a number of pre-defined styles provided by matplotlib. For
12+
example, there's a pre-defined style called "ggplot", which emulates the
13+
aesthetics of ggplot_ (a popular plotting package for R_). To use this style,
14+
just add::
15+
16+
>>> from matplotlib import style
17+
>>> style.use('ggplot')
18+
19+
To list all available styles, use::
20+
21+
>>> print style.available
22+
23+
24+
Defining your own style
25+
=======================
26+
27+
You can create custom styles and use them by calling ``style.use`` with the
28+
path or URL to the style sheet. Alternatively, if you add your
29+
``<style-name>.mplstyle`` file to ``~/.matplotlib/stylelib`` (you may need to
30+
create this directory), you can reuse your custom style sheet with a call to
31+
``style.use(<style-name>)``. Note that a custom style sheet in
32+
``~/.matplotlib/stylelib`` will override a style sheet defined by matplotlib if
33+
the styles have the same name.
34+
35+
For example, you might want to create
36+
``~/.matplotlib/stylelib/presentation.mplstyle`` with the following::
37+
38+
axes.titlesize : 24
39+
axes.labelsize : 20
40+
lines.linewidth : 3
41+
lines.markersize : 10
42+
xtick.labelsize : 16
43+
ytick.labelsize : 16
44+
45+
Then, when you want to adapt a plot designed for a paper to one that looks
46+
good in a presentation, you can just add::
47+
48+
>>> from matplotlib import style
49+
>>> style.use('presentation')
50+
51+
52+
Composing styles
53+
================
54+
55+
Style sheets are designed to be composed together. So you can have a style
56+
sheet that customizes colors and a separate style sheet that alters element
57+
sizes for presentations. These styles can easily be combined by passing
58+
a list of styles::
59+
60+
>>> from matplotlib import style
61+
>>> style.use(['dark_background', 'presentation'])
62+
63+
Note that styles further to the right will overwrite values that are already
64+
defined by styles on the right.
65+
66+
67+
Temporary styling
68+
=================
69+
70+
If you only want to use a style for a specific block of code but don't want
71+
to change the global styling, the style package provides a context manager
72+
for limiting your changes to a specific scope. To isolate the your styling
73+
changes, you can write something like the following::
74+
75+
76+
>>> import numpy as np
77+
>>> import matplotlib.pyplot as plt
78+
>>> from matplotlib import style
79+
>>>
80+
>>> with style.context(('dark_background')):
81+
>>> plt.plot(np.sin(np.linspace(0, 2*np.pi)), 'r-o')
82+
>>>
83+
>>> # Some plotting code with the default style
84+
>>>
85+
>>> plt.show()
86+
87+
88+
.. _matplotlibrc: http://matplotlib.sourceforge.net/users/customizing.html
89+
.. _ggplot: http://had.co.nz/ggplot/
90+
.. _R: http://www.r-project.org/

doc/users/whats_new.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,23 @@ an offset will be determined such that the tick labels are
9898
meaningful. If `False` then the full number will be formatted in all
9999
conditions.
100100

101+
``style`` package added
102+
```````````````````````
103+
You can now easily switch between different styles using the new ``style``
104+
package::
105+
106+
>>> from matplotlib import style
107+
>>> style.use('dark_background')
108+
109+
Subsequent plots will use updated colors, sizes, etc. To list all available
110+
styles, use::
111+
112+
>>> print style.available
113+
114+
You can add your own custom ``<style name>.mplstyle`` files to
115+
``~/.matplotlib/stylelib`` or call ``use`` with a URL pointing to a file with
116+
``matplotlibrc`` settings.
117+
101118
.. _whats-new-1-3:
102119

103120
new in matplotlib-1.3

0 commit comments

Comments
 (0)