[matplotlib-devel] Patch: more Pythonic rc handling

2012-07-10 Thread Maciek Dems
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"' %
+  

Re: [matplotlib-devel] Patch: more Pythonic rc handling

2012-07-11 Thread Maciek Dems
Dnia wtorek, 10 lipca 2012 21:32:40 Maciek Dems pisze:
> 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).

I have posted the path to the github fork, as described in developer manual of 
matplotlib. The most recent changes are here:

http://github.com/macdems/matplotlib/compare/master...better-rc

Best regards,
Maciek

-- 
Maciek Dems http://dems.art.pl/en/

--
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
___
Matplotlib-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel


Re: [matplotlib-devel] Misalignment imshow vs. grid lines

2012-10-30 Thread Maciek Dems
In reply to message from Nicolas Rougier, dated Tuesday 30 of October 2012, 
on subject "Re: [matplotlib-devel] Misalignment imshow vs. grid lines"
> You're right. Using 'none' interpolation seems to solve the problem. Good
> to know !

Unfortunately it does not! It only makes problem less pronounced, but still 
present. Furthermore pcolor is also affected, similarly to imshow, however, 
the misalignment is usually no more than one pixel (although in some 
applications it is still unacceptable).

I guess that the problem is with truncation errors in some calculations...

Here are some test scripts and sample results. Mind that the misalignment 
depend randomly on zoom factor and the position of the image.

The scripts:

# test_imshow.py
# --
import numpy as np
import matplotlib.pyplot as plt

n = 16
fig = plt.figure(figsize=(10,10))

Z = np.ones((n, n))
Z[::2, ::2] = 2
Z[1::2, 1::2] = 2

def test(interp, sub):
plt.subplot(sub)

plt.imshow(Z, interpolation=interp,
cmap='gray', extent=[0, n, 0, n], vmin=0)

plt.xticks(np.arange(n))
plt.yticks(np.arange(n))
plt.grid(ls='solid')
plt.title(interp)

test('nearest', 121)
test('none', 122)

plt.show()



# test_pcolor.py
# --
import numpy as np
import matplotlib.pyplot as plt

n = 16
fig = plt.figure(figsize=(10,10))

Z = np.ones((n, n))
Z[::2, ::2] = 2
Z[1::2, 1::2] = 2

plt.pcolor(np.arange(n+1), np.arange(n+1), Z,
cmap='gray', vmin=0)

plt.xticks(np.arange(n))
plt.yticks(np.arange(n))
plt.grid(ls='solid')

plt.show()



The results (some manual zooming) are attached and also available here:
http://dems.art.pl/files/imshow.png
http://dems.art.pl/files/pcolor.png

-- 
Maciek Dems http://dems.art.pl/en
<><>--
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_sfd2d_oct___
Matplotlib-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel