Skip to content

Commit f6edd17

Browse files
author
y-p
committed
ENH: add option display.with_mpl_style GH3075
1 parent a21cdb4 commit f6edd17

File tree

5 files changed

+128
-1
lines changed

5 files changed

+128
-1
lines changed

RELEASE.rst

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ pandas 0.11.0
112112
113113
df = DataFrame(dict(A = ts))
114114
df['2001']
115+
- added option `display.mpl_style` providing a sleeker visual style
116+
for plots. Based on https://gist.github.com/huyng/816622 (GH3075_).
117+
115118

116119
**API Changes**
117120

@@ -239,9 +242,11 @@ pandas 0.11.0
239242
.. _GH2807: https://github.com/pydata/pandas/issues/2807
240243
.. _GH2918: https://github.com/pydata/pandas/issues/2918
241244
.. _GH3011: https://github.com/pydata/pandas/issues/3011
245+
.. _GH2745: https://github.com/pydata/pandas/issues/2745
246+
.. _GH622: https://github.com/pydata/pandas/issues/622
247+
.. _GH797: https://github.com/pydata/pandas/issues/797
242248
.. _GH2681: https://github.com/pydata/pandas/issues/2681
243249
.. _GH2719: https://github.com/pydata/pandas/issues/2719
244-
.. _GH2745: https://github.com/pydata/pandas/issues/2745
245250
.. _GH2746: https://github.com/pydata/pandas/issues/2746
246251
.. _GH2747: https://github.com/pydata/pandas/issues/2747
247252
.. _GH2751: https://github.com/pydata/pandas/issues/2751
@@ -282,7 +287,10 @@ pandas 0.11.0
282287
.. _GH3076: https://github.com/pydata/pandas/issues/3076
283288
.. _GH3063: https://github.com/pydata/pandas/issues/3063
284289
.. _GH3059: https://github.com/pydata/pandas/issues/3059
290+
.. _GH2993: https://github.com/pydata/pandas/issues/2993
285291
.. _GH3115: https://github.com/pydata/pandas/issues/3115
292+
.. _GH3070: https://github.com/pydata/pandas/issues/3070
293+
.. _GH3075: https://github.com/pydata/pandas/issues/3075
286294
.. _GH3130: https://github.com/pydata/pandas/issues/3130
287295

288296
pandas 0.10.1

doc/source/v0.11.0.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ Enhancements
301301
- DataFrame.from_records now accepts not only dicts but any instance of
302302
the collections.Mapping ABC.
303303

304+
- added option `display.with_wmp_style` providing a sleeker visual style
305+
for plots. Based on https://gist.github.com/huyng/816622 (GH3075_).
304306

305307
See the `full release notes
306308
<https://github.com/pydata/pandas/blob/master/RELEASE.rst>`__ or issue tracker
@@ -325,3 +327,4 @@ on GitHub for a complete list.
325327
.. _GH3076: https://github.com/pydata/pandas/issues/3076
326328
.. _GH3059: https://github.com/pydata/pandas/issues/3059
327329
.. _GH3070: https://github.com/pydata/pandas/issues/3070
330+
.. _GH3075: https://github.com/pydata/pandas/issues/3075

pandas/core/config_init.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,38 @@
147147
perform a null check when repr'ing.
148148
"""
149149

150+
pc_mpl_style_doc = """
151+
: bool
152+
153+
Setting this to 'default' will modify the rcParams used by matplotlib
154+
to give plots a more pleasing visual style by default.
155+
Setting this to None/False restores the values to their initial value.
156+
"""
157+
158+
style_backup = dict()
159+
def mpl_style_cb(key):
160+
import sys
161+
from pandas.tools.plotting import mpl_stylesheet
162+
global style_backup
163+
164+
val = cf.get_option(key)
165+
166+
if 'matplotlib' not in sys.modules.keys():
167+
if not(key): # starting up, we get reset to None
168+
return val
169+
raise Exception("matplotlib has not been imported. aborting")
170+
171+
import matplotlib.pyplot as plt
172+
173+
174+
if val == 'default':
175+
style_backup = dict([(k,plt.rcParams[k]) for k in mpl_stylesheet])
176+
plt.rcParams.update(mpl_stylesheet)
177+
elif not val:
178+
if style_backup:
179+
plt.rcParams.update(style_backup)
180+
181+
return val
150182

151183
with cf.config_prefix('display'):
152184
cf.register_option('precision', 7, pc_precision_doc, validator=is_int)
@@ -177,6 +209,9 @@
177209
cf.register_option('line_width', 80, pc_line_width_doc)
178210
cf.register_option('chop_threshold', None, pc_chop_threshold_doc)
179211
cf.register_option('max_seq_items', None, pc_max_seq_items)
212+
cf.register_option('mpl_style', None, pc_mpl_style_doc,
213+
validator=is_one_of_factory([None, False, 'default']),
214+
cb=mpl_style_cb)
180215

181216
tc_sim_interactive_doc = """
182217
: boolean

