|
| 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/ |