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

Skip to content

Commit e8f143c

Browse files
committed
alpha is left as None until you really want to specify it; closes 2957321.
I think this, together with some other recent commits, fixes various longstanding problems in the way alpha has been handled. The default value is left as None, and with this value, existing alpha specified in RGBA sequences will not be overridden. Substitution of the default value of 1 for a value of None is delayed until the GraphicsContextBase set_alpha method is executed. svn path=/trunk/matplotlib/; revision=8372
1 parent e103fc9 commit e8f143c

10 files changed

Lines changed: 51 additions & 51 deletions

File tree

examples/pylab_examples/figimage_demo.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""
2-
See pcolor_demo2 for a much faster way of generating pcolor plots
2+
This illustrates placing images directly in the figure, with no axes.
3+
34
"""
45
import numpy as np
56
import matplotlib
@@ -12,19 +13,9 @@
1213
Z.shape = 100,100
1314
Z[:,50:] = 1.
1415

15-
ax = fig.add_subplot(111)
16-
ax.grid(True)
17-
1816
im1 = plt.figimage(Z, xo=50, yo=0, cmap=cm.jet, origin='lower')
1917
im2 = plt.figimage(Z, xo=100, yo=100, alpha=.8, cmap=cm.jet, origin='lower')
2018

21-
22-
if 0:
23-
dpi = 72
24-
plt.savefig('figimage_%d.png'%dpi, dpi=dpi, facecolor='gray')
25-
plt.savefig('figimage_%d.pdf'%dpi, dpi=dpi, facecolor='gray')
26-
plt.savefig('figimage_%d.svg'%dpi, dpi=dpi, facecolor='gray')
27-
plt.savefig('figimage_%d.eps'%dpi, dpi=dpi, facecolor='gray')
2819
plt.show()
2920

3021

lib/matplotlib/artist.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def __init__(self):
7878
self._transformSet = False
7979
self._visible = True
8080
self._animated = False
81-
self._alpha = 1.0
81+
self._alpha = None
8282
self.clipbox = None
8383
self._clippath = None
8484
self._clipon = True

lib/matplotlib/axes.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ def __init__(self, fig, rect,
372372
Keyword Description
373373
================ =========================================
374374
*adjustable* [ 'box' | 'datalim' | 'box-forced']
375-
*alpha* float: the alpha transparency
375+
*alpha* float: the alpha transparency (can be None)
376376
*anchor* [ 'C', 'SW', 'S', 'SE', 'E', 'NE', 'N',
377377
'NW', 'W' ]
378378
*aspect* [ 'auto' | 'equal' | aspect_ratio ]
@@ -5338,14 +5338,14 @@ def dopatch(xs,ys):
53385338

53395339
@docstring.dedent_interpd
53405340
def scatter(self, x, y, s=20, c='b', marker='o', cmap=None, norm=None,
5341-
vmin=None, vmax=None, alpha=1.0, linewidths=None,
5341+
vmin=None, vmax=None, alpha=None, linewidths=None,
53425342
faceted=True, verts=None,
53435343
**kwargs):
53445344
"""
53455345
call signatures::
53465346
53475347
scatter(x, y, s=20, c='b', marker='o', cmap=None, norm=None,
5348-
vmin=None, vmax=None, alpha=1.0, linewidths=None,
5348+
vmin=None, vmax=None, alpha=None, linewidths=None,
53495349
verts=None, **kwargs)
53505350
53515351
Make a scatter plot of *x* versus *y*, where *x*, *y* are
@@ -5438,7 +5438,7 @@ def scatter(self, x, y, s=20, c='b', marker='o', cmap=None, norm=None,
54385438
*norm* instance, your settings for *vmin* and *vmax* will
54395439
be ignored.
54405440
5441-
*alpha*: 0 <= scalar <= 1
5441+
*alpha*: 0 <= scalar <= 1 or None
54425442
The alpha value for the patches
54435443
54445444
*linewidths*: [ None | scalar | sequence ]
@@ -5656,7 +5656,7 @@ def scatter(self, x, y, s=20, c='b', marker='o', cmap=None, norm=None,
56565656
def hexbin(self, x, y, C = None, gridsize = 100, bins = None,
56575657
xscale = 'linear', yscale = 'linear', extent = None,
56585658
cmap=None, norm=None, vmin=None, vmax=None,
5659-
alpha=1.0, linewidths=None, edgecolors='none',
5659+
alpha=None, linewidths=None, edgecolors='none',
56605660
reduce_C_function = np.mean, mincnt=None, marginals=False,
56615661
**kwargs):
56625662
"""
@@ -5665,7 +5665,7 @@ def hexbin(self, x, y, C = None, gridsize = 100, bins = None,
56655665
hexbin(x, y, C = None, gridsize = 100, bins = None,
56665666
xscale = 'linear', yscale = 'linear',
56675667
cmap=None, norm=None, vmin=None, vmax=None,
5668-
alpha=1.0, linewidths=None, edgecolors='none'
5668+
alpha=None, linewidths=None, edgecolors='none'
56695669
reduce_C_function = np.mean, mincnt=None, marginals=True
56705670
**kwargs)
56715671
@@ -5744,7 +5744,7 @@ def hexbin(self, x, y, C = None, gridsize = 100, bins = None,
57445744
array *C* is used. Note if you pass a norm instance, your settings
57455745
for *vmin* and *vmax* will be ignored.
57465746
5747-
*alpha*: scalar
5747+
*alpha*: scalar between 0 and 1, or None
57485748
the alpha value for the patches
57495749
57505750
*linewidths*: [ None | scalar ]
@@ -6438,14 +6438,14 @@ def fill_betweenx(self, y, x1, x2=0, where=None, **kwargs):
64386438

64396439
@docstring.dedent_interpd
64406440
def imshow(self, X, cmap=None, norm=None, aspect=None,
6441-
interpolation=None, alpha=1.0, vmin=None, vmax=None,
6441+
interpolation=None, alpha=None, vmin=None, vmax=None,
64426442
origin=None, extent=None, shape=None, filternorm=1,
64436443
filterrad=4.0, imlim=None, resample=None, url=None, **kwargs):
64446444
"""
64456445
call signature::
64466446
64476447
imshow(X, cmap=None, norm=None, aspect=None, interpolation=None,
6448-
alpha=1.0, vmin=None, vmax=None, origin=None, extent=None,
6448+
alpha=None, vmin=None, vmax=None, origin=None, extent=None,
64496449
**kwargs)
64506450
64516451
Display the image in *X* to current axes. *X* may be a float
@@ -6505,6 +6505,7 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
65056505
65066506
*alpha*: scalar
65076507
The alpha blending value, between 0 (transparent) and 1 (opaque)
6508+
or *None*
65086509
65096510
*origin*: [ None | 'upper' | 'lower' ]
65106511
Place the [0,0] index of the array in the upper left or lower left
@@ -6673,7 +6674,7 @@ def pcolor(self, *args, **kwargs):
66736674
66746675
An mpl color or sequence of colors will set the edge color
66756676
6676-
*alpha*: 0 <= scalar <= 1
6677+
*alpha*: 0 <= scalar <= 1 or *None*
66776678
the alpha blending value
66786679
66796680
Return value is a :class:`matplotlib.collection.Collection`
@@ -6729,7 +6730,7 @@ def pcolor(self, *args, **kwargs):
67296730

67306731
if not self._hold: self.cla()
67316732

6732-
alpha = kwargs.pop('alpha', 1.0)
6733+
alpha = kwargs.pop('alpha', None)
67336734
norm = kwargs.pop('norm', None)
67346735
cmap = kwargs.pop('cmap', None)
67356736
vmin = kwargs.pop('vmin', None)
@@ -6858,7 +6859,7 @@ def pcolormesh(self, *args, **kwargs):
68586859
68596860
An mpl color or sequence of colors will set the edge color
68606861
6861-
*alpha*: 0 <= scalar <= 1
6862+
*alpha*: 0 <= scalar <= 1 or *None*
68626863
the alpha blending value
68636864
68646865
Return value is a :class:`matplotlib.collection.QuadMesh`
@@ -6878,7 +6879,7 @@ def pcolormesh(self, *args, **kwargs):
68786879
"""
68796880
if not self._hold: self.cla()
68806881

6881-
alpha = kwargs.pop('alpha', 1.0)
6882+
alpha = kwargs.pop('alpha', None)
68826883
norm = kwargs.pop('norm', None)
68836884
cmap = kwargs.pop('cmap', None)
68846885
vmin = kwargs.pop('vmin', None)
@@ -7003,7 +7004,7 @@ def pcolorfast(self, *args, **kwargs):
70037004
luminance data. If either are *None*, the min and max of the color
70047005
array *C* is used. If you pass a norm instance, *vmin* and *vmax*
70057006
will be *None*.
7006-
*alpha*: 0 <= scalar <= 1
7007+
*alpha*: 0 <= scalar <= 1 or *None*
70077008
the alpha blending value
70087009
70097010
Return value is an image if a regular or rectangular grid
@@ -7014,7 +7015,7 @@ def pcolorfast(self, *args, **kwargs):
70147015

70157016
if not self._hold: self.cla()
70167017

7017-
alpha = kwargs.pop('alpha', 1.0)
7018+
alpha = kwargs.pop('alpha', None)
70187019
norm = kwargs.pop('norm', None)
70197020
cmap = kwargs.pop('cmap', None)
70207021
vmin = kwargs.pop('vmin', None)

lib/matplotlib/backend_bases.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,8 @@ def set_alpha(self, alpha):
727727
Set the alpha value used for blending - not supported on
728728
all backends
729729
"""
730+
if alpha is None:
731+
alpha = 1.0
730732
self._alpha = alpha
731733

732734
def set_antialiased(self, b):

lib/matplotlib/cm.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,21 +160,25 @@ def set_colorbar(self, im, ax):
160160
'set the colorbar image and axes associated with mappable'
161161
self.colorbar = im, ax
162162

163-
def to_rgba(self, x, alpha=1.0, bytes=False):
163+
def to_rgba(self, x, alpha=None, bytes=False):
164164
'''Return a normalized rgba array corresponding to *x*. If *x*
165165
is already an rgb array, insert *alpha*; if it is already
166166
rgba, return it unchanged. If *bytes* is True, return rgba as
167167
4 uint8s instead of 4 floats.
168168
'''
169+
if alpha is None:
170+
_alpha = 1.0
171+
else:
172+
_alpha = alpha
169173
try:
170174
if x.ndim == 3:
171175
if x.shape[2] == 3:
172176
if x.dtype == np.uint8:
173-
alpha = np.array(alpha*255, np.uint8)
177+
_alpha = np.array(_alpha*255, np.uint8)
174178
m, n = x.shape[:2]
175179
xx = np.empty(shape=(m,n,4), dtype = x.dtype)
176180
xx[:,:,:3] = x
177-
xx[:,:,3] = alpha
181+
xx[:,:,3] = _alpha
178182
elif x.shape[2] == 4:
179183
xx = x
180184
else:

lib/matplotlib/collections.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -443,25 +443,27 @@ def set_edgecolors(self, c):
443443
def set_alpha(self, alpha):
444444
"""
445445
Set the alpha tranparencies of the collection. *alpha* must be
446-
a float.
446+
a float or *None*.
447447
448-
ACCEPTS: float
448+
ACCEPTS: float or None
449449
"""
450-
try: float(alpha)
451-
except TypeError: raise TypeError('alpha must be a float')
452-
else:
453-
artist.Artist.set_alpha(self, alpha)
454-
try:
455-
self._facecolors = mcolors.colorConverter.to_rgba_array(
456-
self._facecolors_original, self._alpha)
457-
except (AttributeError, TypeError, IndexError):
458-
pass
450+
if alpha is not None:
459451
try:
460-
if self._edgecolors_original != 'face':
461-
self._edgecolors = mcolors.colorConverter.to_rgba_array(
462-
self._edgecolors_original, self._alpha)
463-
except (AttributeError, TypeError, IndexError):
464-
pass
452+
float(alpha)
453+
except TypeError:
454+
raise TypeError('alpha must be a float or None')
455+
artist.Artist.set_alpha(self, alpha)
456+
try:
457+
self._facecolors = mcolors.colorConverter.to_rgba_array(
458+
self._facecolors_original, self._alpha)
459+
except (AttributeError, TypeError, IndexError):
460+
pass
461+
try:
462+
if self._edgecolors_original != 'face':
463+
self._edgecolors = mcolors.colorConverter.to_rgba_array(
464+
self._edgecolors_original, self._alpha)
465+
except (AttributeError, TypeError, IndexError):
466+
pass
465467

466468
def get_linewidths(self):
467469
return self._linewidths

lib/matplotlib/colorbar.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ class ColorbarBase(cm.ScalarMappable):
197197

198198
def __init__(self, ax, cmap=None,
199199
norm=None,
200-
alpha=1.0,
200+
alpha=None,
201201
values=None,
202202
boundaries=None,
203203
orientation='vertical',

lib/matplotlib/contour.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ def __init__(self, ax, *args, **kwargs):
655655
self.linewidths = kwargs.get('linewidths', None)
656656
self.linestyles = kwargs.get('linestyles', None)
657657

658-
self.alpha = kwargs.get('alpha', 1.0)
658+
self.alpha = kwargs.get('alpha', None)
659659
self.origin = kwargs.get('origin', None)
660660
self.extent = kwargs.get('extent', None)
661661
cmap = kwargs.get('cmap', None)

lib/matplotlib/figure.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ def hold(self, b=None):
340340
def figimage(self, X,
341341
xo=0,
342342
yo=0,
343-
alpha=1.0,
343+
alpha=None,
344344
norm=None,
345345
cmap=None,
346346
vmin=None,
@@ -380,7 +380,7 @@ def figimage(self, X,
380380
None, the min and max of the luminance values will be
381381
used. Note if you pass a norm instance, the settings for
382382
*vmin* and *vmax* will be ignored.
383-
alpha the alpha blending value, default is 1.0
383+
alpha the alpha blending value, default is None
384384
origin [ 'upper' | 'lower' ] Indicates where the [0,0] index of
385385
the array is in the upper left or lower left corner of
386386
the axes. Defaults to the rc image.origin value

lib/matplotlib/text.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def get_rotation(rotation):
6161
========================== =========================================================================
6262
Property Value
6363
========================== =========================================================================
64-
alpha float
64+
alpha float or None
6565
animated [True | False]
6666
backgroundcolor any matplotlib color
6767
bbox rectangle prop dict plus key 'pad' which is a pad in points

0 commit comments

Comments
 (0)