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

Skip to content

Commit 39a01ad

Browse files
committed
fixed some examples bugs
svn path=/trunk/matplotlib/; revision=4061
1 parent e7f1e51 commit 39a01ad

5 files changed

Lines changed: 169 additions & 56 deletions

File tree

examples/keypress_demo.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
#!/usr/bin/env python
22
"""
33
Show how to connect to keypress events
4-
5-
Note, on the wx backend on some platforms (eg linux), you have to
6-
first click on the figure before the keypress events are activated.
7-
If you know how to fix this, please email us!
84
"""
9-
from pylab import *
5+
import numpy as n
6+
from pylab import figure, show
107

118
def press(event):
129
print 'press', event.key
13-
if event.key=='g':
14-
grid()
15-
draw()
10+
if event.key=='x':
11+
visible = xl.get_visible()
12+
xl.set_visible(not visible)
13+
fig.canvas.draw()
14+
15+
fig = figure()
16+
ax = fig.add_subplot(111)
17+
18+
fig.canvas.mpl_connect('key_press_event', press)
1619

17-
connect('key_press_event', press)
20+
ax.plot(n.random.rand(12), n.random.rand(12), 'go')
21+
xl = ax.set_xlabel('easy come, easy go')
1822

19-
title('press g to toggle grid')
20-
plot(rand(12), rand(12), 'go')
2123
show()

examples/units/bar_unit_demo.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44

55
N = 5
66
menMeans = (150*cm, 160*cm, 146*cm, 172*cm, 155*cm)
7-
menStd = (20*cm, 30*cm, 32*cm, 10*cm, 20*cm)
7+
menStd = ( 20*cm, 30*cm, 32*cm, 10*cm, 20*cm)
88

99
fig = figure()
1010
ax = fig.add_subplot(111)
1111

1212
ind = nx.arange(N) # the x locations for the groups
13-
width = 0.35 # the width of the bars
13+
width = 0.35 # the width of the bars
1414
p1 = ax.bar(ind, menMeans, width, color='r', bottom=0*cm, yerr=menStd)
1515

16+
1617
womenMeans = (145*cm, 149*cm, 172*cm, 165*cm, 200*cm)
1718
womenStd = (30*cm, 25*cm, 20*cm, 31*cm, 22*cm)
1819
p2 = ax.bar(ind+width, womenMeans, width, color='y', bottom=0*cm, yerr=womenStd)

lib/matplotlib/axes.py

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,6 +1173,8 @@ def update_datalim(self, xys):
11731173
# Otherwise, it will compute the bounds of it's current data
11741174
# and the data in xydata
11751175
xys = npy.asarray(xys)
1176+
1177+
11761178
self.dataLim.update_numerix_xy(xys, -1)
11771179

11781180

@@ -3242,21 +3244,6 @@ def make_iterable(x):
32423244

32433245
patches = []
32443246

3245-
# lets do some conversions now
3246-
if self.xaxis is not None:
3247-
xconv = self.xaxis.converter
3248-
if xconv is not None:
3249-
units = self.xaxis.get_units()
3250-
left = xconv.convert( left, units )
3251-
width = xconv.convert( width, units )
3252-
3253-
if self.yaxis is not None:
3254-
yconv = self.yaxis.converter
3255-
if yconv is not None :
3256-
units = self.yaxis.get_units()
3257-
bottom = yconv.convert( bottom, units )
3258-
height = yconv.convert( height, units )
3259-
32603247

