Skip to content

Commit aa5d9b5

Browse files
committed
ENH: add pd.plot_params.use as contextmanager to temporarily set a plot parameter #2205
1 parent 8d45dc4 commit aa5d9b5

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

pandas/tests/test_graphics.py

+12
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,18 @@ def test_xcompat(self):
255255
lines = ax.get_lines()
256256
self.assert_(isinstance(lines[0].get_xdata(), PeriodIndex))
257257

258+
plt.close('all')
259+
#useful if you're plotting a bunch together
260+
with pd.plot_params.use('x_compat', True):
261+
ax = df.plot()
262+
lines = ax.get_lines()
263+
self.assert_(not isinstance(lines[0].get_xdata(), PeriodIndex))
264+
265+
plt.close('all')
266+
ax = df.plot()
267+
lines = ax.get_lines()
268+
self.assert_(isinstance(lines[0].get_xdata(), PeriodIndex))
269+
258270
def _check_data(self, xp, rs):
259271
xp_lines = xp.get_lines()
260272
rs_lines = rs.get_lines()

pandas/tools/plotting.py

+28
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import datetime
55
import warnings
66
import re
7+
from contextlib import contextmanager
78

89
import numpy as np
910

@@ -27,6 +28,12 @@ def _get_standard_kind(kind):
2728

2829

2930
class _Options(dict):
31+
"""
32+
Stores pandas plotting options.
33+
Allows for parameter aliasing so you can just use parameter names that are
34+
the same as the plot function parameters, but is stored in a canonical
35+
format that makes it easy to breakdown into groups later
36+
"""
3037

3138
#alias so the names are same as plotting method parameter names
3239
_ALIASES = {'x_compat' : 'xaxis.compat'}
@@ -56,11 +63,32 @@ def __contains__(self, key):
5663
return super(_Options, self).__contains__(key)
5764

5865
def reset(self):
66+
"""
67+
Reset the option store to its initial state
68+
69+
Returns
70+
-------
71+
None
72+
"""
5973
self.__init__()
6074

6175
def _get_canonical_key(self, key):
6276
return self._ALIASES.get(key, key)
6377

78+
@contextmanager
79+
def use(self, key, value):
80+
"""
81+
Temporarily set a parameter value using the with statement.
82+
Aliasing allowed.
83+
"""
84+
old_value = self[key]
85+
try:
86+
self[key] = value
87+
yield self
88+
finally:
89+
self[key] = old_value
90+
91+
6492
plot_params = _Options()
6593

6694
def scatter_matrix(frame, alpha=0.5, figsize=None, ax=None, grid=False,

0 commit comments

Comments
 (0)