The standard RC parameters handling in Matplotlib has always troubled me. The
syntax rc('figure.subplot', top=0.9) is not very conveniet if one wants to
change a singe property. Direct rcParams['figure.subplot.top'] seems better
suited in this case.
However, as the dots are already used to indicate grouping in RC, it seems
very natural to use the syntax like:
rc.figure.subplot.top = 0.9
In my opinion this is very elegant, efficient and much Pythonic approach.
In the attachment I include a path to the current git main repo, which enables
this way of handling RC properties. I would appreciate very much if they were
reviewed and included in the next release of Matplotlib (the patch is not
particularly large).
Best regrds,
Maciek
--
Maciek Dems http://dems.art.pl/>From a829a9d17f9c302e44309219b29855c9bafdacbd Mon Sep 17 00:00:00 2001
From: Maciek Dems
Date: Tue, 10 Jul 2012 15:16:16 +0200
Subject: [PATCH] Added extended rc handling, enabling elegant Pythonic
'rc.figure.subplot.left = 0.1' syntax.
---
lib/matplotlib/__init__.py | 86 +++-
lib/matplotlib/pyplot.py |4 +--
2 files changed, 70 insertions(+), 20 deletions(-)
diff --git a/lib/matplotlib/__init__.py b/lib/matplotlib/__init__.py
index 5eb9e77..61c 100644
--- a/lib/matplotlib/__init__.py
+++ b/lib/matplotlib/__init__.py
@@ -812,13 +812,39 @@ if rcParams['axes.formatter.use_locale']:
import locale
locale.setlocale(locale.LC_ALL, '')
-def rc(group, **kwargs):
+class _SubRc(object):
+def __init__(self, group):
+self.__dict__['_group'] = group
+
+def __setattr__(self, attr, value):
+name = _Rc.aliases.get(attr) or attr
+key = self._group + '.' + name
+if key not in rcParams:
+raise KeyError('Unrecognized key "%s"' % key)
+rcParams[key] = value
+
+def __getattr__(self, attr):
+name = _Rc.aliases.get(attr) or attr
+newgroup = self._group + '.' + name
+return _SubRc(newgroup)
+
+def __repr__(self):
+ if self._group not in rcParams:
+raise KeyError('Unrecognized key "%s"' % self._group)
+ return repr(rcParams[self._group])
+
+class _Rc(object):
"""
-Set the current rc params. Group is the grouping for the rc, eg.
-for ``lines.linewidth`` the group is ``lines``, for
-``axes.facecolor``, the group is ``axes``, and so on. Group may
-also be a list or tuple of group names, eg. (*xtick*, *ytick*).
-*kwargs* is a dictionary attribute name/value pairs, eg::
+Set the current rc params. There are two alternative ways of using
+this object. One is to call it like a function::
+
+ rc(group, **kwargs)
+
+Group is the grouping for the rc, eg. for ``lines.linewidth``
+the group is ``lines``, for ``axes.facecolor``, the group is ``axes``,
+and so on. Group may also be a list or tuple of group names,
+eg. (*xtick*, *ytick*). *kwargs* is a dictionary attribute name/value
+pairs, eg::
rc('lines', linewidth=2, color='r')
@@ -840,6 +866,7 @@ def rc(group, **kwargs):
'ec''edgecolor'
'mew' 'markeredgewidth'
'aa''antialiased'
+'sans' 'sans-serif'
= =
Thus you could abbreviate the above rc command as::
@@ -860,6 +887,14 @@ def rc(group, **kwargs):
This enables you to easily switch between several configurations.
Use :func:`~matplotlib.pyplot.rcdefaults` to restore the default
rc params after changes.
+
+Another way of using this object is to use the Python syntax like::
+
+ rc.figure.subplot.top = 0.9
+
+which is equivalent to::
+
+ rc('figure.subplot', top=0.9)
"""
aliases = {
@@ -870,19 +905,36 @@ def rc(group, **kwargs):
'ec' : 'edgecolor',
'mew' : 'markeredgewidth',
'aa' : 'antialiased',
-}
+'sans': 'sans-serif'
+}
-if is_string_like(group):
-group = (group,)
-for g in group:
-for k,v in kwargs.iteritems():
-name = aliases.get(k) or k
-key = '%s.%s' % (g, name)
-try:
+def __call__(self, group, **kwargs):
+if matplotlib.is_string_like(group):
+group = (group,)
+for g in group:
+for k,v in kwargs.items():
+name = _Rc.aliases.get(k) or k
+key = '%s.%s' % (g, name)
+if key not in rcParams:
+raise KeyError('Unrecognized key "%s" for group "%s" and name "%s"' %
+