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

Skip to content

Commit ce52093

Browse files
committed
Merged revisions 8627-8628 via svnmerge from
https://matplotlib.svn.sourceforge.net/svnroot/matplotlib/branches/v1_0_maint ........ r8627 | jdh2358 | 2010-08-13 08:23:33 -1000 (Fri, 13 Aug 2010) | 1 line fix docstring typo ........ r8628 | efiring | 2010-08-14 11:21:58 -1000 (Sat, 14 Aug 2010) | 5 lines fix bugs: patch alpha handling, bar color kwarg interpretation This changeset is somewhat intrusive, with side-effects of moving most patch color handling out of the draw method, and of changing rgb2hex to allow rgba. This simplifies backend_svg slightly. ........ svn path=/trunk/matplotlib/; revision=8629
1 parent 76a4df8 commit ce52093

6 files changed

Lines changed: 74 additions & 53 deletions

File tree

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
2010-08-14 Fix bug in patch alpha handling, and in bar color kwarg - EF
2+
13
2010-08-12 Removed all traces of numerix module after 17 months of
24
deprecation warnings. - EF
35

lib/matplotlib/axes.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4585,13 +4585,17 @@ def make_iterable(x):
45854585
color = [None] * nbars
45864586
else:
45874587
color = list(mcolors.colorConverter.to_rgba_array(color))
4588+
if len(color) == 0: # until to_rgba_array is changed
4589+
color = [[0,0,0,0]]
45884590
if len(color) < nbars:
45894591
color *= nbars
45904592

45914593
if edgecolor is None:
45924594
edgecolor = [None] * nbars
45934595
else:
45944596
edgecolor = list(mcolors.colorConverter.to_rgba_array(edgecolor))
4597+
if len(edgecolor) == 0: # until to_rgba_array is changed
4598+
edgecolor = [[0,0,0,0]]
45954599
if len(edgecolor) < nbars:
45964600
edgecolor *= nbars
45974601

lib/matplotlib/backends/backend_svg.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def _get_hatch(self, gc, rgbFace):
118118
'<rect x="0" y="0" width="%d" height="%d" fill="%s"/>' %
119119
(HATCH_SIZE+1, HATCH_SIZE+1, fill))
120120
path = '<path d="%s" fill="%s" stroke="%s" stroke-width="1.0"/>' % (
121-
path_data, rgb2hex(gc.get_rgb()[:3]), rgb2hex(gc.get_rgb()[:3]))
121+
path_data, rgb2hex(gc.get_rgb()), rgb2hex(gc.get_rgb()))
122122
self._svgwriter.write(path)
123123
self._svgwriter.write('\n </pattern>\n</defs>')
124124
self._hatchd[dictkey] = id
@@ -135,7 +135,7 @@ def _get_style(self, gc, rgbFace):
135135
if rgbFace is None:
136136
fill = 'none'
137137
else:
138-
fill = rgb2hex(rgbFace[:3])
138+
fill = rgb2hex(rgbFace)
139139

140140
offset, seq = gc.get_dashes()
141141
if seq is None:
@@ -149,7 +149,7 @@ def _get_style(self, gc, rgbFace):
149149
return 'fill: %s; stroke: %s; stroke-width: %f; ' \
150150
'stroke-linejoin: %s; stroke-linecap: %s; %s opacity: %f' % (
151151
fill,
152-
rgb2hex(gc.get_rgb()[:3]),
152+
rgb2hex(gc.get_rgb()),
153153
linewidth,
154154
gc.get_joinstyle(),
155155
_capstyle_d[gc.get_capstyle()],
@@ -469,7 +469,7 @@ def draw_text_as_path(self, gc, x, y, s, prop, angle, ismath):
469469
glyph_map=self._glyph_map
470470

471471
text2path = self._text2path
472-
color = rgb2hex(gc.get_rgb()[:3])
472+
color = rgb2hex(gc.get_rgb())
473473
fontsize = prop.get_size_in_points()
474474

475475
write = self._svgwriter.write
@@ -592,7 +592,7 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath):
592592
y -= font.get_descent() / 64.0
593593

594594
fontsize = prop.get_size_in_points()
595-
color = rgb2hex(gc.get_rgb()[:3])
595+
color = rgb2hex(gc.get_rgb())
596596
write = self._svgwriter.write
597597

598598
if rcParams['svg.embed_char_paths']:
@@ -730,7 +730,7 @@ def _draw_mathtext(self, gc, x, y, s, prop, angle):
730730
self.mathtext_parser.parse(s, 72, prop)
731731
svg_glyphs = svg_elements.svg_glyphs
732732
svg_rects = svg_elements.svg_rects
733-
color = rgb2hex(gc.get_rgb()[:3])
733+
color = rgb2hex(gc.get_rgb())
734734
write = self._svgwriter.write
735735

736736
style = "fill: %s" % color

