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

Skip to content

Commit 2dea50f

Browse files
committed
some more units refactoring
svn path=/trunk/matplotlib/; revision=3125
1 parent a6a812c commit 2dea50f

25 files changed

Lines changed: 545 additions & 393 deletions

API_CHANGES

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
Line2D.get_xdata and get_ydata valid_only=False kwarg is replaced
2+
by orig=True. When True, it returns the original data, otherwise
3+
the processed data (masked, converted)
4+
5+
Some modifications to the units interface.
6+
units.ConversionInterface.tickers renamed to
7+
units.ConversionInterface.axisinfo and it now returns a
8+
units.AxisInfo object rather than a tuple. This will make it
9+
easier to add axis info functionality (eg I added a default label
10+
on this iteration) w/o having to change the tuple length and hence
11+
the API of the client code everytime new functionality is added.
12+
Also, units.ConversionInterface.convert_to_value is now simply
13+
named units.ConversionInterface.convert.
14+
115
Axes.errorbar uses Axes.vlines and Axes.hlines to draw its error
216
limits int he vertical and horizontal direction. As you'll see
317
in the changes below, these funcs now return a LineCollection

examples/backend_driver.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ def drive(backend, python='python'):
134134
#backends = ['Agg', 'PS', 'SVG', 'Template']
135135
# backends = [ 'GTK', 'WX', 'TkAgg']
136136
default_backends = ['Agg', 'PS', 'SVG', 'Template']
137-
#default_backends = ['Agg']
138-
#backends = ['Agg']
137+
default_backends = ['Agg']
138+
backends = ['Agg']
139139
if sys.platform == 'win32':
140140
python = r'c:\Python24\python.exe'
141141
else:

examples/simple_plot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
grid(True)
1616

1717
#savefig('simple_plot.png')
18-
#savefig('simple_plot')
18+
savefig('simple_plot')
1919

2020
show()

examples/units/basic_units.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -164,26 +164,23 @@ def convert_to(self, unit):
164164
def get_value(self):
165165
return self.value
166166

167-
168-
169167
def get_unit(self):
170168
return self.unit
171169

172170

173171
class BasicUnit(object):
174-
def __init__(self, name, full_name=None):
172+
def __init__(self, name, fullname=None):
175173
self.name = name
176-
if (not full_name):
177-
full_name = name
178-
self.full_name = full_name
174+
if fullname is None: fullname = name
175+
self.fullname = fullname
179176
self.conversions = dict()
180177

181178

182179
def __repr__(self):
183180
return 'BasicUnit(' + `self.name` + ')'
184181

185182
def __str__(self):
186-
return self.full_name
183+
return self.fullname
187184

188185
def __call__(self, value):
189186
return TaggedValue(value, self)
@@ -296,33 +293,36 @@ def rad_fn(x,pos=None):
296293

297294
class BasicUnitConverter(units.ConversionInterface):
298295

299-
def tickers(x, unit=None):
300-
'return major and minor tick locators and formatters'
296+
def axisinfo(unit):
297+
'return AxisInfo instance for x and unit'
301298

