-
-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Add easy style sheet selection #2236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
643c74b
Add easy style sheet selection.
tonysyu 3270aa4
Add rc_params_in_file to return partially-filled RcParams
tonysyu d83a03c
Rename style files to `*.style`
tonysyu c8cc486
Allow style.use to open URLs
tonysyu 455b54c
Remove pyplot import
tonysyu 7769b29
Add style context manager and tests
tonysyu 3914089
Change style extension to *.mplstyle
tonysyu c3fae2e
Got a little crazy with the whitespace
tonysyu a3de231
Add style package and data to setupext.py
tonysyu d56f73e
Move test so that it actually runs.
tonysyu ec6ce6b
Use explicit string check
tonysyu 5fdc037
Hide rc_params_in_file from parent namespace
tonysyu 0c7437c
Remove usage of import *
tonysyu 200d2e0
Clarify docstring
tonysyu 7392ce6
added `matplotlib.style` to pyplot import list
tacaswell ea63c99
fix url rc specification
adrn eaa23ee
fixed divergent naming scheme
tacaswell 5f80ca1
pep8 clean up
tacaswell f5ecf5e
Add docs for style package
tonysyu a8ef5bf
Import style package for easy use.
tonysyu dc291e0
Use style package from pyplot
tonysyu c5b5bb4
Fix test
tonysyu 7ac26ee
pep8
tacaswell 46a725b
Clear style settings between tests
mdboom d372a35
Add note that style sheets are experimental.
tonysyu e714c77
added python3 emulation code + six + fixed up print calls
tacaswell 512b77c
removed unneeded print statements
tacaswell 17282c8
Attempt to fix python 3 test errors on Travis CI
tonysyu a6142fc
Remove test from list to test Travis CI failure.
tonysyu 19e7bed
Remove test file to test Travis CI failure
tonysyu c604498
Revert commits used to test Travis CI test failures.
tonysyu 0b098e2
Fix import for python 3
tonysyu 7e2bffb
Use iteritems from `six` module
tonysyu 1d87f34
Add compatibility layer for Python 3's urlopen
tonysyu 79f83c9
Fix _fix_url on Python 2.6
mdboom 246c348
Merge pull request #6 from mdboom/style/py26-fixes
tonysyu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
.. _style-sheets | ||
|
||
*********************************** | ||
Customizing plots with style sheets | ||
*********************************** | ||
|
||
|
||
The ``style`` package adds support for easy-to-switch plotting "styles" with | ||
the same parameters as a matplotlibrc_ file. | ||
|
||
There are a number of pre-defined styles provided by matplotlib. For | ||
example, there's a pre-defined style called "ggplot", which emulates the | ||
aesthetics of ggplot_ (a popular plotting package for R_). To use this style, | ||
just add:: | ||
|
||
>>> import matplotlib.pyplot as plt | ||
>>> plt.style.use('ggplot') | ||
|
||
To list all available styles, use:: | ||
|
||
>>> print plt.style.available | ||
|
||
|
||
Defining your own style | ||
======================= | ||
|
||
You can create custom styles and use them by calling ``style.use`` with the | ||
path or URL to the style sheet. Alternatively, if you add your | ||
``<style-name>.mplstyle`` file to ``~/.matplotlib/stylelib`` (you may need to | ||
create this directory), you can reuse your custom style sheet with a call to | ||
``style.use(<style-name>)``. Note that a custom style sheet in | ||
``~/.matplotlib/stylelib`` will override a style sheet defined by matplotlib if | ||
the styles have the same name. | ||
|
||
For example, you might want to create | ||
``~/.matplotlib/stylelib/presentation.mplstyle`` with the following:: | ||
|
||
axes.titlesize : 24 | ||
axes.labelsize : 20 | ||
lines.linewidth : 3 | ||
lines.markersize : 10 | ||
xtick.labelsize : 16 | ||
ytick.labelsize : 16 | ||
|
||
Then, when you want to adapt a plot designed for a paper to one that looks | ||
good in a presentation, you can just add:: | ||
|
||
>>> import matplotlib.pyplot as plt | ||
>>> plt.style.use('presentation') | ||
|
||
|
||
Composing styles | ||
================ | ||
|
||
Style sheets are designed to be composed together. So you can have a style | ||
sheet that customizes colors and a separate style sheet that alters element | ||
sizes for presentations. These styles can easily be combined by passing | ||
a list of styles:: | ||
|
||
>>> import matplotlib.pyplot as plt | ||
>>> plt.style.use(['dark_background', 'presentation']) | ||
|
||
Note that styles further to the right will overwrite values that are already | ||
defined by styles on the right. | ||
|
||
|
||
Temporary styling | ||
================= | ||
|
||
If you only want to use a style for a specific block of code but don't want | ||
to change the global styling, the style package provides a context manager | ||
for limiting your changes to a specific scope. To isolate the your styling | ||
changes, you can write something like the following:: | ||
|
||
|
||
>>> import numpy as np | ||
>>> import matplotlib.pyplot as plt | ||
>>> | ||
>>> with plt.style.context(('dark_background')): | ||
>>> plt.plot(np.sin(np.linspace(0, 2*np.pi)), 'r-o') | ||
>>> | ||
>>> # Some plotting code with the default style | ||
>>> | ||
>>> plt.show() | ||
|
||
|
||
.. _matplotlibrc: http://matplotlib.sourceforge.net/users/customizing.html | ||
.. _ggplot: http://had.co.nz/ggplot/ | ||
.. _R: http://www.r-project.org/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
""" | ||
This example demonstrates the "dark_background" style, which uses white for | ||
elements that are typically black (text, borders, etc). Note, however, that not | ||
all plot elements default to colors defined by an rc parameter. | ||
|
||
""" | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
|
||
|
||
plt.style.use('dark_background') | ||
|
||
L = 6 | ||
x = np.linspace(0, L) | ||
ncolors = len(plt.rcParams['axes.color_cycle']) | ||
shift = np.linspace(0, L, ncolors, endpoint=False) | ||
for s in shift: | ||
plt.plot(x, np.sin(x + s), 'o-') | ||
plt.xlabel('x-axis') | ||
plt.ylabel('y-axis') | ||
plt.title('title') | ||
|
||
plt.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
""" | ||
This example demonstrates the "ggplot" style, which adjusts the style to | ||
emulate ggplot_ (a popular plotting package for R_). | ||
|
||
These settings were shamelessly stolen from [1]_ (with permission). | ||
|
||
.. [1] http://www.huyng.com/posts/sane-color-scheme-for-matplotlib/ | ||
|
||
.. _ggplot: http://had.co.nz/ggplot/ | ||
.. _R: http://www.r-project.org/ | ||
|
||
""" | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
|
||
plt.style.use('ggplot') | ||
|
||
fig, axes = plt.subplots(ncols=2, nrows=2) | ||
ax1, ax2, ax3, ax4 = axes.ravel() | ||
|
||
# scatter plot (Note: `plt.scatter` doesn't use default colors) | ||
x, y = np.random.normal(size=(2, 200)) | ||
ax1.plot(x, y, 'o') | ||
|
||
# sinusoidal lines with colors from default color cycle | ||
L = 2*np.pi | ||
x = np.linspace(0, L) | ||
ncolors = len(plt.rcParams['axes.color_cycle']) | ||
shift = np.linspace(0, L, ncolors, endpoint=False) | ||
for s in shift: | ||
ax2.plot(x, np.sin(x + s), '-') | ||
ax2.margins(0) | ||
|
||
# bar graphs | ||
x = np.arange(5) | ||
y1, y2 = np.random.randint(1, 25, size=(2, 5)) | ||
width = 0.25 | ||
ax3.bar(x, y1, width) | ||
ax3.bar(x+width, y2, width, color=plt.rcParams['axes.color_cycle'][2]) | ||
ax3.set_xticks(x+width) | ||
ax3.set_xticklabels(['a', 'b', 'c', 'd', 'e']) | ||
|
||
# circles with colors from default color cycle | ||
for i, color in enumerate(plt.rcParams['axes.color_cycle']): | ||
xy = np.random.normal(size=2) | ||
ax4.add_patch(plt.Circle(xy, radius=0.3, color=color)) | ||
ax4.axis('equal') | ||
ax4.margins(0) | ||
|
||
plt.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
""" | ||
This example demonstrates the "grayscale" style sheet, which changes all colors | ||
that are defined as rc parameters to grayscale. Note, however, that not all | ||
plot elements default to colors defined by an rc parameter. | ||
|
||
""" | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
|
||
|
||
def color_cycle_example(ax): | ||
L = 6 | ||
x = np.linspace(0, L) | ||
ncolors = len(plt.rcParams['axes.color_cycle']) | ||
shift = np.linspace(0, L, ncolors, endpoint=False) | ||
for s in shift: | ||
ax.plot(x, np.sin(x + s), 'o-') | ||
|
||
def image_and_patch_example(ax): | ||
ax.imshow(np.random.random(size=(20, 20)), interpolation='none') | ||
c = plt.Circle((5, 5), radius=5, label='patch') | ||
ax.add_patch(c) | ||
|
||
|
||
plt.style.use('grayscale') | ||
|
||
fig, (ax1, ax2) = plt.subplots(ncols=2) | ||
|
||
color_cycle_example(ax1) | ||
image_and_patch_example(ax2) | ||
|
||
plt.show() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to have an inline plot here. Doesn't have to be done before merge, just a note to self, really.