lib/matplotlib/colors.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,8 @@ def is_color_like(c):
217217

218218

219219
def rgb2hex(rgb):
220-
'Given a len 3 rgb tuple of 0-1 floats, return the hex string'
221-
return '#%02x%02x%02x' % tuple([round(val*255) for val in rgb])
220+
'Given an rgb or rgba sequence of 0-1 floats, return the hex string'
221+
return '#%02x%02x%02x' % tuple([round(val*255) for val in rgb[:3]])
222222

223223
hexColorPattern = re.compile("\A#[a-fA-F0-9]{6}\Z")
224224

lib/matplotlib/mlab.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ def prepca(P, frac=0):
785785
- *fracVar* : the fraction of the variance accounted for by each
786786
component returned
787787
788-
A similar function of the same name was in the MATLAB
788+
A similar function of the same name was in the MATLAB
789789
R13 Neural Network Toolbox but is not found in later versions;
790790
its successor seems to be called "processpcs".
791791
"""
@@ -2108,7 +2108,7 @@ def csv2rec(fname, comments='#', skiprows=0, checkrows=0, delimiter=',',
21082108
- *checkrows*: is the number of rows to check to validate the column
21092109
data type. When set to zero all rows are validated.
21102110
2111-
- *converted*: if not *None*, is a dictionary mapping column number or
2111+
- *converterd*: if not *None*, is a dictionary mapping column number or
21122112
munged column name to a converter function.
21132113
21142114
- *names*: if not None, is a list of header names. In this case, no

lib/matplotlib/patches.py

Lines changed: 58 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def __str__(self):
5555
def __init__(self,
5656
edgecolor=None,
5757
facecolor=None,
58+
color=None,
5859
linewidth=None,
5960
linestyle=None,
6061
antialiased = None,
@@ -74,20 +75,22 @@ def __init__(self,
7475
if linestyle is None: linestyle = "solid"
7576
if antialiased is None: antialiased = mpl.rcParams['patch.antialiased']
7677

77-
if 'color' in kwargs:
78+
self._fill = True # needed for set_facecolor call
79+
if color is not None:
7880
if (edgecolor is not None or
7981
facecolor is not None):
8082
import warnings
8183
warnings.warn("Setting the 'color' property will override"
8284
"the edgecolor or facecolor properties. ")
83-
84-
self.set_edgecolor(edgecolor)
85-
self.set_facecolor(facecolor)
85+
self.set_color(color)
86+
else:
87+
self.set_edgecolor(edgecolor)
88+
self.set_facecolor(facecolor)
8689
self.set_linewidth(linewidth)
8790
self.set_linestyle(linestyle)
8891
self.set_antialiased(antialiased)
8992
self.set_hatch(hatch)
90-
self.fill = fill
93+
self.set_fill(fill)
9194
self._combined_transform = transforms.IdentityTransform()
9295

9396
self.set_path_effects(path_effects)
@@ -98,7 +101,7 @@ def get_verts(self):
98101
"""
99102
Return a copy of the vertices used in this patch
100103
101-
If the patch contains Bézier curves, the curves will be
104+
If the patch contains Bezier curves, the curves will be
102105
interpolated by line segments. To access the curves as
103106
curves, use :meth:`get_path`.
104107
"""
@@ -223,7 +226,7 @@ def set_edgecolor(self, color):
223226
ACCEPTS: mpl color spec, or None for default, or 'none' for no color
224227
"""
225228
if color is None: color = mpl.rcParams['patch.edgecolor']
226-
self._edgecolor = color
229+
self._edgecolor = colors.colorConverter.to_rgba(color, self._alpha)
227230

228231
def set_ec(self, color):
229232
"""alias for set_edgecolor"""
@@ -236,7 +239,12 @@ def set_facecolor(self, color):
236239
ACCEPTS: mpl color spec, or None for default, or 'none' for no color
237240
"""
238241
if color is None: color = mpl.rcParams['patch.facecolor']
239-
self._facecolor = color
242+
self._original_facecolor = color # save: otherwise changing _fill
243+
# may lose alpha information
244+
self._facecolor = colors.colorConverter.to_rgba(color, self._alpha)
245+
if not self._fill:
246+
self._facecolor = list(self._facecolor)
247+
self._facecolor[3] = 0
240248

241249
def set_fc(self, color):
242250
"""alias for set_facecolor"""
@@ -256,6 +264,21 @@ def set_color(self, c):
256264
self.set_facecolor(c)
257265
self.set_edgecolor(c)
258266

267+
def set_alpha(self, alpha):
268+
"""
269+
Set the alpha tranparency of the patch.
270+
271+
ACCEPTS: float or None
272+
"""
273+
if alpha is not None:
274+
try:
275+
float(alpha)
276+
except TypeError:
277+
raise TypeError('alpha must be a float or None')
278+
artist.Artist.set_alpha(self, alpha)
279+
self.set_facecolor(self._original_facecolor) # using self._fill and self._alpha
280+
self._edgecolor = colors.colorConverter.to_rgba(
281+
self._edgecolor[:3], self._alpha)
259282

260283
def set_linewidth(self, w):
261284
"""
@@ -289,11 +312,12 @@ def set_fill(self, b):
289312
290313
ACCEPTS: [True | False]
291314
"""
292-
self.fill = b
315+
self._fill = b
316+
self.set_facecolor(self._original_facecolor)
293317

294318
def get_fill(self):
295319
'return whether fill is set'
296-
return self.fill
320+
return self._fill
297321

298322
def set_hatch(self, hatch):
299323
"""
@@ -345,28 +369,24 @@ def draw(self, renderer):
345369
renderer.open_group('patch', self.get_gid())
346370
gc = renderer.new_gc()
347371