302299
if unit==radians:
303-
return (
304-
ticker.MultipleLocator(base=nx.pi/2),
305-
ticker.NullLocator(),
306-
ticker.FuncFormatter(rad_fn),
307-
ticker.NullFormatter(),
300+
return units.AxisInfo(
301+
majloc=ticker.MultipleLocator(base=nx.pi/2),
302+
majfmt=ticker.FuncFormatter(rad_fn),
303+
label=unit.fullname,
308304
)
309305
elif unit==degrees:
310-
return (
311-
ticker.AutoLocator(),
312-
ticker.NullLocator(),
313-
ticker.FormatStrFormatter(r'$%i^\circ$'),
314-
ticker.NullFormatter(),
306+
return units.AxisInfo(
307+
majloc=ticker.AutoLocator(),
308+
majfmt=ticker.FormatStrFormatter(r'$%i^\circ$'),
309+
label=unit.fullname,
315310
)
316-
else:
317-
return None
318-
tickers = staticmethod(tickers)
311+
elif unit is not None:
312+
return units.AxisInfo(label=unit.fullname)
313+
return None
314+
axisinfo = staticmethod(axisinfo)
319315

320-
def convert_to_value(val, unit):
316+
def convert(val, unit):
321317
if iterable(val):
322318
return [thisval.convert_to(unit).get_value() for thisval in val]
323319
else:
324320
return val.convert_to(unit).get_value()
325-
convert_to_value = staticmethod(convert_to_value)
321+
convert = staticmethod(convert)
326322

323+
def default_units(x):
324+
'return the default unit for x or None'
325+
return x.unit
326+
default_units = staticmethod(default_units)
327327

328-
units.manager.converters[TaggedValue] = BasicUnitConverter()
328+
units.registry[TaggedValue] = BasicUnitConverter()

examples/units/date_support.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,31 @@
44
import matplotlib.units as units
55
import matplotlib.dates as dates
66
import matplotlib.ticker as ticker
7-
import basic_units
87
import datetime
98

109
class DateConverter(units.ConversionInterface):
1110

12-
def tickers(x, unit=None):
13-
'return major and minor tick locators and formatters'
14-
majloc = dates.AutoDateLocator()
15-
minloc = ticker.NullLocator()
16-
majfmt = dates.AutoDateFormatter(majloc)
17-
minfmt = ticker.NullFormatter()
18-
return majloc, minloc, majfmt, minfmt
19-
tickers = staticmethod(tickers)
11+
def axisinfo(unit):
12+
'return the unit AxisInfo'
13+
if unit=='date':
14+
majloc = dates.AutoDateLocator()
15+
majfmt = dates.AutoDateFormatter(majloc)
16+
return units.AxisInfo(
17+
majloc = majloc,
18+
majfmt = majfmt,
19+
label='date',
20+
)
21+
else: return None
22+
axisinfo = staticmethod(axisinfo)
2023

21-
def convert_to_value(value, unit):
24+
def convert(value, unit):
2225
return dates.date2num(value)
23-
convert_to_value = staticmethod(convert_to_value)
26+
convert = staticmethod(convert)
2427

28+
def default_units(x):
29+
'return the default unit for x or None'
30+
return 'date'
31+
default_units = staticmethod(default_units)
2532

26-
units.manager.converters[datetime.date] = DateConverter()
33+
units.registry[datetime.date] = DateConverter()
2734

examples/units/evans_test.py

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,20 @@ def value( self, unit ):
1717

1818
class FooConverter:
1919

20-
def tickers(x, unit):
21-
'return (majorloc, minorloc, majorfmt, minorfmt) or None to accept defaults'
20+
def axisinfo(unit):
21+
'return the Foo AxisInfo'
2222
if unit==1.0 or unit==2.0:
23-
majloc = ticker.IndexLocator( 4, 0 )
24-
majfmt = ticker.FormatStrFormatter("VAL: %s")
25-
minloc = ticker.NullLocator()
26-
minfmt = ticker.NullFormatter()
27-
return majloc, minloc, majfmt, minfmt
28-
else: return None
29-
tickers = staticmethod(tickers)
30-
31-
def convert_to_value(obj, unit):
23+
return units.AxisInfo(
24+
majloc = ticker.IndexLocator( 4, 0 ),
25+
majfmt = ticker.FormatStrFormatter("VAL: %s"),
26+
label='foo',
27+
)
28+
29+
else:
30+
return None
31+
axisinfo = staticmethod(axisinfo)
32+
33+
def convert(obj, unit):
3234
"""
3335
convert obj using unit. If obj is a sequence, return the
3436
converted sequence
@@ -38,9 +40,18 @@ def convert_to_value(obj, unit):
3840
return [o.value(unit) for o in obj]
3941
else:
4042
return obj.value(unit)
41-
convert_to_value = staticmethod(convert_to_value)
43+
convert = staticmethod(convert)
4244

43-
units.manager.converters[Foo] = FooConverter()
45+
def default_units(x):
46+
'return the default unit for x or None'
47+
if iterable(x):
48+
for thisx in x:
49+
return thisx.unit
50+
else:
51+
return x.unit
52+
default_units = staticmethod(default_units)
53+
54+
units.registry[Foo] = FooConverter()
4455

4556
# create some Foos
4657
x = []
@@ -62,9 +73,14 @@ def convert_to_value(obj, unit):
6273

6374
#fig.savefig('plot1.png')
6475

65-
# plot without specifying units; will use the None branch for tickers
76+
# plot without specifying units; will use the None branch for axisinfo
6677
fig2 = figure()
6778
ax = fig2.add_subplot(111)
68-
ax.plot( x, y )
79+
ax.plot( x, y ) # uses default units
6980
#p.savefig('plot2.png')
81+
82+
fig3 = figure()
83+
ax = fig3.add_subplot(111)
84+
ax.plot( x, y, xunits=0.5)
85+
7086
show()

examples/units/evans_test2.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,9 @@ def cos( x ):
2828

2929
ax = fig.add_subplot(211)
3030
ax.plot(x, cos(x), xunits=radians)
31-
ax.set_xlabel('radians')
3231

3332
ax = fig.add_subplot(212)
3433
ax.plot(x, cos(x), xunits=degrees)
35-
ax.set_xlabel('degrees')
3634

3735
show()
3836

examples/units/radian_demo.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77

88
ax = fig.add_subplot(211)
99
ax.plot(x, nx.cos(x), xunits=radians)
10-
ax.set_xlabel('radians')
1110

1211
ax = fig.add_subplot(212)
1312
ax.plot(x, nx.cos(x), xunits=degrees)
14-
ax.set_xlabel('degrees')
1513

1614
show()
1715

examples/units/units_sample.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,19 @@
33

44
cms = cm *nx.arange(0, 10, 2)
55

6-
76
fig = figure()
87

98
ax1 = fig.add_subplot(2,2,1)
109
ax1.plot(cms, cms, xunits=cm, yunits=cm)
11-
ax1.set_xlabel('in centimeters')
12-
ax1.set_ylabel('in centimeters')
13-
14-
10+
1511
ax2 = fig.add_subplot(2,2,2)
1612
ax2.plot(cms, cms, xunits=cm, yunits=inch)
17-
ax2.set_xlabel('in centimeters')
18-
ax2.set_ylabel('in inches')
1913

2014
ax3 = fig.add_subplot(2,2,3)
2115
ax3.plot(cms, cms, xunits=inch, yunits=cm)
22-
ax3.set_xlabel('in inches')
23-
ax3.set_ylabel('in centimeters')
2416

2517
ax4 = fig.add_subplot(2,2,4)
2618
ax4.plot(cms, cms, xunits=inch, yunits=inch)
27-
ax4.set_xlabel('in inches')
28-
ax4.set_ylabel('in inches')
2919
#fig.savefig('simple_conversion_plot.png')
3020

3121
show()

examples/units/units_scatter.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,28 @@
88
The example below shows support for unit conversions over masked
99
arrays.
1010
"""
11-
import matplotlib
12-
matplotlib.rcParams['numerix'] = 'numpy'
13-
import numpy as N
1411
from basic_units import secs, hertz, minutes
15-
from matplotlib.pylab import figure, show
12+
from matplotlib.pylab import figure, show, nx
1613

1714
# create masked array
18-
x = N.ma.MaskedArray((1,2,3,4,5,6,7,8), N.float64, mask=(1,0,1,0,0,0,1,0))
1915

16+
#xsecs = secs*nx.ma.MaskedArray((1,2,3,4,5,6,7,8), nx.Float, mask=(1,0,1,0,0,0,1,0))
17+
xsecs = secs*nx.arange(1,10.)
2018

2119
fig = figure()
2220
ax1 = fig.add_subplot(3,1,1)
23-
ax1.scatter(x, x)
24-
ax1.set_ylabel('seconds')
21+
ax1.scatter(xsecs, xsecs)
22+
#ax1.set_ylabel('seconds')
2523
ax1.axis([0,10,0,10])
2624

2725
ax2 = fig.add_subplot(3,1,2, sharex=ax1)
28-
xsecs = secs*x
29-
3026
ax2.scatter(xsecs, xsecs, yunits=hertz)
31-
ax2.set_ylabel('Hertz')
3227
ax2.axis([0,10,0,1])
3328

3429
ax3 = fig.add_subplot(3,1,3, sharex=ax1)
35-
xsecs = secs*x
3630
ax3.scatter(xsecs, xsecs, yunits=hertz)
37-
ax3.set_yunits(minutes)
31+
ax3.yaxis.set_units(minutes)
3832
ax3.axis([0,10,0,1])
39-
ax3.set_xlabel('seconds')
40-
ax3.set_ylabel('minutes')
41-
42-
#fig.savefig('units-test-scatter.png')
4333

4434
show()
4535

0 commit comments

Comments
 (0)