Skip to content

Commit d0ae284

Browse files
committed
refinements to the rec editors plus examples
svn path=/trunk/matplotlib/; revision=5100
1 parent a87f665 commit d0ae284

File tree

7 files changed

+373
-48
lines changed

7 files changed

+373
-48
lines changed

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2008-04-30 Added some record array editing widgets for gtk -- see
2+
examples/rec_edit*.py - JDH
3+
14
2008-04-29 Fix bug in mlab.sqrtm - MM
25

36
2008-04-28 Fix bug in SVG text with Mozilla-based viewers (the symbol

examples/data/demodata.csv

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
clientid,date,weekdays,gains,prices,up
2+
0,2008-04-30,Wed,-0.52458192906686452,7791404.0091921333,False
3+
1,2008-05-01,Thu,0.076191536201738269,3167180.7366340165,True
4+
2,2008-05-02,Fri,-0.86850970062880861,9589766.9613829032,False
5+
3,2008-05-03,Sat,-0.42701083852713395,8949415.1867596991,False
6+
4,2008-05-04,Sun,0.2532553652693274,937163.44375252665,True
7+
5,2008-05-05,Mon,-0.68151636911081892,949579.88022264629,False
8+
6,2008-05-06,Tue,0.0071911579626532168,7268426.906552773,True
9+
7,2008-05-07,Wed,0.67449747200412147,7517014.782897247,True
10+
8,2008-05-08,Thu,-1.1841008656818983,1920959.5423492221,False
11+
9,2008-05-09,Fri,-1.5803692595811152,8456240.6198725495,False

examples/date_demo.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""
2+
Simple example showing how to plot a time series with datetime objects
3+
"""
4+
import datetime
5+
import matplotlib.pyplot as plt
6+
7+
today = datetime.date.today()
8+
dates = [today+datetime.timedelta(days=i) for i in range(10)]
9+
10+
fig = plt.figure()
11+
ax = fig.add_subplot(111)
12+
ax.plot(dates, range(10))
13+
fig.autofmt_xdate()
14+
plt.show()

examples/rec_edit_gtk_custom.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
generate an editable gtk treeview widget for record arrays with custom
3+
formatting of the cells and show how to limit string entries to a list
4+
of strings
5+
"""
6+
import gtk
7+
import numpy as np
8+
import matplotlib.mlab as mlab
9+
import mpl_toolkits.gtktools as gtktools
10+
11+
r = mlab.csv2rec('data/demodata.csv', converterd={'weekdays':str})
12+
13+
14+
formatd = mlab.get_formatd(r)
15+
formatd['date'] = mlab.FormatDate('%Y-%m-%d')
16+
formatd['prices'] = mlab.FormatMillions(precision=1)
17+
formatd['gain'] = mlab.FormatPercent(precision=2)
18+
19+
# use a drop down combo for weekdays
20+
stringd = dict(weekdays=['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'])
21+
constant = ['clientid'] # block editing of this field
22+
23+
24+
liststore = gtktools.RecListStore(r, formatd=formatd, stringd=stringd)
25+
treeview = gtktools.RecTreeView(liststore, constant=constant)
26+
27+
def mycallback(liststore, rownum, colname, oldval, newval):
28+
print 'verify: old=%s, new=%s, rec=%s'%(oldval, newval, liststore.r[rownum][colname])
29+
30+
liststore.callbacks.connect('cell_changed', mycallback)
31+
32+
win = gtk.Window()
33+
win.set_title('click to edit')
34+
win.add(treeview)
35+
win.show_all()
36+
win.connect('delete-event', lambda *args: gtk.main_quit())
37+
gtk.main()

examples/rec_edit_gtk_simple.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
Load a CSV file into a record array and edit it in a gtk treeview
3+
"""
4+
5+
import gtk
6+
import numpy as np
7+
import matplotlib.mlab as mlab
8+
import mpl_toolkits.gtktools as gtktools
9+
10+
r = mlab.csv2rec('data/demodata.csv', converterd={'weekdays':str})
11+
12+
liststore, treeview, win = gtktools.edit_recarray(r)
13+
win.set_title('click to edit')
14+
win.connect('delete-event', lambda *args: gtk.main_quit())
15+
gtk.main()

lib/matplotlib/mlab.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2170,7 +2170,7 @@ def csv2rec(fname, comments='#', skiprows=0, checkrows=0, delimiter=',',
21702170
data type. When set to zero all rows are validated.
21712171
21722172
converterd, if not None, is a dictionary mapping column number or
2173-
munged column name to a converter function
2173+
munged column name to a converter function.
21742174
21752175
names, if not None, is a list of header names. In this case, no
21762176
header will be read from the file
@@ -2256,11 +2256,18 @@ def newfunc(name, val):
22562256
return func(val)
22572257
return newfunc
22582258

2259+
2260+
def mybool(x):
2261+
if x=='True': return True
2262+
elif x=='False': return False
2263+
else: raise ValueError('invalid bool')
2264+
22592265
dateparser = dateutil.parser.parse
22602266
mydateparser = with_default_value(dateparser, datetime.date(1,1,1))
22612267
myfloat = with_default_value(float, np.nan)
22622268
myint = with_default_value(int, -1)
22632269
mystr = with_default_value(str, '')
2270+
mybool = with_default_value(mybool, None)
22642271

22652272
def mydate(x):
22662273
# try and return a date object
@@ -2273,7 +2280,7 @@ def mydate(x):
22732280

22742281
def get_func(name, item, func):
22752282
# promote functions in this order
2276-
funcmap = {myint:myfloat, myfloat:mydate, mydate:mydateparser, mydateparser:mystr}
2283+
funcmap = {mybool:myint,myint:myfloat, myfloat:mydate, mydate:mydateparser, mydateparser:mystr}
22772284
try: func(name, item)
22782285
except:
22792286
if func==mystr:
@@ -2294,7 +2301,7 @@ def get_converters(reader):
22942301
converters = None
22952302
for i, row in enumerate(reader):
22962303
if i==0:
2297-
converters = [myint]*len(row)
2304+
converters = [mybool]*len(row)
22982305
if checkrows and i>checkrows:
22992306
break
23002307
#print i, len(names), len(row)
@@ -2308,6 +2315,9 @@ def get_converters(reader):
23082315
func = converters[j]
23092316
if len(item.strip()):
23102317
func = get_func(name, item, func)
2318+
else:
2319+
# how should we handle custom converters and defaults?
2320+
func = with_default_value(func, None)
23112321
converters[j] = func
23122322
return converters
23132323

@@ -2427,6 +2437,13 @@ def toval(self, x):
24272437
def fromstr(self, s):
24282438
return int(s)
24292439

2440+
class FormatBool(FormatObj):
2441+
def toval(self, x):
2442+
return x
2443+
2444+
def fromstr(self, s):
2445+
return bool(s)
2446+
24302447
class FormatPercent(FormatFloat):
24312448
def __init__(self, precision=4):
24322449
FormatFloat.__init__(self, precision, scale=100.)
@@ -2465,6 +2482,7 @@ def fromstr(self, x):
24652482

24662483

24672484
defaultformatd = {
2485+
np.bool_ : FormatBool(),
24682486
np.int16 : FormatInt(),
24692487
np.int32 : FormatInt(),
24702488
np.int64 : FormatInt(),

0 commit comments

Comments
 (0)