Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0e7daad

Browse files
committed
Merge pull request#2236 from tonysyu/stylesheets
Add easy style sheet selection
2 parentseb12e9a +246c348 commit0e7daad

File tree

16 files changed

+647
-32
lines changed

16 files changed

+647
-32
lines changed

‎doc/conf.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
extensions= ['matplotlib.sphinxext.mathmpl','sphinxext.math_symbol_table',
2929
'sphinx.ext.autodoc','matplotlib.sphinxext.only_directives',
3030
'sphinx.ext.doctest','sphinx.ext.autosummary',
31-
'matplotlib.sphinxext.plot_directive','sphinx.ext.inheritance_diagram',
31+
'matplotlib.sphinxext.plot_directive',
32+
'sphinx.ext.inheritance_diagram',
3233
'sphinxext.gen_gallery','sphinxext.gen_rst',
3334
'matplotlib.sphinxext.ipython_console_highlighting',
3435
'sphinxext.github',
@@ -117,6 +118,7 @@
117118
('text_labels_and_annotations','Text, labels, and annotations'),
118119
('ticks_and_spines','Ticks and spines'),
119120
('subplots_axes_and_figures','Subplots, axes, and figures'),
121+
('style_sheets','Style sheets'),
120122
('specialty_plots','Specialty plots'),
121123
('showcase','Showcase'),
122124
('api','API'),

‎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: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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 amatplotlibrc_ 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 ofggplot_ (a popular plotting package forR_). To use this style,
14+
just add::
15+
16+
>>> import matplotlib.pyplot as plt
17+
>>> plt.style.use('ggplot')
18+
19+
To list all available styles, use::
20+
21+
>>> print plt.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+
>>> import matplotlib.pyplot as plt
49+
>>> plt.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+
>>> import matplotlib.pyplot as plt
61+
>>> plt.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+
>>>
79+
>>> with plt.style.context(('dark_background')):
80+
>>> plt.plot(np.sin(np.linspace(0, 2*np.pi)), 'r-o')
81+
>>>
82+
>>> # Some plotting code with the default style
83+
>>>
84+
>>> plt.show()
85+
86+
87+
.. _matplotlibrc:http://matplotlib.sourceforge.net/users/customizing.html
88+
.. _ggplot:http://had.co.nz/ggplot/
89+
.. _R:http://www.r-project.org/

‎doc/users/whats_new.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,26 @@ 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+
118+
*Note that this is an experimental feature*, and the interface may change as
119+
users test out this new feature.
120+
101121
.. _whats-new-1-3:
102122

103123
new in matplotlib-1.3
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
This example demonstrates the "dark_background" style, which uses white for
3+
elements that are typically black (text, borders, etc). Note, however, that not
4+
all plot elements default to colors defined by an rc parameter.
5+
6+
"""
7+
importnumpyasnp
8+
importmatplotlib.pyplotasplt
9+
10+
11+
plt.style.use('dark_background')
12+
13+
L=6
14+
x=np.linspace(0,L)
15+
ncolors=len(plt.rcParams['axes.color_cycle'])
16+
shift=np.linspace(0,L,ncolors,endpoint=False)
17+
forsinshift:
18+
plt.plot(x,np.sin(x+s),'o-')
19+
plt.xlabel('x-axis')
20+
plt.ylabel('y-axis')
21+
plt.title('title')
22+
23+
plt.show()

‎examples/style_sheets/plot_ggplot.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""
2+
This example demonstrates the "ggplot" style, which adjusts the style to
3+
emulate ggplot_ (a popular plotting package for R_).
4+
5+
These settings were shamelessly stolen from [1]_ (with permission).
6+
7+
.. [1] http://www.huyng.com/posts/sane-color-scheme-for-matplotlib/
8+
9+
.. _ggplot: http://had.co.nz/ggplot/
10+
.. _R: http://www.r-project.org/
11+
12+
"""
13+
importnumpyasnp
14+
importmatplotlib.pyplotasplt
15+
16+
plt.style.use('ggplot')
17+
18+
fig,axes=plt.subplots(ncols=2,nrows=2)
19+
ax1,ax2,ax3,ax4=axes.ravel()
20+
21+
# scatter plot (Note: `plt.scatter` doesn't use default colors)
22+
x,y=np.random.normal(size=(2,200))
23+
ax1.plot(x,y,'o')
24+
25+
# sinusoidal lines with colors from default color cycle
26+
L=2*np.pi
27+
x=np.linspace(0,L)
28+
ncolors=len(plt.rcParams['axes.color_cycle'])
29+
shift=np.linspace(0,L,ncolors,endpoint=False)
30+
forsinshift:
31+
ax2.plot(x,np.sin(x+s),'-')
32+
ax2.margins(0)
33+
34+
# bar graphs
35+
x=np.arange(5)
36+
y1,y2=np.random.randint(1,25,size=(2,5))
37+
width=0.25
38+
ax3.bar(x,y1,width)
39+
ax3.bar(x+width,y2,width,color=plt.rcParams['axes.color_cycle'][2])
40+
ax3.set_xticks(x+width)
41+
ax3.set_xticklabels(['a','b','c','d','e'])
42+
43+
# circles with colors from default color cycle
44+
fori,colorinenumerate(plt.rcParams['axes.color_cycle']):
45+
xy=np.random.normal(size=2)
46+
ax4.add_patch(plt.Circle(xy,radius=0.3,color=color))
47+
ax4.axis('equal')
48+
ax4.margins(0)
49+
50+
plt.show()
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
This example demonstrates the "grayscale" style sheet, which changes all colors
3+
that are defined as rc parameters to grayscale. Note, however, that not all
4+
plot elements default to colors defined by an rc parameter.
5+
6+
"""
7+
importnumpyasnp
8+
importmatplotlib.pyplotasplt
9+
10+
11+
defcolor_cycle_example(ax):
12+
L=6
13+
x=np.linspace(0,L)
14+
ncolors=len(plt.rcParams['axes.color_cycle'])
15+
shift=np.linspace(0,L,ncolors,endpoint=False)
16+
forsinshift:
17+
ax.plot(x,np.sin(x+s),'o-')
18+
19+
defimage_and_patch_example(ax):
20+
ax.imshow(np.random.random(size=(20,20)),interpolation='none')
21+
c=plt.Circle((5,5),radius=5,label='patch')
22+
ax.add_patch(c)
23+
24+
25+
plt.style.use('grayscale')
26+
27+
fig, (ax1,ax2)=plt.subplots(ncols=2)
28+
29+
color_cycle_example(ax1)
30+
image_and_patch_example(ax2)
31+
32+
plt.show()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp