8000 added xlim kwarg patch · matplotlib/matplotlib@b01f1a0 · GitHub
[go: up one dir, main page]

Skip to content

Commit b01f1a0

Browse files
committed
added xlim kwarg patch
svn path=/trunk/matplotlib/; revision=1161
1 parent 80cd333 commit b01f1a0

File tree

5 files changed

+141
-30
lines changed

5 files changed

+141
-30
lines changed

API_CHANGES

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
API CHANGES in matplotlib-0.75
2+
3+
xlim/ylim/axis always return the new limits regardless of arguments.
4+
They now take kwargs which allow you to selectively change the upper
5+
or lower limits while leaving unnamed limits unchanged. See
6+
help(xlim) for example
7+
18
API CHANGES in matplotlib-0.73
29

310
Removed deprecated ColormapJet and friends

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+
2005-04-11 Applied a variant of rick's xlim/ylim/axis patch. These
4+
functions now take kwargs to let you selectively alter only
5+
the min or max if desired. Eg xlim(xmin=2) or
6+
axis(ymax=3). They always return the new lim
7+
8+
39
2005-04-11 Incorporated Werner's wx patch -- wx backend should be
410
compatible with wxpython2.4 and recent versions of 2.5.
511
Some early versions of wxpython 2.5 will not work because

lib/matplotlib/axes.py

Lines changed: 81 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
from __future__ import division, generators
32

43
import math, sys
@@ -2892,22 +2891,55 @@ def _send_ylim_event(self):
28922891
func(self)
28932892

28942893

2895-
def set_xlim(self, v, emit=True):
2894+
def set_xlim(self, *args, **kwargs):
28962895
"""
2897-
SET_XLIM(v, emit=True)
2896+
set_xlim(self, *args, **kwargs):
28982897
28992898
Set the limits for the xaxis; v = [xmin, xmax]
2899+
2900+
set_xlim((valmin, valmax))
2901+
set_xlim(valmin, valmax)
2902+
set_xlim(xmin=1) # xmax unchanged
2903+
set_xlim(xmax=1) # xmin unchanged
2904+
2905+
Valid kwargs:
29002906
2901-
If emit is false, do not trigger an event
2907+
xmin : the min of the xlim
2908+
xmax : the max of the xlim
2909+
emit : notify observers of lim change
2910+
2911+
2912+
Returns the current xlimits as a length 2 tuple
29022913
29032914
ACCEPTS: len(2) sequence of floats
29042915
"""
2905-
vmin, vmax = v
2916+
2917+
vmin, vmax = self.get_xlim()
2918+
2919+
xmin = popd(kwargs, 'xmin', None)
2920+
xmax = popd(kwargs, 'xmax', None)
2921+
emit = popd(kwargs, 'emit', False)
2922+
if len(args)!=0 and (xmin is not None or xmax is not None):
2923+
raise TypeError('You cannot pass args and xmin/xmax kwargs')
2924+
2925+
if len(args)==0:
2926+
if xmin is not None: vmin = xmin
2927+
if xmax is not None: vmax = xmax
2928+
elif len(args)==1:
2929+
vmin, vmax = args[0]
2930+
elif len(args)==2:
2931+
vmin, vmax = args
2932+
else:
2933+
raise ValueError('args must be length 0, 1 or 2')
2934+
2935+
2936+
29062937
if self.transData.get_funcx().get_type()==LOG10 and min(vmin, vmax)<=0:
29072938
raise ValueError('Cannot set nonpositive limits with log transform')
2908-
self.viewLim.intervalx().set_bounds(*v)
2939+
2940+
self.viewLim.intervalx().set_bounds(vmin, vmax)
29092941
if emit: self._send_xlim_event()
2910-
2942+
return vmin, vmax
29112943

29122944
def set_xscale(self, value, basex = 10, subsx=None):
29132945
"""
@@ -2980,20 +3012,56 @@ def set_ylabel(self, ylabel, fontdict=None, **kwargs):
29803012
label.update(kwargs)
29813013
return label
29823014

2983-
def set_ylim(self, v, emit=True):
3015+
3016+
def set_ylim(self, *args, **kwargs):
29843017
"""
2985-
SET_YLIM(v, emit=True)
3018+
set_ylim(self, *args, **kwargs):
3019+
3020+
Set the limits for the yaxis; v = [ymin, ymax]
3021+
3022+
set_ylim((valmin, valmax))
3023+
set_ylim(valmin, valmax)
3024+
set_ylim(ymin=1) # ymax unchanged
3025+
set_ylim(ymax=1) # ymin unchanged
29863026
2987-
Set the limits for the xaxis; v = [ymin, ymax]. If emit is false, do
2988-
not trigger an event.
3027+
Valid kwargs:
3028+
3029+
ymin : the min of the ylim
3030+
ymax : the max of the ylim
3031+
emit : notify observers of lim change
3032+
3033+
3034+
Returns the current ylimits as a length 2 tuple
29893035
29903036
ACCEPTS: len(2) sequence of floats
29913037
"""
2992-
vmin, vmax = v
3038+
3039+
vmin, vmax = self.get_ylim()
3040+
3041+
ymin = popd(kwargs, 'ymin', None)
3042+
ymax = popd(kwargs, 'ymax', None)
3043+
emit = popd(kwargs, 'emit', False)
3044+
if len(args)!=0 and (ymin is not None or ymax is not None):
3045+
raise TypeError('You cannot pass args and ymin/ymax kwargs')
3046+
3047+
if len(args)==0:
3048+
if ymin is not None: vmin = ymin
3049+
if ymax is not None: vmax = ymax
3050+
elif len(args)==1:
3051+
vmin, vmax = args[0]
3052+
elif len(args)==2:
3053+
vmin, vmax = args
3054+
else:
3055+
raise ValueError('args must be length 0, 1 or 2')
3056+
3057+
3058+
29933059
if self.transData.get_funcy().get_type()==LOG10 and min(vmin, vmax)<=0:
29943060
raise ValueError('Cannot set nonpositive limits with log transform')
2995-
self.viewLim.intervaly().set_bounds(*v)
3061+
3062+
self.viewLim.intervaly().set_bounds(vmin, vmax)
29963063
if emit: self._send_ylim_event()
3064+
return vmin, vmax
29973065

29983066
def set_yscale(self, value, basey=10, subsy=None):
29993067
"""

