Skip to content

Commit 74cf7ca

Browse files
committed
refactor alpha, but failed alpha blending in agg
1 parent 4852f6b commit 74cf7ca

25 files changed

+202
-131
lines changed

lib/matplotlib/artist.py

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from .path import Path
1717

1818
from .traitlets import (Instance, Configurable, gTransformInstance, Bool, Undefined,
19-
BaseDescriptor, getargspec, PrivateMethodMixin)
19+
BaseDescriptor, getargspec, PrivateMethodMixin, Float, TraitError)
2020

2121
# Note, matplotlib artists use the doc strings for set and get
2222
# methods to enable the introspection methods of setp and getp. Every
@@ -146,6 +146,20 @@ def _animated_changed(self, name, new):
146146
self.pchanged()
147147
self.stale = True
148148

149+
# Float defaults to 0.0, must have None arg
150+
alpha = Float(None, allow_none=True)
151+
152+
def _alpha_validate(self, value, trait):
153+
if 0>value>1:
154+
msg = ("The '%s' trait of %s instance can only be"
155+
"transparent (0.0) through opaque (1.0)")
156+
raise TraitError(msg % (trait.name, self.__class__))
157+
return value
158+
159+
def _alpha_changed(self, name, new):
160+
self.pchanged()
161+
self.stale = True
162+
149163
def __init__(self):
150164
# self._stale = True
151165
# self._axes = None
@@ -157,7 +171,7 @@ def __init__(self):
157171

158172
# self._visible = True
159173
# self._animated = False
160-
self._alpha = None
174+
# self._alpha = None
161175
self.clipbox = None
162176
self._clippath = None
163177
self._clipon = True
@@ -765,12 +779,13 @@ def set_clip_path(self, path, transform=None):
765779
self.pchanged()
766780
self.stale = True
767781

768-
def get_alpha(self):
769-
"""
770-
Return the alpha value used for blending - not supported on all
771-
backends
772-
"""
773-
return self._alpha
782+
#!DEPRECATED
783+
# def get_alpha(self):
784+
# """
785+
# Return the alpha value used for blending - not supported on all
786+
# backends
787+
# """
788+
# return self._alpha
774789

775790
#!DEPRECATED
776791
# def get_visible(self):
@@ -864,16 +879,17 @@ def draw(self, renderer, *args, **kwargs):
864879
return
865880
self.stale = False
866881

867-
def set_alpha(self, alpha):
868-
"""
869-
Set the alpha value used for blending - not supported on
870-
all backends.
882+
#!DEPRECATED
883+
# def set_alpha(self, alpha):
884+
# """
885+
# Set the alpha value used for blending - not supported on
886+
# all backends.
871887

872-
ACCEPTS: float (0.0 transparent through 1.0 opaque)
873-
"""
874-
self._alpha = alpha
875-
self.pchanged()
876-
self.stale = True
888+
# ACCEPTS: float (0.0 transparent through 1.0 opaque)
889+
# """
890+
# self._alpha = alpha
891+
# self.pchanged()
892+
# self.stale = True
877893

878894
#!DEPRECATED
879895
# def set_visible(self, b):
@@ -966,7 +982,7 @@ def update_from(self, other):
966982
self.private('transform', other.private('transform'))
967983
self.transform_set = other.transform_set
968984
self.private('visible', other.private('visible'))
969-
self._alpha = other._alpha
985+
self.private('alpha',other.alpha)
970986
self.clipbox = other.clipbox
971987
self._clipon = other._clipon
972988
self._clippath = other._clippath

lib/matplotlib/axes/_axes.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4269,7 +4269,7 @@ def hexbin(self, x, y, C=None, gridsize=100, bins=None,
42694269
collection.set_array(accum)
42704270
collection.set_cmap(cmap)
42714271
collection.set_norm(norm)
4272-
collection.set_alpha(alpha)
4272+
collection.alpha = alpha
42734273
collection.update(kwargs)
42744274

42754275
if vmin is not None or vmax is not None:
@@ -4330,7 +4330,7 @@ def coarse_bin(x, y, coarse):
43304330
hbar.set_array(values)
43314331
hbar.set_cmap(cmap)
43324332
hbar.set_norm(norm)
4333-
hbar.set_alpha(alpha)
4333+
hbar.alpha = alpha
43344334
hbar.update(kwargs)
43354335
self.add_collection(hbar, autolim=False)
43364336

@@ -4358,7 +4358,7 @@ def coarse_bin(x, y, coarse):
43584358
vbar.set_array(values)
43594359
vbar.set_cmap(cmap)
43604360
vbar.set_norm(norm)
4361-
vbar.set_alpha(alpha)
4361+
vbar.alpha = alpha
43624362
vbar.update(kwargs)
43634363
self.add_collection(vbar, autolim=False)
43644364

@@ -4940,7 +4940,7 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
49404940
resample=resample, **kwargs)
49414941

49424942
im.set_data(X)
4943-
im.set_alpha(alpha)
4943+
im.alpha = alpha
49444944
if im.get_clip_path() is None:
49454945
# image does not already have clipping set, clip to axes patch
49464946
im.set_clip_path(self.patch)
@@ -5256,7 +5256,7 @@ def pcolor(self, *args, **kwargs):
52565256

52575257
collection = mcoll.PolyCollection(verts, **kwargs)
52585258

5259-
collection.set_alpha(alpha)
5259+
collection.alpha = alpha
52605260
collection.set_array(C)
52615261
if norm is not None and not isinstance(norm, mcolors.Normalize):
52625262
msg = "'norm' must be an instance of 'mcolors.Normalize'"
@@ -5407,7 +5407,7 @@ def pcolormesh(self, *args, **kwargs):
54075407
collection = mcoll.QuadMesh(
54085408
Nx - 1, Ny - 1, coords,
54095409
antialiased=antialiased, shading=shading, **kwargs)
5410-
collection.set_alpha(alpha)
5410+
collection.alpha = alpha
54115411
collection.set_array(C)
54125412
if norm is not None and not isinstance(norm, mcolors.Normalize):
54135413
msg = "'norm' must be an instance of 'mcolors.Normalize'"
@@ -5591,7 +5591,7 @@ def pcolorfast(self, *args, **kwargs):
55915591
# handle relevant superclass kwargs; the initializer
55925592
# should do much more than it does now.
55935593
collection = mcoll.QuadMesh(nc, nr, coords, 0, edgecolors="None")
5594-
collection.set_alpha(alpha)
5594+
collection.alpha = alpha
55955595
collection.set_array(C)
55965596
collection.set_cmap(cmap)
55975597
collection.set_norm(norm)
@@ -5610,7 +5610,7 @@ def pcolorfast(self, *args, **kwargs):
56105610
extent=(xl, xr, yb, yt),
56115611
**kwargs)
56125612
im.set_data(C)
5613-
im.set_alpha(alpha)
5613+
im.alpha = alpha
56145614
self.add_image(im)
56155615
ret = im
56165616

lib/matplotlib/axes/_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2300,7 +2300,7 @@ def draw(self, renderer=None, inframe=False):
23002300
zorder_images.sort(key=lambda x: x[0])
23012301

23022302
mag = renderer.get_image_magnification()
2303-
ims = [(im.make_image(mag), 0, 0, im.get_alpha())
2303+
ims = [(im.make_image(mag), 0, 0, im.alpha)
23042304
for z, im in zorder_images]
23052305

23062306
l, b, r, t = self.bbox.extents

lib/matplotlib/backends/backend_nbagg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def closer(event):
217217

218218
canvas = FigureCanvasNbAgg(figure)
219219
if rcParams['nbagg.transparent']:
220-
figure.patch.set_alpha(0)
220+
figure.patch.alpha = 0
221221
manager = FigureManagerNbAgg(canvas, num)
222222

223223
if is_interactive():

lib/matplotlib/collections.py

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ def set_facecolor(self, c):
609609
if c is None:
610610
c = mpl.rcParams['patch.facecolor']
611611
self._facecolors_original = c
612-
self._facecolors = mcolors.colorConverter.to_rgba_array(c, self._alpha)
612+
self._facecolors = mcolors.colorConverter.to_rgba_array(c, self.alpha)
613613
self.stale = True
614614

615615
def set_facecolors(self, c):
@@ -657,39 +657,57 @@ def set_edgecolor(self, c):
657657
if c is None:
658658
c = mpl.rcParams['patch.edgecolor']
659659
self._edgecolors_original = c
660-
self._edgecolors = mcolors.colorConverter.to_rgba_array(c, self._alpha)
660+
self._edgecolors = mcolors.colorConverter.to_rgba_array(c, self.alpha)
661661
self.stale = True
662662

663663
def set_edgecolors(self, c):
664664
"""alias for set_edgecolor"""
665665
return self.set_edgecolor(c)
666666

667-
def set_alpha(self, alpha):
668-
"""
669-
Set the alpha tranparencies of the collection. *alpha* must be
670-
a float or *None*.
667+
def _alpha_validate(self, value, trait):
668+
value = artist.Artist._alpha_validate(self, value, trait)
671669

672-
ACCEPTS: float or None
673-
"""
674-
if alpha is not None:
675-
try:
676-
float(alpha)
677-
except TypeError:
678-
raise TypeError('alpha must be a float or None')
679-
artist.Artist.set_alpha(self, alpha)
680670
try:
681671
self._facecolors = mcolors.colorConverter.to_rgba_array(
682-
self._facecolors_original, self._alpha)
672+
self._facecolors_original, value)
683673
except (AttributeError, TypeError, IndexError):
684674
pass
685675
try:
686676
if (not isinstance(self._edgecolors_original, six.string_types)
687677
or self._edgecolors_original != str('face')):
688678
self._edgecolors = mcolors.colorConverter.to_rgba_array(
689-
self._edgecolors_original, self._alpha)
679+
self._edgecolors_original, value)
690680
except (AttributeError, TypeError, IndexError):
691681
pass
692682

683+
return value
684+
685+
#!DEPRECATED
686+
# def set_alpha(self, alpha):
687+
# """
688+
# Set the alpha tranparencies of the collection. *alpha* must be
689+
# a float or *None*.
690+
691+
# ACCEPTS: float or None
692+
# """
693+
# if alpha is not None:
694+
# try:
695+
# float(alpha)
696+
# except TypeError:
697+
# raise TypeError('alpha must be a float or None')
698+
# artist.Artist.set_alpha(self, alpha)
699+
# try:
700+
# self._facecolors = mcolors.colorConverter.to_rgba_array(
701+
# self._facecolors_original, self._alpha)
702+
# except (AttributeError, TypeError, IndexError):
703+
# pass
704+
# try:
705+
# if self._edgecolors_original != str('face'):
706+
# self._edgecolors = mcolors.colorConverter.to_rgba_array(
707+
# self._edgecolors_original, self._alpha)
708+
# except (AttributeError, TypeError, IndexError):
709+
# pass
710+
693711
def get_linewidths(self):
694712
return self._linewidths
695713
get_linewidth = get_linewidths
@@ -710,9 +728,9 @@ def update_scalarmappable(self):
710728
if not self.check_update("array"):
711729
return
712730
if self._is_filled:
713-
self._facecolors = self.to_rgba(self._A, self._alpha)
731+
self._facecolors = self.to_rgba(self._A, self.alpha)
714732
elif self._is_stroked:
715-
self._edgecolors = self.to_rgba(self._A, self._alpha)
733+
self._edgecolors = self.to_rgba(self._A, self.alpha)
716734
self.stale = True
717735

718736
def get_fill(self):

lib/matplotlib/colorbar.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ def __init__(self, ax, mappable, **kw):
885885

886886
if isinstance(mappable, contour.ContourSet):
887887
CS = mappable
888-
kw['alpha'] = mappable.get_alpha()
888+
kw['alpha'] = mappable.alpha
889889
kw['boundaries'] = CS._levels
890890
kw['values'] = CS.cvalues
891891
kw['extend'] = CS.extend
@@ -900,7 +900,7 @@ def __init__(self, ax, mappable, **kw):
900900
kw.setdefault('extend', cmap.colorbar_extend)
901901

902902
if isinstance(mappable, martist.Artist):
903-
kw['alpha'] = mappable.get_alpha()
903+
kw['alpha'] = mappable.alpha
904904

905905
ColorbarBase.__init__(self, ax, **kw)
906906

@@ -972,7 +972,7 @@ def update_bruteforce(self, mappable):
972972
self.solids = None
973973
self.lines = list()
974974
self.dividers = None
975-
self.set_alpha(mappable.get_alpha())
975+
self.set_alpha(mappable.alpha)
976976
self.cmap = mappable.cmap
977977
self.norm = mappable.norm
978978
self.config_axis()

lib/matplotlib/contour.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,7 +1017,7 @@ def legend_elements(self, variable_name='x', str_format=str):
10171017
(0, 0), 1, 1,
10181018
facecolor=collection.get_facecolor()[0],
10191019
hatch=collection.get_hatch(),
1020-
alpha=collection.get_alpha())
1020+
alpha=collection.alpha)
10211021
artists.append(patch)
10221022

10231023
lower = str_format(lower)
@@ -1134,7 +1134,7 @@ def changed(self):
11341134
else:
11351135
collection.set_color(color)
11361136
for label, cv in zip(self.labelTexts, self.labelCValues):
1137-
label.set_alpha(self.alpha)
1137+
label.alpha = self.alpha
11381138
label.set_color(self.labelMappable.to_rgba(cv))
11391139
# add label colors
11401140
cm.ScalarMappable.changed(self)

lib/matplotlib/figure.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ def figimage(self, X,
659659
im.stale_callback = _stale_figure_callback
660660

661661
im.set_array(X)
662-
im.set_alpha(alpha)
662+
im.alpha = alpha
663663
if norm is None:
664664
im.set_clim(vmin, vmax)
665665
self.images.append(im)
@@ -1096,7 +1096,7 @@ def draw(self, renderer):
10961096
# make a composite image blending alpha
10971097
# list of (_image.Image, ox, oy)
10981098
mag = renderer.get_image_magnification()
1099-
ims = [(im.make_image(mag), im.ox, im.oy, im.get_alpha())
1099+
ims = [(im.make_image(mag), im.ox, im.oy, im.alpha)
11001100
for im in self.images]
11011101

11021102
im = _image.from_images(int(self.bbox.height * mag),

lib/matplotlib/finance.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,7 @@ def _candlestick(ax, quotes, width=0.2, colorup='k', colordown='r',
805805
facecolor=color,
806806
edgecolor=color,
807807
)
808-
rect.set_alpha(alpha)
808+
rect.alpha = alpha
809809

810810
lines.append(vline)
811811
patches.append(rect)

0 commit comments

Comments
 (0)