348-
if cbook.is_string_like(self._edgecolor) and self._edgecolor.lower()=='none':
349-
gc.set_linewidth(0)
350-
else:
351-
gc.set_foreground(self._edgecolor)
352-
gc.set_linewidth(self._linewidth)
353-
gc.set_linestyle(self._linestyle)
372+
gc.set_alpha(self._edgecolor[3])
373+
gc.set_foreground(self._edgecolor, isRGB=True)
374+
375+
lw = self._linewidth
376+
if self._edgecolor[3] == 0:
377+
lw = 0
378+
gc.set_linewidth(lw)
379+
gc.set_linestyle(self._linestyle)
354380

355381
gc.set_antialiased(self._antialiased)
356382
self._set_gc_clip(gc)
357383
gc.set_capstyle('projecting')
358384
gc.set_url(self._url)
359385
gc.set_snap(self.get_snap())
360386

361-
if (not self.fill or self._facecolor is None or
362-
(cbook.is_string_like(self._facecolor) and self._facecolor.lower()=='none')):
363-
rgbFace = None
364-
gc.set_alpha(1.0)
365-
else:
366-
r, g, b, a = colors.colorConverter.to_rgba(self._facecolor, self._alpha)
367-
rgbFace = (r, g, b)
368-
gc.set_alpha(a)
369-
387+
rgbFace = self._facecolor
388+
if rgbFace[3] == 0:
389+
rgbFace = None # (some?) renderers expect this as no-fill signal
370390

371391
if self._hatch:
372392
gc.set_hatch(self._hatch )
@@ -3823,39 +3843,35 @@ def get_path_in_displaycoord(self):
38233843
)
38243844

38253845
#if not fillable:
3826-
# self.fill = False
3846+
# self._fill = False
38273847

38283848
return _path, fillable
38293849

38303850

38313851

38323852
def draw(self, renderer):
38333853
if not self.get_visible(): return
3834-
#renderer.open_group('patch')
3854+
3855+
renderer.open_group('patch', self.get_gid())
38353856
gc = renderer.new_gc()
38363857

3858+
gc.set_alpha(self._edgecolor[3])
3859+
gc.set_foreground(self._edgecolor, isRGB=True)
38373860

3838-
if cbook.is_string_like(self._edgecolor) and self._edgecolor.lower()=='none':
3839-
gc.set_linewidth(0)
3840-
else:
3841-
gc.set_foreground(self._edgecolor)
3842-
gc.set_linewidth(self._linewidth)
3843-
gc.set_linestyle(self._linestyle)
3861+
lw = self._linewidth
3862+
if self._edgecolor[3] == 0:
3863+
lw = 0
3864+
gc.set_linewidth(lw)
3865+
gc.set_linestyle(self._linestyle)
38443866

38453867
gc.set_antialiased(self._antialiased)
38463868
self._set_gc_clip(gc)
38473869
gc.set_capstyle('round')
38483870
gc.set_snap(self.get_snap())
38493871

3850-
if (not self.fill or self._facecolor is None or
3851-
(cbook.is_string_like(self._facecolor) and self._facecolor.lower()=='none')):
3852-
rgbFace = None
3853-
gc.set_alpha(1.0)
3854-
else:
3855-
r, g, b, a = colors.colorConverter.to_rgba(self._facecolor, self._alpha)
3856-
rgbFace = (r, g, b)
3857-
gc.set_alpha(a)
3858-
3872+
rgbFace = self._facecolor
3873+
if rgbFace[3] == 0:
3874+
rgbFace = None # (some?) renderers expect this as no-fill signal
38593875

38603876
if self._hatch:
38613877
gc.set_hatch(self._hatch )
@@ -3870,7 +3886,6 @@ def draw(self, renderer):
38703886

38713887
affine = transforms.IdentityTransform()
38723888

3873-
renderer.open_group('patch', self.get_gid())
38743889

38753890
if self.get_path_effects():
38763891
for path_effect in self.get_path_effects():

0 commit comments

Comments
 (0)