8000 ENH: Accept ('color', alpha) tuple to define a color · matplotlib/matplotlib@cb522c9 · GitHub
[go: up one dir, main page]

Skip to content

Commit cb522c9

Browse files
story645j1642
authored andcommitted
ENH: Accept ('color', alpha) tuple to define a color
2 parents 546b9fc + 02fb7e0 commit cb522c9

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

examples/event_handling/cursor_demo.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ def on_mouse_move(self, event):
5656
self.set_cross_hair_visible(True)
5757
x, y = event.xdata, event.ydata
5858
# update the line positions
59-
self.horizontal_line.set_ydata(y)
60-
self.vertical_line.set_xdata(x)
59+
self.horizontal_line.set_ydata([y])
60+
self.vertical_line.set_xdata([x])
6161
self.text.set_text(f'x={x:1.2f}, y={y:1.2f}')
6262
self.ax.figure.canvas.draw()
6363

@@ -132,8 +132,8 @@ def on_mouse_move(self, event):
132132
self.set_cross_hair_visible(True)
133133
# update the line positions
134134
x, y = event.xdata, event.ydata
135-
self.horizontal_line.set_ydata(y)
136-
self.vertical_line.set_xdata(x)
135+
self.horizontal_line.set_ydata([y])
136+
self.vertical_line.set_xdata([x])
137137
self.text.set_text(f'x={x:1.2f}, y={y:1.2f}')
138138

139139
self.ax.figure.canvas.restore_region(self.background)
@@ -204,8 +204,8 @@ def on_mouse_move(self, event):
204204
x = self.x[index]
205205
y = self.y[index]
206206
# update the line positions
207-
self.horizontal_line.set_ydata(y)
208-
self.vertical_line.set_xdata(x)
207+
self.horizontal_line.set_ydata([y])
208+
self.vertical_line.set_xdata([x])
209209
self.text.set_text(f'x={x:1.2f}, y={y:1.2f}')
210210
self.ax.figure.canvas.draw()
211211

lib/matplotlib/colors.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,11 @@ def to_rgba(c, alpha=None):
286286
Tuple of floats ``(r, g, b, a)``, where each channel (red, green, blue,
287287
alpha) can assume values between 0 and 1.
288288
"""
289+
if isinstance(c, tuple) and len(c) == 2 and isinstance(c[0], str):
290+
if 0 <= c[1] <= 1:
291+
c, alpha = c[0], c[1]
292+
else:
293+
raise ValueError('Alpha must be between 0 and 1, inclusive.')
289294
# Special-case nth color syntax because it should not be cached.
290295
if _is_nth_color(c):
291296
prop_cycler = mpl.rcParams['axes.prop_cycle']
@@ -428,6 +433,11 @@ def to_rgba_array(c, alpha=None):
428433 10000
# Special-case inputs that are already arrays, for performance. (If the
429434
# array has the wrong kind or shape, raise the error during one-at-a-time
430435
# conversion.)
436+
if isinstance(c, tuple) and len(c) == 2 and isinstance(c[0], str):
437+
if 0 <= c[1] <= 1:
438+
c, alpha = c[0], c[1]
439+
else:
440+
raise ValueError('Alpha must be between 0 and 1, inclusive.')
431441
if np.iterable(alpha):
432442
alpha = np.asarray(alpha).ravel()
433443
if (isinstance(c, np.ndarray) and c.dtype.kind in "if"

lib/matplotlib/tests/test_colors.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,6 +1301,26 @@ def test_to_rgba_array_alpha_array():
13011301
assert_array_equal(c[:, 3], alpha)
13021302

13031303

1304+
def test_to_rgba_color_alpha_tuple():
1305+
assert mcolors.to_rgba(('red', 0.1)) == mcolors.to_rgba('red', alpha=0.1)
1306+
1307+
1308+
def test_to_rgba_raises_error():
1309+
with pytest.raises(ValueError, match='Alpha must be between 0 and 1,'):
1310+
mcolors.to_rgba(('blue', 2))
1311+
1312+
1313+
def test_to_rgba_array_color_alpha_tuple():
1314+
booleans = mcolors.to_rgba_array(('red', 0.1)) \
1315+
== mcolors.to_rgba_array('red', alpha=0.1)
1316+
assert booleans.all()
1317+
1318+
1319+
def test_to_rgba_array_raises_error():
1320+
with pytest.raises(ValueError, match='Alpha must be between 0 and 1,'):
1321+
mcolors.to_rgba_array(('blue', 2))
1322+
1323+
13041324
def test_failed_conversions():
13051325
with pytest.raises(ValueError):
13061326
mcolors.to_rgba('5')

0 commit comments

Comments
 (0)
0