pandas/tests/test_graphics.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from pandas import Series, DataFrame, MultiIndex, PeriodIndex, date_range
99
import pandas.util.testing as tm
1010
from pandas.util.testing import ensure_clean
11+
from pandas.core.config import set_option,get_option,config_prefix
1112

1213
import numpy as np
1314

@@ -693,6 +694,21 @@ def test_grouped_hist(self):
693694
for ax in axes.ravel():
694695
self.assert_(len(ax.patches) > 0)
695696

697+
def test_option_mpl_style(self):
698+
# just a sanity check
699+
try:
700+
import matplotlib
701+
except:
702+
raise nose.SkipTest
703+
704+
set_option('display.mpl_style', 'default')
705+
set_option('display.mpl_style', None)
706+
set_option('display.mpl_style', False)
707+
try:
708+
set_option('display.mpl_style', 'default2')
709+
except ValueError:
710+
pass
711+
696712
def _check_plot_works(f, *args, **kwargs):
697713
import matplotlib.pyplot as plt
698714

pandas/tools/plotting.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,71 @@
2323
except ImportError:
2424
pass
2525

26+
# Extracted from https://gist.github.com/huyng/816622
27+
# this is the rcParams set when setting display.with_mpl_style
28+
# to True.
29+
mpl_stylesheet = {
30+
'axes.axisbelow': True,
31+
'axes.color_cycle': ['#348ABD',
32+
'#7A68A6',
33+
'#A60628',
34+
'#467821',
35+
'#CF4457',
36+
'#188487',
37+
'#E24A33'],
38+
'axes.edgecolor': '#bcbcbc',
39+
'axes.facecolor': '#eeeeee',
40+
'axes.grid': True,
41+
'axes.labelcolor': '#555555',
42+
'axes.labelsize': 'large',
43+
'axes.linewidth': 1.0,
44+
'axes.titlesize': 'x-large',
45+
'figure.edgecolor': 'white',
46+
'figure.facecolor': 'white',
47+
'figure.figsize': (6.0, 4.0),
48+
'figure.subplot.hspace': 0.5,
49+
'font.family': 'monospace',
50+
'font.monospace': ['Andale Mono',
51+
'Nimbus Mono L',
52+
'Courier New',
53+
'Courier',
54+
'Fixed',
55+
'Terminal',
56+
'monospace'],
57+
'font.size': 10,
58+
'interactive': True,
59+
'keymap.all_axes': ['a'],
60+
'keymap.back': ['left', 'c', 'backspace'],
61+
'keymap.forward': ['right', 'v'],
62+
'keymap.fullscreen': ['f'],
63+
'keymap.grid': ['g'],
64+
'keymap.home': ['h', 'r', 'home'],
65+
'keymap.pan': ['p'],
66+
'keymap.save': ['s'],
67+
'keymap.xscale': ['L', 'k'],
68+
'keymap.yscale': ['l'],
69+
'keymap.zoom': ['o'],
70+
'legend.fancybox': True,
71+
'lines.antialiased': True,
72+
'lines.linewidth': 1.0,
73+
'patch.antialiased': True,
74+
'patch.edgecolor': '#EEEEEE',
75+
'patch.facecolor': '#348ABD',
76+
'patch.linewidth': 0.5,
77+
'toolbar': 'toolbar2',
78+
'xtick.color': '#555555',
79+
'xtick.direction': 'in',
80+
'xtick.major.pad': 6.0,
81+
'xtick.major.size': 0.0,
82+
'xtick.minor.pad': 6.0,
83+
'xtick.minor.size': 0.0,
84+
'ytick.color': '#555555',
85+
'ytick.direction': 'in',
86+
'ytick.major.pad': 6.0,
87+
'ytick.major.size': 0.0,
88+
'ytick.minor.pad': 6.0,
89+
'ytick.minor.size': 0.0
90+
}
2691

2792
def _get_standard_kind(kind):
2893
return {'density': 'kde'}.get(kind, kind)

0 commit comments

Comments
 (0)