Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9ed9709

Browse files
committed
updated span selector example
svn path=/trunk/matplotlib/; revision=3392
1 parent 339c86f commit 9ed9709

3 files changed

Lines changed: 32 additions & 20 deletions

File tree

examples/widgets/span_selector.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,40 @@
11
#!/usr/bin/env python
22
"""
3-
The SpanSelector is a mouse widget to select a vmin/vmax
4-
range. When you left click drag in the axes, a rectangle shows the
5-
selected region. When you release, the rectangle disappears and a
6-
callback is called with min/max.
3+
The SpanSelector is a mouse widget to select a xmin/xmax range and plot the
4+
detail view of the selected region in the lower axes
75
"""
8-
import pylab
6+
import numpy as npy
7+
from pylab import figure, show
98
from matplotlib.widgets import SpanSelector
109

11-
fig = pylab.figure(figsize=(8,6))
10+
fig = figure(figsize=(8,6))
1211
ax = fig.add_subplot(211, axisbg='#FFFFCC')
1312

14-
x,y = 4*(pylab.rand(2,100)-.5)
15-
ax.plot(x,y,'o')
16-
ax.set_xlim(-2,2)
13+
x = npy.arange(0.0, 5.0, 0.01)
14+
y = npy.sin(2*npy.pi*x) + 0.5*npy.random.randn(len(x))
15+
16+
ax.plot(x, y, '-')
1717
ax.set_ylim(-2,2)
1818
ax.set_title('Press left mouse button and drag to test')
1919

20+
ax2 = fig.add_subplot(212, axisbg='#FFFFCC')
21+
line2, = ax2.plot(x, y, '-')
22+
2023

21-
def onselect(vmin, vmax):
22-
print vmin, vmax
24+
def onselect(xmin, xmax):
25+
indmin, indmax = npy.searchsorted(x, (xmin, xmax))
26+
indmax = min(len(x)-1, indmax)
27+
28+
thisx = x[indmin:indmax]
29+
thisy = y[indmin:indmax]
30+
line2.set_data(thisx, thisy)
31+
ax2.set_xlim(thisx[0], thisx[-1])
32+
ax2.set_ylim(thisy.min(), thisy.max())
33+
fig.canvas.draw()
2334

2435
# set useblit True on gtkagg for enhanced performance
2536
span = SpanSelector(ax, onselect, 'horizontal', useblit=False,
2637
rectprops=dict(alpha=0.5, facecolor='red') )
2738

28-
ax2 = fig.add_subplot(212)
29-
ax2.plot([1,2,3])
30-
31-
span2 = SpanSelector(ax2, onselect, 'vertical')
3239

33-
pylab.show()
40+
show()

lib/matplotlib/cbook.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515

1616

1717
class converter:
18+
"""
19+
Base class for handling string -> python type with support for
20+
missing values
21+
"""
1822
def __init__(self, missing='Null', missingval=None):
1923
self.missing = missing
2024
self.missingval = missingval
@@ -38,7 +42,7 @@ def __init__(self, fmt='%Y-%m-%d', missing='Null', missingval=None):
3842
self.fmt = fmt
3943

4044
def __call__(self, s):
41-
if self.is_missing(): return self.missingval
45+
if self.is_missing(s): return self.missingval
4246
tup = time.strptime(s, self.fmt)
4347
return datetime.datetime(*tup[:6])
4448

@@ -51,7 +55,7 @@ def __init__(self, fmt='%Y-%m-%d', missing='Null', missingval=None):
5155
converter.__init__(self, missing, missingval)
5256
self.fmt = fmt
5357
def __call__(self, s):
54-
if self.is_missing(): return self.missingval
58+
if self.is_missing(s): return self.missingval
5559
tup = time.strptime(s, self.fmt)
5660
return datetime.date(*tup[:3])
5761

@@ -61,7 +65,7 @@ def __init__(self, missing='Null', missingval=None):
6165
converter.__init__(self, missing)
6266
self.missingval = missingval
6367
def __call__(self, s):
64-
if self.is_missing(): return self.missingval
68+
if self.is_missing(s): return self.missingval
6569
return float(s)
6670

6771

@@ -71,7 +75,7 @@ def __init__(self, missing='Null', missingval=None):
7175
converter.__init__(self, missing)
7276

7377
def __call__(self, s):
74-
if self.is_missing(): return self.missingval
78+
if self.is_missing(s): return self.missingval
7579
return int(s)
7680

7781
class CallbackRegistry:

lib/matplotlib/mlab.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,6 +1444,7 @@ def get_converters(reader):
14441444
if func is None:
14451445
func = converterd.get(name)
14461446
if func is None:
1447+
if not item.strip(): continue
14471448
func = converters[j]
14481449
func = get_func(item, func)
14491450
converters[j] = func

0 commit comments

Comments
 (0)