lib/matplotlib/backends/backend_wx.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ def __init__(self, parent, id, figure):
690690
l,b,w,h = figure.bbox.get_bounds()
691691
w = int(math.ceil(w))
692692
h = int(math.ceil(h))
693-
693+
694694
wx.Panel.__init__(self, parent, id, size=wx.Size(w, h))
695695
# Create the drawing bitmap
696696
self.bitmap =wx.EmptyBitmap(w, h)

lib/matplotlib/pylab.py

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ def raise_msg_to_str(msg):
484484

485485

486486

487-
def axis(*v):
487+
def axis(*v, **kwargs):
488488
"""
489489
Set/Get the axis properties::
490490
@@ -498,6 +498,13 @@ def axis(*v):
498498
axis('equal') sets the xlim width and ylim height to be to be
499499
identical. The longer of the two intervals is chosen
500500
501+
502+
if len(*v)==0, you can pass in xmin, xmax, ymin, ymax as kwargs
503+
selectively to alter just those limits w/o changing the others.
504+
See help(xlim) and help(ylim) for more information
505+
506+
The xmin, xmax, ymin, ymax tuple is returned
507+
501508
"""
502509

503510
if len(v)==1 and is_string_like(v[0]):
@@ -520,21 +527,28 @@ def axis(*v):
520527

521528
else:
522529
raise ValueError('Unrecognized string %s to axis; try on or off' % s)
523-
return
530+
ax = gca()
531+
xmin, xmax = ax.get_xlim()
532+
ymin, ymax = ax.get_ylim()
533+
draw_if_interactive()
534+
return xmin, xmax, ymin, ymax
524535

525536
try: v[0]
526537
except IndexError:
527-
xlim = gca().get_xlim()
528-
ylim = gca().get_ylim()
529-
return [xlim[0], xlim[1], ylim[0], ylim[1]]
538+
xmin, xmax = gca().set_xlim(**kwargs)
539+
ymin, ymax = gca().set_ylim(**kwargs)
540+
draw_if_interactive()
541+
return [xmin, xmax, ymin, ymax]
530542

531543
v = v[0]
532544
if len(v) != 4:
533545
raise ValueError('v must contain [xmin xmax ymin ymax]')
534-
546+
547+
535548
gca().set_xlim([v[0], v[1]])
536549
gca().set_ylim([v[2], v[3]])
537550
draw_if_interactive()
551+
return v
538552

539553
def axes(*args, **kwargs):
540554
"""
@@ -1099,14 +1113,21 @@ def xlim(*args, **kwargs):
10991113
xmin, xmax = xlim() : return the current xlim
11001114
xlim( (xmin, xmax) ) : set the xlim to xmin, xmax
11011115
xlim( xmin, xmax ) : set the xlim to xmin, xmax
1116+
1117+
If you do not specify args, you can pass the xmin and xmax as
1118+
kwargs, eg
1119+
1120+
xlim(xmax=3) # adjust the max leaving min unchanged
1121+
xlim(xmin=1) # adjust the min leaving max unchanged
1122+
1123+
The new axis limits are returned as a length 2 tuple
1124+
11021125
"""
11031126
ax = gca()
1104-
if len(args)==0: return ax.get_xlim()
1105-
elif len(args)==1: lim = ax.set_xlim(args)
1106-
elif len(args)==2: lim = ax.set_xlim((args[0], args[1]))
1107-
else: raise TypeError('Illegal number of arguments to xlim')
1127+
ret = ax.set_xlim(*args, **kwargs)
11081128
draw_if_interactive()
1109-
return lim
1129+
return ret
1130+
11101131

11111132
def ylim(*args, **kwargs):
11121133
"""
@@ -1115,14 +1136,23 @@ def ylim(*args, **kwargs):
11151136
ymin, ymax = ylim() : return the current ylim
11161137
ylim( (ymin, ymax) ) : set the ylim to ymin, ymax
11171138
ylim( ymin, ymax ) : set the ylim to ymin, ymax
1139+
1140+
If you do not specify args, you can pass the ymin and ymax as
1141+
kwargs, eg
1142+
1143+
ylim(ymax=3) # adjust the max leaving min unchanged
1144+
ylim(ymin=1) # adjust the min leaving max unchanged
1145+
1146+
The new axis limits are returned as a length 2 tuple
1147+
11181148
"""
11191149
ax = gca()
1120-
if len(args)==0: return ax.get_ylim()
1121-
elif len(args)==1: lim = ax.set_ylim(args)
1122-
elif len(args)==2: lim = ax.set_ylim((args[0], args[1]))
1123-
else: raise TypeError('Illegal number of arguments to ylim')
1150+
ret = ax.set_ylim(*args, **kwargs)
11241151
draw_if_interactive()
1125-
return lim
1152+
return ret
1153+
1154+
1155+
11261156

11271157
def xticks(*args, **kwargs):
11281158
"""

0 commit comments

Comments
 (0)
0