Skip to content

Commit a9fe054

Browse files
committed
added spy and spy2
svn path=/trunk/matplotlib/; revision=681
1 parent 2a68b99 commit a9fe054

File tree

5 files changed

+101
-3
lines changed

5 files changed

+101
-3
lines changed

CHANGELOG

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
New entries should be added at the top
22

3+
2004-11-12 Added new plotting functions spy, spy2 for sparse matrix
4+
visualization - JDH
5+
6+
2004-11-11 Added rgrids, thetragrids for customizing the grid
7+
locations and labels for polar plots - JDH
8+
39
2004-11-11 make the Gtk backends build without an X-server connection - JV
410

511
2004-11-10 matplotlib/__init__.py: Added FROZEN to signal we are running under

lib/matplotlib/axes.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import division, generators
22

3-
import math, sys
3+
import math, sys, random
44

55
from numerix import MLab, absolute, arange, array, asarray, ones, transpose, \
66
log, log10, Float, ravel
@@ -11,7 +11,7 @@
1111
from cbook import iterable, is_string_like, flatten, enumerate, True, False,\
1212
allequal
1313
from collections import RegularPolyCollection, PolyCollection
14-
from colors import colorConverter, normalize, Colormap
14+
from colors import colorConverter, normalize, Colormap, LinearSegmentedColormap
1515
import cm
1616
from cm import ColormapJet, Grayscale
1717
import _image
@@ -2419,6 +2419,7 @@ def set_yticks(self, ticks):
24192419
'Set the y ticks with list of ticks'
24202420
return self.yaxis.set_ticks(ticks)
24212421

2422+
24222423
def specgram(self, x, NFFT=256, Fs=2, detrend=mlab.detrend_none,
24232424
window=mlab.window_hanning, noverlap=128,
24242425
cmap = None, xextent=None):
@@ -2466,6 +2467,37 @@ def specgram(self, x, NFFT=256, Fs=2, detrend=mlab.detrend_none,
24662467

24672468
return Pxx, freqs, bins, im
24682469

2470+
def spy(self, Z, marker='s', markersize=10, **kwargs):
2471+
"""
2472+
SPY(Z, **kwrags) plots the sparsity pattern of the matrix S
2473+
using plot markers.
2474+
2475+
kwargs give the marker properties - see help(plot) for more
2476+
information on marker properties
2477+
2478+
The line handles are returned
2479+
"""
2480+
x,y,z = mlab.get_xyz_where(Z, Z>0)
2481+
return self.plot(x+0.5,y+0.5, linestyle='None',
2482+
marker=marker,markersize=markersize, **kwargs)
2483+
2484+
def spy2(self, Z):
2485+
"""
2486+
SPY2(Z) plots the sparsity pattern of the matrix S as an image
2487+
2488+
The image instance is returned
2489+
"""
2490+
2491+
#binary colormap min white, max black
2492+
cmapdata = {
2493+
'red' : ((0., 1., 1.), (1., 0., 0.)),
2494+
'green': ((0., 1., 1.), (1., 0., 0.)),
2495+
'blue' : ((0., 1., 1.), (1., 0., 0.))
2496+
}
2497+
binary = LinearSegmentedColormap('binary', cmapdata, 2)
2498+
Z = where(Z>0,1.,0.)
2499+
return self.imshow(transpose(Z), interpolation='nearest', cmap=binary)
2500+
24692501
def table(self,
24702502
cellText=None, cellColours=None,
24712503
cellLoc='right', colWidths=None,

lib/matplotlib/matlab.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,31 @@ def specgram(*args, **kwargs):
10021002
return ret
10031003
specgram.__doc__ = Axes.specgram.__doc__
10041004

1005+
def spy(*args, **kwargs):
1006+
try: ret = gca().spy(*args, **kwargs)
1007+
except ValueError, msg:
1008+
msg = raise_msg_to_str(msg)
1009+
error_msg(msg)
1010+
else:
1011+
Pxx, freqs, bins, im = ret
1012+
gci._current = im
1013+
draw_if_interactive()
1014+
return ret
1015+
spy.__doc__ = Axes.spy.__doc__
1016+
1017+
1018+
def spy2(*args, **kwargs):
1019+
try: ret = gca().spy2(*args, **kwargs)
1020+
except ValueError, msg:
1021+
msg = raise_msg_to_str(msg)
1022+
error_msg(msg)
1023+
else:
1024+
Pxx, freqs, bins, im = ret
1025+
gci._current = im
1026+
draw_if_interactive()
1027+
return ret
1028+
spy2.__doc__ = Axes.spy2.__doc__
1029+
10051030
def subplot(*args, **kwargs):
10061031
"""
10071032
Create a subplot command, creating axes with

lib/matplotlib/mlab.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,40 @@ def bivariate_normal(X, Y, sigmax=1.0, sigmay=1.0,
11411141

11421142

11431143

1144+
def get_xyz_where(Z, Cond):
1145+
"""
1146+
Z and Cond are MxN matrices. Z are data and Cond is a boolean
1147+
matrix where some condition is satisfied. Return value is x,y,z
1148+
where x and y are the indices into Z and z are the values of Z at
1149+
those indices. x,y,z are 1D arrays
1150+
"""
1151+
1152+
M,N = Z.shape
1153+
z = ravel(Z)
1154+
ind = nonzero( ravel(Cond) )
1155+
1156+
x = arange(M); x.shape = M,1
1157+
X = repeat(x, N, 1)
1158+
x = ravel(X)
1159+
1160+
y = arange(N); y.shape = 1,N
1161+
Y = repeat(y, M)
1162+
y = ravel(Y)
1163+
1164+
x = take(x, ind)
1165+
y = take(y, ind)
1166+
z = take(z, ind)
1167+
return x,y,z
1168+
1169+
def get_sparse_matrix(M,N,frac=0.1):
1170+
'return a MxN sparse matrix with frac elements randomly filled'
1171+
data = zeros((M,N))*0.
1172+
for i in range(int(M*N*frac)):
1173+
x = random.randint(0,M-1)
1174+
y = random.randint(0,N-1)
1175+
data[x,y] = rand()
1176+
return data
1177+
11441178

11451179
### the following code was written and submitted by Fernando Perez
11461180
### from the ipython numutils package under a BSD license

unit/memleak_hawaii3.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import os, sys, time
44
import matplotlib
5-
matplotlib.use('Cairo')
5+
#matplotlib.use('Cairo')
6+
matplotlib.use('Agg')
67
from matplotlib.matlab import *
78

89

0 commit comments

Comments
 (0)