Tony S Yu wrote:
> On Sep 26, 2008, at 5:01 PM, Eric Firing wrote:
>> Also, if an image cannot be resolved by the output device, info is
>> lost--one might not see anything at a location where there actually is
>> a value--whereas with markers, a marker will always show up, and the
>> only problem is that one can't necessarily distinguish a single point
>> from a cluster.
>> The real problem with all-zero values is that plot can't handle
>> "plot(,)". One can work around this by putting in bogus values to
>> plot a single point, saving the line, and then setting the line data
>> to empty; or, better, by not using the high-level plot command, but by
>> generating the Line2D object and adding it to the axes. The Line2D
>> initializer is happy with empty x and y sequences. I think if you use
>> this approach it will kill two bugs (failure on all-zeros with sparse
>> and full arrays) with one very simple stone.
> Thanks for the tip Eric. Below is a patch for spy that implements Eric's
> suggestion. This patch seems to work for a couple simple tests on my
> end: sparse and dense arrays with non-zero and all-zero values.
> A couple of notes:
> * the call to `add_artist` isn't needed to show the correct plot, but it
> may be helpful for debugging.
> * the docstring for `spy` suggests that a Line2D instance is returned,
> but `spy` currently returns a list with a Line2D instance. I set
> all-zero arrays to return a list also, for consistency.
Changes to spy and a few other things are in svn 6127.
Regarding your last point, the docstring made more sense than the
original implementation, so I changed the implementation to correspond
to it. I hope this does not cause more trouble than it is worth; if it
looks like it will, then I can easily change the behavior back and
modify the docstring. It seems silly to always return a list with a
single item, though, and I doubt that many people are making heavy use
of the return value of the spy method anyway.
Regarding your original idea, that sparse arrays should be handled like
ordinary arrays, with only nonzero values plotted: I think this is
going too far, and not far enough, so I did the following:
1) If "precision" is None or a non-zero value, the behavior for sparse
and ordinary arrays is identical. Previously, the precision kwarg was
silently ignored for sparse arrays. Now it is used.
2) If "precision" is 0, then one gets the old behavior: all locations
with data are shown, regardless of value. It seems to me that one
really wants to have this behavior available, to see how much of a
sparse array is filled in.
I am not entirely comfortable with the way the "precision" kwarg is
being used to control this, but it seemed preferable to adding another
kwarg. Alternatives could include swapping the roles of 0 and None, or
letting precision take a string value to specify the old behavior.
Actually, I think the most logical thing would be to let the default
None give the old behavior, and require precision=0 to get the new
behavior. What do you think? Is it OK if I make this change? It is
more consistent with the old behavior.
I also changed the behavior so that if a sparse array is input, with no
marker specifications, it simply makes a default marker plot instead of
raising an exception.
> Index: matplotlib/lib/matplotlib/axes.py
> --- matplotlib/lib/matplotlib/axes.py (revision 6123)
> +++ matplotlib/lib/matplotlib/axes.py (working copy)
> @@ -6723,9 +6723,9 @@
> if hasattr(Z, 'tocoo'):
> c = Z.tocoo()
> - y = c.row
> - x = c.col
> - z = c.data
> + nonzero = c.data != 0.
> + y = c.row[nonzero]
> + x = c.col[nonzero]
> Z = np.asarray(Z)
> if precision is None: mask = Z!=0.
> @@ -6733,8 +6733,12 @@
> y,x,z = mlab.get_xyz_where(mask, mask)
> if marker is None: marker = 's'
> if markersize is None: markersize = 10
> - lines = self.plot(x, y, linestyle='None',
> - marker=marker, markersize=markersize, **kwargs)
> + if len(x) == 0:
> + lines = [mlines.Line2D(, )]
> + self.add_artist(lines)
> + else:
> + lines = self.plot(x, y, linestyle='None',
> + marker=marker, markersize=markersize,
> nr, nc = Z.shape
> self.set_xlim(xmin=-0.5, xmax=nc-0.5)
> self.set_ylim(ymin=nr-0.5, ymax=-0.5)
> Index: matplotlib/examples/pylab_examples/masked_demo.py
> --- matplotlib/examples/pylab_examples/masked_demo.py (revision 6123)
> +++ matplotlib/examples/pylab_examples/masked_demo.py (working copy)
> @@ -1,4 +1,4 @@
> -#!/bin/env python
> +#!/usr/bin/env python
> Plot lines with points masked out.
> Index: matplotlib/examples/misc/rec_groupby_demo.py
> --- matplotlib/examples/misc/rec_groupby_demo.py (revision 6123)
> +++ matplotlib/examples/misc/rec_groupby_demo.py (working copy)
> @@ -2,7 +2,7 @@
> import matplotlib.mlab as mlab
> -r = mlab.csv2rec('data/aapl.csv')
> +r = mlab.csv2rec('../data/aapl.csv')
> def daily_return(prices):