32613248
if align == 'edge':
32623249
pass
@@ -3645,23 +3632,24 @@ def errorbar(self, x, y, yerr=None, xerr=None,
36453632
a list of error bar cap lines, the third element is a list of
36463633
line collections for the horizontal and vertical error ranges
36473634
"""
3635+
36483636
self._process_unit_info(xdata=x, ydata=y, kwargs=kwargs)
36493637
if not self._hold: self.cla()
36503638

3651-
# make sure all the args are iterable arrays
3652-
if not iterable(x): x = npy.array([x])
3653-
else: x = npy.asarray(x)
3639+
# make sure all the args are iterable; use lists not arrays to preserve units
3640+
if not iterable(x):
3641+
x = [x]
36543642

3655-
if not iterable(y): y = npy.array([y])
3656-
else: y = npy.asarray(y)
3643+
if not iterable(y):
3644+
y = [y]
36573645

36583646
if xerr is not None:
3659-
if not iterable(xerr): xerr = npy.array([xerr])
3660-
else: xerr = npy.asarray(xerr)
3647+
if not iterable(xerr):
3648+
xerr = [xerr]
36613649

36623650
if yerr is not None:
3663-
if not iterable(yerr): yerr = npy.array([yerr])
3664-
else: yerr = npy.asarray(yerr)
3651+
if not iterable(yerr):
3652+
yerr = [yerr]
36653653

36663654
l0 = None
36673655

@@ -3677,7 +3665,9 @@ def errorbar(self, x, y, yerr=None, xerr=None,
36773665
if 'lw' in kwargs:
36783666
lines_kw['lw']=kwargs['lw']
36793667

3680-
if not iterable(lolims): lolims = npy.array([lolims]*len(x), bool)
3668+
# arrays fine here, they are booleans and hence not units
3669+
if not iterable(lolims):
3670+
lolims = npy.asarray([lolims]*len(x), bool)
36813671
else: lolims = npy.asarray(lolims, bool)
36823672

36833673
if not iterable(uplims): uplims = npy.array([uplims]*len(x), bool)
@@ -3699,12 +3689,14 @@ def errorbar(self, x, y, yerr=None, xerr=None,
36993689
plot_kw['mew']=kwargs['mew']
37003690

37013691
if xerr is not None:
3702-
if len(xerr.shape) == 1:
3703-
left = x-xerr
3704-
right = x+xerr
3692+
if iterable(xerr) and len(xerr)==2:
3693+
# using list comps rather than arrays to preserve units
3694+
left = [thisx-thiserr for (thisx, thiserr) in zip(x,xerr[0])]
3695+
right = [thisx+thiserr for (thisx, thiserr) in zip(x,xerr[1])]
37053696
else:
3706-
left = x-xerr[0]
3707-
right = x+xerr[1]
3697+
# using list comps rather than arrays to preserve units
3698+
left = [thisx-thiserr for (thisx, thiserr) in zip(x,xerr)]
3699+
right = [thisx+thiserr for (thisx, thiserr) in zip(x,xerr)]
37083700

37093701
barcols.append( self.hlines(y, left, right, **lines_kw ) )
37103702
if capsize > 0:
@@ -3723,12 +3715,14 @@ def errorbar(self, x, y, yerr=None, xerr=None,
37233715
caplines.extend( self.plot(right, y, 'k|', **plot_kw) )
37243716

37253717
if yerr is not None:
3726-
if len(yerr.shape) == 1:
3727-
lower = y-yerr
3728-
upper = y+yerr
3718+
if iterable(yerr) and len(yerr)==2:
3719+
# using list comps rather than arrays to preserve units
3720+
lower = [thisy-thiserr for (thisy, thiserr) in zip(y,yerr[0])]
3721+
upper = [thisy+thiserr for (thisy, thiserr) in zip(y,yerr[1])]
37293722
else:
3730-
lower = y-yerr[0]
3731-
upper = y+yerr[1]
3723+
# using list comps rather than arrays to preserve units
3724+
lower = [thisy-thiserr for (thisy, thiserr) in zip(y,yerr)]
3725+
upper = [thisy+thiserr for (thisy, thiserr) in zip(y,yerr)]
37323726

37333727
barcols.append( self.vlines(x, lower, upper, **lines_kw) )
37343728
if capsize > 0:

lib/matplotlib/mlab.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,6 +1455,115 @@ def rec2csv(r, fname, delimiter=','):
14551455
for row in r:
14561456
writer.writerow(map(str, row))
14571457
fh.close()
1458+
1459+
try:
1460+
import pyExcelerator as excel
1461+
except ImportError:
1462+
pass
1463+
else:
1464+
1465+
class Format:
1466+
xlstyle = None
1467+
def convert(self, x):
1468+
return x
1469+
1470+
class FormatFloat(Format):
1471+
def __init__(self, precision=4):
1472+
self.xlstyle = excel.XFStyle()
1473+
zeros = ''.join(['0']*precision)
1474+
self.xlstyle.num_format_str = '#,##0.%s;[RED]-#,##0.%s'%(zeros, zeros)
1475+
1476+
class FormatInt(Format):
1477+
convert = int
1478+
def __init__(self):
1479+
1480+
self.xlstyle = excel.XFStyle()
1481+
self.xlstyle.num_format_str = '#,##;[RED]-#,##'
1482+
1483+
class FormatPercent(Format):
1484+
def __init__(self, precision=4):
1485+
self.xlstyle = excel.XFStyle()
1486+
zeros = ''.join(['0']*precision)
1487+
self.xlstyle.num_format_str = '0.%s%;[RED]-0.%s%'%(zeros, zeros)
1488+
1489+
class FormatThousands(FormatFloat):
1490+
def __init__(self, precision=1):
1491+
FormatFloat.__init__(self, precision)
1492+
1493+
def convert(self, x):
1494+
return x/1e3
1495+
1496+
class FormatMillions(FormatFloat):
1497+
def __init__(self, precision=1):
1498+
FormatFloat.__init__(self, precision)
1499+
1500+
def convert(self, x):
1501+
return x/1e6
1502+
1503+
class FormatDate(Format):
1504+
def __init__(self, fmt='%Y-%m-%d'):
1505+
self.fmt = fmt
1506+
1507+
def convert(self, val):
1508+
return val.strftime(self.fmt)
1509+
1510+
class FormatDatetime(Format):
1511+
def __init__(self, fmt='%Y-%m-%d %H:%M:%S'):
1512+
self.fmt = fmt
1513+
1514+
def convert(self, val):
1515+
return val.strftime(self.fmt)
1516+
1517+
class FormatObject(Format):
1518+
1519+
def convert(self, x):
1520+
return str(x)
1521+
1522+
def rec2excel(ws, r, formatd=None, rownum=0):
1523+
"""
1524+
save record array r to excel pyExcelerator worksheet ws
1525+
starting at rownum
1526+
1527+
formatd is a dictionary mapping dtype name -> Format instances
1528+
"""
1529+
1530+
if formatd is None:
1531+
formatd = dict()
1532+
1533+
formats = []
1534+
for i, name in enumerate(r.dtype.names):
1535+
dt = r.dtype[name]
1536+
format = formatd.get(name)
1537+
if format is None:
1538+
format = rec2excel.formatd.get(dt.type, FormatObject())
1539+
1540+
ws.write(rownum, i, name)
1541+
formats.append(format)
1542+
1543+
rownum+=1
1544+
1545+
ind = npy.arange(len(r.dtype.names))
1546+
for row in r:
1547+
for i in ind:
1548+
val = row[i]
1549+
format = formats[i]
1550+
val = format.convert(val)
1551+
if format.xlstyle is None:
1552+
ws.write(rownum, i, val)
1553+
else:
1554+
ws.write(rownum, i, val, format.xlstyle)
1555+
rownum += 1
1556+
rec2excel.formatd = {
1557+
npy.int16 : FormatInt(),
1558+
npy.int32 : FormatInt(),
1559+
npy.int64 : FormatInt(),
1560+
npy.float32 : FormatFloat(),
1561+
npy.float64 : FormatFloat(),
1562+
npy.object_ : FormatObject(),
1563+
npy.string_ : Format(),
1564+
}
1565+
1566+
14581567

14591568
# some record array helpers
14601569
def rec_append_field(rec, name, arr, dtype=None):

lib/matplotlib/patches.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,10 @@ def get_verts(self):
350350
Return the vertices of the rectangle
351351
"""
352352
x, y = self.xy
353-
left, right = self.convert_xunits((x, x + self.width))
354-
bottom, top = self.convert_yunits((y, y + self.height))
353+
left = self.convert_xunits(x)
354+
right = self.convert_xunits(x + self.width)
355+
bottom = self.convert_yunits(y)
356+
top = self.convert_yunits(y+self.height)
355357

356358
return ( (left, bottom), (left, top),
357359
(right, top), (right, bottom),
@@ -806,8 +808,15 @@ def contains(self,ev):
806808
def get_verts(self):
807809

808810
xcenter, ycenter = self.center
809-
810811
width, height = self.width, self.height
812+
813+
xcenter = self.convert_xunits(xcenter)
814+
width = self.convert_xunits(width)
815+
ycenter = self.convert_yunits(ycenter)
816+
height = self.convert_xunits(height)
817+
818+
819+
811820
angle = self.angle
812821

813822
theta = npy.arange(0.0, 360.0, 1.0)*npy.pi/180.0
@@ -820,8 +829,6 @@ def get_verts(self):
820829
[npy.sin(rtheta), npy.cos(rtheta)],
821830
])
822831

823-
x = self.convert_xunits(x)
824-
y = self.convert_yunits(y)
825832

826833
x, y = npy.dot(R, npy.array([x, y]))
827834
x += xcenter
@@ -857,16 +864,16 @@ def draw(self, renderer):
857864
x, y = self.center
858865
x = self.convert_xunits(x)
859866
y = self.convert_yunits(y)
867+
w = self.convert_xunits(self.width)/2.
868+
h = self.convert_yunits(self.height)/2.
860869

861870
theta = self.angle * npy.pi/180.
862871
T = npy.array([
863872
[1, 0, x],
864873
[0, 1, y],
865874
[0, 0, 1]])
866875

867-
w, h = self.width/2, self.height/2.
868-
w = self.convert_xunits(w)
869-
h = self.convert_yunits(h)
876+
870877

871878

872879
S = npy.array([

0 commit comments

Comments
 (0)