From 2834c09cd418cf41cc92bf5d08df7f3556277711 Mon Sep 17 00:00:00 2001 From: David Reed Date: Sun, 19 Jan 2014 12:22:42 -0500 Subject: [PATCH 01/24] NF - added new feature to colors called shade_color to lighten or darken a given color by a given percentage --- lib/matplotlib/colors.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 1066cf8067c6..d6b9d3982276 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -51,6 +51,7 @@ import numpy as np from numpy import ma import matplotlib.cbook as cbook +from colorsys import rgb_to_hls as rgb2hls, hls_to_rgb as hls2rgb parts = np.__version__.split('.') NP_MAJOR, NP_MINOR = map(int, parts[:2]) @@ -1483,3 +1484,38 @@ def from_levels_and_colors(levels, colors, extend='neither'): norm = BoundaryNorm(levels, ncolors=n_data_colors) return cmap, norm + +def shade_color(color, percent): + """Shade Color + + This color utility function allows the user to easily darken or lighten a color for + plotting purposes. + + Parameters + ---------- + color : string, list, hexvalue + Any acceptable Matplotlib color value, such as 'red', 'slategrey', '#FFEE11', (1,0,0) + + percent : the amount by which to brighten or darken the color. + + Returns + ------- + color : tuple of floats + tuple representing converted rgb values + + """ + + cc = CC() + + rgb = cc.to_rgb(color) + + h,l,s = rgb2hls(*rgb) + + l *= 1 + float(percent)/100 + + l = min(1, l) + l = max(0, l) + + r,g,b = hls2rgb(h,l,s) + + return r,g,b \ No newline at end of file From c926ba5a24c7ceb8d5cbcd69a908d961fe5fd189 Mon Sep 17 00:00:00 2001 From: David Reed Date: Sun, 19 Jan 2014 12:33:27 -0500 Subject: [PATCH 02/24] NF - added new feature to colors called shade_color to lighten or darken a given color by a given percentage (altered CC to ColorConverter) --- lib/matplotlib/colors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index d6b9d3982276..ee50aed969ac 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -1505,7 +1505,7 @@ def shade_color(color, percent): """ - cc = CC() + cc = ColorConverter() rgb = cc.to_rgb(color) From d4233b87715338c77139e4328ba45c7020d7d53d Mon Sep 17 00:00:00 2001 From: David Reed Date: Sat, 16 May 2015 12:11:35 -0400 Subject: [PATCH 03/24] NF - added new feature to colors called shade_color to lighten or darken a given color by a given percentage, added modifications noted in PR #2745 which included fixing PEP8 errors and adding a test. --- lib/matplotlib/colors.py | 25 ++++++++++++++----------- lib/matplotlib/tests/test_colors.py | 29 +++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index ee50aed969ac..3dc716ca65be 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -48,10 +48,12 @@ """ from __future__ import print_function, division import re +from colorsys import rgb_to_hls, hls_to_rgb + import numpy as np from numpy import ma import matplotlib.cbook as cbook -from colorsys import rgb_to_hls as rgb2hls, hls_to_rgb as hls2rgb + parts = np.__version__.split('.') NP_MAJOR, NP_MINOR = map(int, parts[:2]) @@ -1485,11 +1487,15 @@ def from_levels_and_colors(levels, colors, extend='neither'): norm = BoundaryNorm(levels, ncolors=n_data_colors) return cmap, norm + def shade_color(color, percent): - """Shade Color + """ + A color helper utility to either darken or lighten given color. This color utility function allows the user to easily darken or lighten a color for - plotting purposes. + plotting purposes. This function first converts the given color to RGB using + ColorConverter and then to HSL. The saturation is modified according to the given + percentage and converted back to RGB. Parameters ---------- @@ -1505,17 +1511,14 @@ def shade_color(color, percent): """ - cc = ColorConverter() - - rgb = cc.to_rgb(color) + rgb = colorConverter.to_rgb(color) - h,l,s = rgb2hls(*rgb) + h, l, s = rgb_to_hls(*rgb) l *= 1 + float(percent)/100 - l = min(1, l) - l = max(0, l) + l = np.clip(l, 0, 1) - r,g,b = hls2rgb(h,l,s) + r, g, b = hls_to_rgb(h, l, s) - return r,g,b \ No newline at end of file + return r, g, b diff --git a/lib/matplotlib/tests/test_colors.py b/lib/matplotlib/tests/test_colors.py index 2524f82bc62b..cbdc4c6d566d 100644 --- a/lib/matplotlib/tests/test_colors.py +++ b/lib/matplotlib/tests/test_colors.py @@ -1,8 +1,7 @@ from __future__ import print_function from nose.tools import assert_raises import numpy as np -from numpy.testing.utils import assert_array_equal, assert_array_almost_equal - +from numpy.testing.utils import assert_array_equal, assert_array_almost_equal, assert_equal import matplotlib.colors as mcolors import matplotlib.cm as cm @@ -175,6 +174,32 @@ def test_cmap_and_norm_from_levels_and_colors2(): assert_raises(ValueError, mcolors.from_levels_and_colors, levels, colors) +def _shade_test_helper(color, shade, expected): + sc = mcolors.shade_color(color, shade) + assert_equal(sc, expected) + + +def test_color_shading(): + test_colors = ( + 'white', + 'red', + 'black', + [0, .5, .9], + 'slategrey', + ) + test_shade = (0, .5, 1, -.5, -1) + known_shaded_result = ( + (1.0, 1.0, 1.0), + (1.0, 0.0049999999999998934, 0.0049999999999998934), + (0.0, 0.0, 0.0), + (0.0, 0.49749999999999983, 0.89549999999999996), + (0.43433441408059281, 0.49694117647058816, 0.55954793886058363) + ) + for color, shade, expected in zip(test_colors, test_shade, known_shaded_result): + _shade_test_helper(color, shade, expected) + + + if __name__ == '__main__': import nose nose.runmodule(argv=['-s', '--with-doctest'], exit=False) From eca57fe3786849346359bd568a4a063d4bd87b18 Mon Sep 17 00:00:00 2001 From: David Reed Date: Sat, 16 May 2015 12:40:50 -0400 Subject: [PATCH 04/24] NF - added new feature to colors called shade_color to lighten or darken a given color by a given percentage, added modifications noted in PR #2745 which included fixing PEP8 errors and adding a test. --- doc/users/whats_new.rst | 10 ++++++++++ examples/color/color_shade_demo.py | 21 +++++++++++++++++++++ lib/matplotlib/tests/test_colors.py | 17 ++++++++++------- 3 files changed, 41 insertions(+), 7 deletions(-) create mode 100644 examples/color/color_shade_demo.py diff --git a/doc/users/whats_new.rst b/doc/users/whats_new.rst index 2017452f6cbf..014116b78078 100644 --- a/doc/users/whats_new.rst +++ b/doc/users/whats_new.rst @@ -26,6 +26,16 @@ new in matplotlib-1.3 New plotting features --------------------- +Easy color shading +`````````````````` +Often we would like the ability add subtle color variation to our plots, +especially when similar element are plotted in close proximity to one another. +:func:`matplotlib.colors.shade_color` can be used to take an existing color and +slightly darken it, by giving a negative percentage, or to slightly lighten it, +by giving a positive percentage. + +.. plot:: mpl_examples/color/color_shade_demo.py + `xkcd`-style sketch plotting ```````````````````````````` To give your plots a sense of authority that they may be missing, diff --git a/examples/color/color_shade_demo.py b/examples/color/color_shade_demo.py new file mode 100644 index 000000000000..39b278d4f325 --- /dev/null +++ b/examples/color/color_shade_demo.py @@ -0,0 +1,21 @@ +""" +Demo of shade_color utility. + +""" +import matplotlib.pyplot as plt +import matplotlib.colors as mcolors + +fig = plt.figure() +ax = fig.add_subplot(111) + +lightened_color = mcolors.shade_color('blue', -50) +darkened_color = mcolors.shade_color('blue', +50) + +ax.fill_between([0,1], [0,0], [1,1], facecolor=darkened_color) +ax.fill_between([0,1], [0,0], [.66, .66], facecolor='blue') +ax.fill_between([0,1], [0,0], [.33, .33], facecolor=lightened_color) + +plt.xlim([0, 1]) +plt.ylim([0, 1]) + +plt.show() \ No newline at end of file diff --git a/lib/matplotlib/tests/test_colors.py b/lib/matplotlib/tests/test_colors.py index cbdc4c6d566d..954c3ddb3611 100644 --- a/lib/matplotlib/tests/test_colors.py +++ b/lib/matplotlib/tests/test_colors.py @@ -181,25 +181,28 @@ def _shade_test_helper(color, shade, expected): def test_color_shading(): test_colors = ( - 'white', 'red', - 'black', + 'red', + 'red', + 'red', + 'red', [0, .5, .9], 'slategrey', ) - test_shade = (0, .5, 1, -.5, -1) + test_shade = (0, 50, 100, -50, -100, 20, -20) known_shaded_result = ( + (1.0, 0.0, 0.0), + (1.0, 0.5, 0.5), (1.0, 1.0, 1.0), - (1.0, 0.0049999999999998934, 0.0049999999999998934), + (0.5, 0.0, 0.0), (0.0, 0.0, 0.0), - (0.0, 0.49749999999999983, 0.89549999999999996), - (0.43433441408059281, 0.49694117647058816, 0.55954793886058363) + (0.080000000000000071, 0.59111111111111092, 1.0), + (0.35097730430754981, 0.40156862745098038, 0.45215995059441105) ) for color, shade, expected in zip(test_colors, test_shade, known_shaded_result): _shade_test_helper(color, shade, expected) - if __name__ == '__main__': import nose nose.runmodule(argv=['-s', '--with-doctest'], exit=False) From 3216669eb133ccb1932f7972584e3ef84837166c Mon Sep 17 00:00:00 2001 From: domspad Date: Sat, 11 Jul 2015 15:02:22 -0500 Subject: [PATCH 05/24] using plt --- examples/pylab_examples/annotation_demo2.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/pylab_examples/annotation_demo2.py b/examples/pylab_examples/annotation_demo2.py index 3276a4d60d3f..0825359cac5a 100644 --- a/examples/pylab_examples/annotation_demo2.py +++ b/examples/pylab_examples/annotation_demo2.py @@ -1,10 +1,10 @@ -from matplotlib.pyplot import figure, show +import matplotlib.pyplot as plt from matplotlib.patches import Ellipse import numpy as np if 1: - fig = figure(1, figsize=(8, 5)) + fig = plt.figure(1, figsize=(8, 5)) ax = fig.add_subplot(111, autoscale_on=False, xlim=(-1, 5), ylim=(-4, 3)) t = np.arange(0.0, 5.0, 0.01) @@ -80,7 +80,7 @@ if 1: - fig = figure(2) + fig = plt.figure(2) fig.clf() ax = fig.add_subplot(111, autoscale_on=False, xlim=(-1, 5), ylim=(-5, 3)) @@ -150,4 +150,4 @@ ) -show() +plt.show() From 90f63f8a65b5c99fbd7669cd0e4c3dd1586f6ca0 Mon Sep 17 00:00:00 2001 From: domspad Date: Sat, 11 Jul 2015 15:05:46 -0500 Subject: [PATCH 06/24] pylab to plt --- examples/pylab_examples/annotation_demo.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/pylab_examples/annotation_demo.py b/examples/pylab_examples/annotation_demo.py index e58f9cac776f..d76098287fc4 100644 --- a/examples/pylab_examples/annotation_demo.py +++ b/examples/pylab_examples/annotation_demo.py @@ -35,7 +35,7 @@ """ -from matplotlib.pyplot import figure, show +import matplotlib.pyplot as plt from matplotlib.patches import Ellipse import numpy as np @@ -43,7 +43,7 @@ if 1: # if only one location is given, the text and xypoint being # annotated are assumed to be the same - fig = figure() + fig = plt.figure() ax = fig.add_subplot(111, autoscale_on=False, xlim=(-1, 5), ylim=(-3, 5)) t = np.arange(0.0, 5.0, 0.01) @@ -93,7 +93,7 @@ # example is placed in the fractional figure coordinate system. # Text keyword args like horizontal and vertical alignment are # respected - fig = figure() + fig = plt.figure() ax = fig.add_subplot(111, polar=True) r = np.arange(0, 1, 0.001) theta = 2*2*np.pi*r @@ -138,4 +138,4 @@ ax.set_xlim(-20, 20) ax.set_ylim(-20, 20) -show() +plt.show() From 1ee298612658786632aad395cbfc9629633d0098 Mon Sep 17 00:00:00 2001 From: domspad Date: Sat, 11 Jul 2015 15:12:48 -0500 Subject: [PATCH 07/24] pylab to plt and np --- examples/pylab_examples/anscombe.py | 63 +++++++++++++++-------------- 1 file changed, 32 insertions(+), 31 deletions(-) diff --git a/examples/pylab_examples/anscombe.py b/examples/pylab_examples/anscombe.py index b5dd3b316d72..9e6b25e9eb0c 100755 --- a/examples/pylab_examples/anscombe.py +++ b/examples/pylab_examples/anscombe.py @@ -9,51 +9,52 @@ matplotlib fun for a rainy day """ -from pylab import * +import matplotlib.pyplot as plt +import numpy as np -x = array([10, 8, 13, 9, 11, 14, 6, 4, 12, 7, 5]) -y1 = array([8.04, 6.95, 7.58, 8.81, 8.33, 9.96, 7.24, 4.26, 10.84, 4.82, 5.68]) -y2 = array([9.14, 8.14, 8.74, 8.77, 9.26, 8.10, 6.13, 3.10, 9.13, 7.26, 4.74]) -y3 = array([7.46, 6.77, 12.74, 7.11, 7.81, 8.84, 6.08, 5.39, 8.15, 6.42, 5.73]) -x4 = array([8, 8, 8, 8, 8, 8, 8, 19, 8, 8, 8]) -y4 = array([6.58, 5.76, 7.71, 8.84, 8.47, 7.04, 5.25, 12.50, 5.56, 7.91, 6.89]) +x = np.array([10, 8, 13, 9, 11, 14, 6, 4, 12, 7, 5]) +y1 = np.array([8.04, 6.95, 7.58, 8.81, 8.33, 9.96, 7.24, 4.26, 10.84, 4.82, 5.68]) +y2 = np.array([9.14, 8.14, 8.74, 8.77, 9.26, 8.10, 6.13, 3.10, 9.13, 7.26, 4.74]) +y3 = np.array([7.46, 6.77, 12.74, 7.11, 7.81, 8.84, 6.08, 5.39, 8.15, 6.42, 5.73]) +x4 = np.array([8, 8, 8, 8, 8, 8, 8, 19, 8, 8, 8]) +y4 = np.array([6.58, 5.76, 7.71, 8.84, 8.47, 7.04, 5.25, 12.50, 5.56, 7.91, 6.89]) def fit(x): return 3 + 0.5*x -xfit = array([amin(x), amax(x)]) +xfit = np.array([np.amin(x), np.amax(x)]) -subplot(221) -plot(x, y1, 'ks', xfit, fit(xfit), 'r-', lw=2) -axis([2, 20, 2, 14]) -setp(gca(), xticklabels=[], yticks=(4, 8, 12), xticks=(0, 10, 20)) -text(3, 12, 'I', fontsize=20) +plt.subplot(221) +plt.plot(x, y1, 'ks', xfit, fit(xfit), 'r-', lw=2) +plt.axis([2, 20, 2, 14]) +plt.setp(plt.gca(), xticklabels=[], yticks=(4, 8, 12), xticks=(0, 10, 20)) +plt.text(3, 12, 'I', fontsize=20) -subplot(222) -plot(x, y2, 'ks', xfit, fit(xfit), 'r-', lw=2) -axis([2, 20, 2, 14]) -setp(gca(), xticklabels=[], yticks=(4, 8, 12), yticklabels=[], xticks=(0, 10, 20)) -text(3, 12, 'II', fontsize=20) +plt.subplot(222) +plt.plot(x, y2, 'ks', xfit, fit(xfit), 'r-', lw=2) +plt.axis([2, 20, 2, 14]) +plt.setp(plt.gca(), xticklabels=[], yticks=(4, 8, 12), yticklabels=[], xticks=(0, 10, 20)) +plt.text(3, 12, 'II', fontsize=20) -subplot(223) -plot(x, y3, 'ks', xfit, fit(xfit), 'r-', lw=2) -axis([2, 20, 2, 14]) -text(3, 12, 'III', fontsize=20) -setp(gca(), yticks=(4, 8, 12), xticks=(0, 10, 20)) +plt.subplot(223) +plt.plot(x, y3, 'ks', xfit, fit(xfit), 'r-', lw=2) +plt.axis([2, 20, 2, 14]) +plt.text(3, 12, 'III', fontsize=20) +plt.setp(plt.gca(), yticks=(4, 8, 12), xticks=(0, 10, 20)) -subplot(224) +plt.subplot(224) -xfit = array([amin(x4), amax(x4)]) -plot(x4, y4, 'ks', xfit, fit(xfit), 'r-', lw=2) -axis([2, 20, 2, 14]) -setp(gca(), yticklabels=[], yticks=(4, 8, 12), xticks=(0, 10, 20)) -text(3, 12, 'IV', fontsize=20) +xfit = np.array([np.amin(x4), np.amax(x4)]) +plt.plot(x4, y4, 'ks', xfit, fit(xfit), 'r-', lw=2) +plt.axis([2, 20, 2, 14]) +plt.setp(plt.gca(), yticklabels=[], yticks=(4, 8, 12), xticks=(0, 10, 20)) +plt.text(3, 12, 'IV', fontsize=20) # verify the stats pairs = (x, y1), (x, y2), (x, y3), (x4, y4) for x, y in pairs: - print('mean=%1.2f, std=%1.2f, r=%1.2f' % (mean(y), std(y), corrcoef(x, y)[0][1])) + print('mean=%1.2f, std=%1.2f, r=%1.2f' % (np.mean(y), np.std(y), np.corrcoef(x, y)[0][1])) -show() +plt.show() From 0fa4b9462ee629e08ac4e46c9265d7effc98928e Mon Sep 17 00:00:00 2001 From: domspad Date: Sat, 11 Jul 2015 15:24:45 -0500 Subject: [PATCH 08/24] pylab to plt and np --- examples/pylab_examples/arrow_demo.py | 46 +++++++++++++-------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/examples/pylab_examples/arrow_demo.py b/examples/pylab_examples/arrow_demo.py index afd4d2a3e30f..23f01f1b9581 100644 --- a/examples/pylab_examples/arrow_demo.py +++ b/examples/pylab_examples/arrow_demo.py @@ -45,17 +45,17 @@ def make_arrow_plot(data, size=4, display='length', shape='right', linewidth and edgecolor. """ - xlim(-0.5, 1.5) - ylim(-0.5, 1.5) - gcf().set_size_inches(size, size) - xticks([]) - yticks([]) + plt.xlim(-0.5, 1.5) + plt.ylim(-0.5, 1.5) + plt.gcf().set_size_inches(size, size) + plt.xticks([]) + plt.yticks([]) max_text_size = size*12 min_text_size = size label_text_size = size*2.5 text_params = {'ha': 'center', 'va': 'center', 'family': 'sans-serif', 'fontweight': 'bold'} - r2 = sqrt(2) + r2 = np.sqrt(2) deltas = { 'AT': (1, 0), @@ -103,13 +103,13 @@ def make_arrow_plot(data, size=4, display='length', shape='right', } def do_fontsize(k): - return float(clip(max_text_size*sqrt(data[k]), + return float(clip(max_text_size*np.sqrt(data[k]), min_text_size, max_text_size)) - A = text(0, 1, '$A_3$', color='r', size=do_fontsize('A'), **text_params) - T = text(1, 1, '$T_3$', color='k', size=do_fontsize('T'), **text_params) - G = text(0, 0, '$G_3$', color='g', size=do_fontsize('G'), **text_params) - C = text(1, 0, '$C_3$', color='b', size=do_fontsize('C'), **text_params) + A = plt.text(0, 1, '$A_3$', color='r', size=do_fontsize('A'), **text_params) + T = plt.text(1, 1, '$T_3$', color='k', size=do_fontsize('T'), **text_params) + G = plt.text(0, 0, '$G_3$', color='g', size=do_fontsize('G'), **text_params) + C = plt.text(1, 0, '$C_3$', color='b', size=do_fontsize('C'), **text_params) arrow_h_offset = 0.25 # data coordinates, empirically determined max_arrow_length = 1 - 2*arrow_h_offset @@ -119,7 +119,7 @@ def do_fontsize(k): max_head_length = 2*max_arrow_width arrow_params = {'length_includes_head': True, 'shape': shape, 'head_starts_at_zero': head_starts_at_zero} - ax = gca() + ax = plt.gca() sf = 0.6 # max arrow size represents this in data coords d = (r2/2 + arrow_h_offset - 0.5)/r2 # distance for diags @@ -179,7 +179,7 @@ def draw_arrow(pair, alpha=alpha, ec=ec, labelcolor=labelcolor): x_scale, y_scale = deltas[pair] x_pos, y_pos = positions[pair] - arrow(x_pos, y_pos, x_scale*length, y_scale*length, + plt.arrow(x_pos, y_pos, x_scale*length, y_scale*length, fc=fc, ec=ec, alpha=alpha, width=width, head_width=head_width, head_length=head_length, **arrow_params) @@ -192,24 +192,24 @@ def draw_arrow(pair, alpha=alpha, ec=ec, labelcolor=labelcolor): where = label_positions[pair] if where == 'left': - orig_position = 3*array([[max_arrow_width, max_arrow_width]]) + orig_position = 3*np.array([[max_arrow_width, max_arrow_width]]) elif where == 'absolute': - orig_position = array([[max_arrow_length/2.0, 3*max_arrow_width]]) + orig_position = np.array([[max_arrow_length/2.0, 3*max_arrow_width]]) elif where == 'right': - orig_position = array([[length - 3*max_arrow_width, + orig_position = np.array([[length - 3*max_arrow_width, 3*max_arrow_width]]) elif where == 'center': - orig_position = array([[length/2.0, 3*max_arrow_width]]) + orig_position = np.array([[length/2.0, 3*max_arrow_width]]) else: raise ValueError("Got unknown position parameter %s" % where) - M = array([[cx, sx], [-sx, cx]]) + M = np.array([[cx, sx], [-sx, cx]]) coords = dot(orig_position, M) + [[x_pos, y_pos]] - x, y = ravel(coords) + x, y = np.ravel(coords) orig_label = rate_labels[pair] label = '$%s_{_{\mathrm{%s}}}$' % (orig_label[0], orig_label[1:]) - text(x, y, label, size=label_text_size, ha='center', va='center', + plt.text(x, y, label, size=label_text_size, ha='center', va='center', color=labelcolor or fc) for p in positions.keys(): @@ -302,11 +302,11 @@ def draw_arrow(pair, alpha=alpha, ec=ec, labelcolor=labelcolor): display = 'length' size = 4 - figure(figsize=(size, size)) + plt.figure(figsize=(size, size)) make_arrow_plot(d, display=display, linewidth=0.001, edgecolor=None, normalize_data=scaled, head_starts_at_zero=True, size=size) - draw() + plt.draw() - show() + plt.show() From 1b72df1a390441fbe9301a75ca832485a3a2f83f Mon Sep 17 00:00:00 2001 From: domspad Date: Sat, 11 Jul 2015 15:37:34 -0500 Subject: [PATCH 09/24] pylab to plt and np --- examples/pylab_examples/axes_props.py | 43 ++++++++++++++------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/examples/pylab_examples/axes_props.py b/examples/pylab_examples/axes_props.py index 4ae17f380384..889e664f0155 100644 --- a/examples/pylab_examples/axes_props.py +++ b/examples/pylab_examples/axes_props.py @@ -3,30 +3,31 @@ You can control the axis tick and grid properties """ -from pylab import * +import matplotlib.pyplot as plt +import numpy as np -t = arange(0.0, 2.0, 0.01) -s = sin(2*pi*t) -plot(t, s) -grid(True) +t = np.arange(0.0, 2.0, 0.01) +s = np.sin(2*pi*t) +plt.plot(t, s) +plt.grid(True) # MATLAB style -xticklines = getp(gca(), 'xticklines') -yticklines = getp(gca(), 'yticklines') -xgridlines = getp(gca(), 'xgridlines') -ygridlines = getp(gca(), 'ygridlines') -xticklabels = getp(gca(), 'xticklabels') -yticklabels = getp(gca(), 'yticklabels') - -setp(xticklines, 'linewidth', 3) -setp(yticklines, 'linewidth', 3) -setp(xgridlines, 'linestyle', '-') -setp(ygridlines, 'linestyle', '-') -setp(yticklabels, 'color', 'r', fontsize='medium') -setp(xticklabels, 'color', 'r', fontsize='medium') - - -show() +xticklines = plt.getp(plt.gca(), 'xticklines') +yticklines = plt.getp(plt.gca(), 'yticklines') +xgridlines = plt.getp(plt.gca(), 'xgridlines') +ygridlines = plt.getp(plt.gca(), 'ygridlines') +xticklabels = plt.getp(plt.gca(), 'xticklabels') +yticklabels = plt.getp(plt.gca(), 'yticklabels') + +plt.setp(xticklines, 'linewidth', 3) +plt.setp(yticklines, 'linewidth', 3) +plt.setp(xgridlines, 'linestyle', '-') +plt.setp(ygridlines, 'linestyle', '-') +plt.setp(yticklabels, 'color', 'r', fontsize='medium') +plt.setp(xticklabels, 'color', 'r', fontsize='medium') + + +plt.show() """ From a0665f76c5d6e08cda87211a9c5e5e1e98d01648 Mon Sep 17 00:00:00 2001 From: domspad Date: Sat, 11 Jul 2015 15:42:14 -0500 Subject: [PATCH 10/24] pylab to plt and np --- examples/pylab_examples/axis_equal_demo.py | 43 +++++++++++----------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/examples/pylab_examples/axis_equal_demo.py b/examples/pylab_examples/axis_equal_demo.py index b05ff7fff9e5..70135fb66437 100644 --- a/examples/pylab_examples/axis_equal_demo.py +++ b/examples/pylab_examples/axis_equal_demo.py @@ -1,31 +1,32 @@ '''This example is only interesting when ran in interactive mode''' -from pylab import * +import matplotlib.pyplot as plt +import numpy as np # Plot circle or radius 3 -an = linspace(0, 2*pi, 100) +an = np.linspace(0, 2*np.pi, 100) -subplot(221) -plot(3*cos(an), 3*sin(an)) -title('not equal, looks like ellipse', fontsize=10) +plt.subplot(221) +plt.plot(3*np.cos(an), 3*np.sin(an)) +plt.title('not equal, looks like ellipse', fontsize=10) -subplot(222) -plot(3*cos(an), 3*sin(an)) -axis('equal') -title('equal, looks like circle', fontsize=10) +plt.subplot(222) +plt.plot(3*np.cos(an), 3*np.sin(an)) +plt.axis('equal') +plt.title('equal, looks like circle', fontsize=10) -subplot(223) -plot(3*cos(an), 3*sin(an)) -axis('equal') -axis([-3, 3, -3, 3]) -title('looks like circle, even after changing limits', fontsize=10) +plt.subplot(223) +plt.plot(3*np.cos(an), 3*np.sin(an)) +plt.axis('equal') +plt.axis([-3, 3, -3, 3]) +plt.title('looks like circle, even after changing limits', fontsize=10) -subplot(224) -plot(3*cos(an), 3*sin(an)) -axis('equal') -axis([-3, 3, -3, 3]) -plot([0, 4], [0, 4]) -title('still equal after adding line', fontsize=10) +plt.subplot(224) +plt.plot(3*np.cos(an), 3*np.sin(an)) +plt.axis('equal') +plt.axis([-3, 3, -3, 3]) +plt.plot([0, 4], [0, 4]) +plt.title('still equal after adding line', fontsize=10) -show() +plt.show() From 8f2b84cafc2ad1d47adde4e2fb5e4936b4e868d7 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Thu, 16 Jul 2015 23:33:10 -0400 Subject: [PATCH 11/24] DOC: slightly update demo Gets a frame rate of ~75fps on my machine --- examples/pylab_examples/system_monitor.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/examples/pylab_examples/system_monitor.py b/examples/pylab_examples/system_monitor.py index 90e5aa8de02e..a661570fc086 100644 --- a/examples/pylab_examples/system_monitor.py +++ b/examples/pylab_examples/system_monitor.py @@ -40,12 +40,15 @@ def get_stats(): ax.set_ylabel('Percent usage') ax.set_title('System Monitor') +start = time.time() for i in range(200): # run for a little while m, c, n = get_stats() pm.set_height(m) pc.set_height(c) pn.set_height(n) - ax.set_ylim([0, 100]) - plt.draw() + fig.canvas.flush_events() + +stop = time.time() +print("{fps:.1f} frames per second".format(fps=200 / (stop - start))) From 887e20d0f3a781d8f45245a407dabcf627e9883d Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Fri, 17 Jul 2015 00:25:41 -0400 Subject: [PATCH 12/24] DOC: whats_new for axes.labelpad closes #2907 --- doc/users/whats_new/rcparams.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/doc/users/whats_new/rcparams.rst b/doc/users/whats_new/rcparams.rst index 0d78a18108fb..3e0db2815628 100644 --- a/doc/users/whats_new/rcparams.rst +++ b/doc/users/whats_new/rcparams.rst @@ -46,3 +46,9 @@ Added "toolmanager" to "toolbar" possible values ```````````````````````````````````````````````` The new value enables the use of ``ToolManager`` + + +Added ``axes.labelpad`` +``````````````````````` + +This new value controls the space between the axis and the label From 188d7a1727dadb3212f4d86bc95e268b80ac6b5c Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Fri, 17 Jul 2015 01:21:36 -0400 Subject: [PATCH 13/24] DOC: protect against non-gui backends in demo --- examples/pylab_examples/system_monitor.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/examples/pylab_examples/system_monitor.py b/examples/pylab_examples/system_monitor.py index a661570fc086..07110190a2c4 100644 --- a/examples/pylab_examples/system_monitor.py +++ b/examples/pylab_examples/system_monitor.py @@ -47,8 +47,10 @@ def get_stats(): pm.set_height(m) pc.set_height(c) pn.set_height(n) - - fig.canvas.flush_events() + try: + fig.canvas.flush_events() + except NotImplementedError: + pass stop = time.time() print("{fps:.1f} frames per second".format(fps=200 / (stop - start))) From b328cbb1ddca72301d78abd6afffc242242f12b5 Mon Sep 17 00:00:00 2001 From: domspad Date: Thu, 16 Jul 2015 23:13:25 -0700 Subject: [PATCH 14/24] pep8 --- examples/pylab_examples/axis_equal_demo.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/pylab_examples/axis_equal_demo.py b/examples/pylab_examples/axis_equal_demo.py index 70135fb66437..998720dac1de 100644 --- a/examples/pylab_examples/axis_equal_demo.py +++ b/examples/pylab_examples/axis_equal_demo.py @@ -1,7 +1,7 @@ '''This example is only interesting when ran in interactive mode''' -import matplotlib.pyplot as plt -import numpy as np +import matplotlib.pyplot as plt +import numpy as np # Plot circle or radius 3 From 5a5f458aaf850357a00590621aae85fc3d63bd0b Mon Sep 17 00:00:00 2001 From: domspad Date: Thu, 16 Jul 2015 23:15:50 -0700 Subject: [PATCH 15/24] pep8 --- examples/pylab_examples/axes_props.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/pylab_examples/axes_props.py b/examples/pylab_examples/axes_props.py index 889e664f0155..2697fca37c52 100644 --- a/examples/pylab_examples/axes_props.py +++ b/examples/pylab_examples/axes_props.py @@ -4,10 +4,10 @@ """ import matplotlib.pyplot as plt -import numpy as np +import numpy as np t = np.arange(0.0, 2.0, 0.01) -s = np.sin(2*pi*t) +s = np.sin(2*np.pi*t) plt.plot(t, s) plt.grid(True) From b26a52194e3a94ca4d2dbf08d7dfc90796b20e70 Mon Sep 17 00:00:00 2001 From: domspad Date: Thu, 16 Jul 2015 23:23:10 -0700 Subject: [PATCH 16/24] pep8 --- examples/pylab_examples/arrow_demo.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/examples/pylab_examples/arrow_demo.py b/examples/pylab_examples/arrow_demo.py index 23f01f1b9581..a2398d8d3019 100644 --- a/examples/pylab_examples/arrow_demo.py +++ b/examples/pylab_examples/arrow_demo.py @@ -9,10 +9,12 @@ """ -from pylab import * +import matplotlib.pyplot as plt +import numpy as np -rates_to_bases = {'r1': 'AT', 'r2': 'TA', 'r3': 'GA', 'r4': 'AG', 'r5': 'CA', 'r6': 'AC', - 'r7': 'GT', 'r8': 'TG', 'r9': 'CT', 'r10': 'TC', 'r11': 'GC', 'r12': 'CG'} +rates_to_bases = {'r1': 'AT', 'r2': 'TA', 'r3': 'GA', 'r4': 'AG', 'r5': 'CA', + 'r6': 'AC', 'r7': 'GT', 'r8': 'TG', 'r9': 'CT', 'r10': 'TC', + 'r11': 'GC', 'r12': 'CG'} numbered_bases_to_rates = dict([(v, k) for k, v in rates_to_bases.items()]) lettered_bases_to_rates = dict([(v, 'r' + v) for k, v in rates_to_bases.items()]) @@ -103,7 +105,7 @@ def make_arrow_plot(data, size=4, display='length', shape='right', } def do_fontsize(k): - return float(clip(max_text_size*np.sqrt(data[k]), + return float(np.clip(max_text_size*np.sqrt(data[k]), min_text_size, max_text_size)) A = plt.text(0, 1, '$A_3$', color='r', size=do_fontsize('A'), **text_params) @@ -204,7 +206,7 @@ def draw_arrow(pair, alpha=alpha, ec=ec, labelcolor=labelcolor): raise ValueError("Got unknown position parameter %s" % where) M = np.array([[cx, sx], [-sx, cx]]) - coords = dot(orig_position, M) + [[x_pos, y_pos]] + coords = np.dot(orig_position, M) + [[x_pos, y_pos]] x, y = np.ravel(coords) orig_label = rate_labels[pair] label = '$%s_{_{\mathrm{%s}}}$' % (orig_label[0], orig_label[1:]) From 37206713b17d8dc1e541c8c0008d3c583f07d6df Mon Sep 17 00:00:00 2001 From: domspad Date: Thu, 16 Jul 2015 23:27:17 -0700 Subject: [PATCH 17/24] pep8 --- examples/pylab_examples/anscombe.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/pylab_examples/anscombe.py b/examples/pylab_examples/anscombe.py index 9e6b25e9eb0c..3392bc55fd5e 100755 --- a/examples/pylab_examples/anscombe.py +++ b/examples/pylab_examples/anscombe.py @@ -9,7 +9,7 @@ matplotlib fun for a rainy day """ -import matplotlib.pyplot as plt +import matplotlib.pyplot as plt import numpy as np x = np.array([10, 8, 13, 9, 11, 14, 6, 4, 12, 7, 5]) From a7fa5d953ed405209d0ebb3c0c4ca9754491594e Mon Sep 17 00:00:00 2001 From: domspad Date: Thu, 16 Jul 2015 23:29:54 -0700 Subject: [PATCH 18/24] pep8 --- examples/pylab_examples/annotation_demo.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/pylab_examples/annotation_demo.py b/examples/pylab_examples/annotation_demo.py index d76098287fc4..5c3634aa4826 100644 --- a/examples/pylab_examples/annotation_demo.py +++ b/examples/pylab_examples/annotation_demo.py @@ -120,7 +120,7 @@ el = Ellipse((0, 0), 10, 20, facecolor='r', alpha=0.5) - fig = figure() + fig = plt.figure() ax = fig.add_subplot(111, aspect='equal') ax.add_artist(el) el.set_clip_box(ax.bbox) From 38215ef33b47a19ff0994a75a98fad449a0677f2 Mon Sep 17 00:00:00 2001 From: domspad Date: Thu, 16 Jul 2015 23:31:27 -0700 Subject: [PATCH 19/24] pep8 --- examples/pylab_examples/annotation_demo2.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/pylab_examples/annotation_demo2.py b/examples/pylab_examples/annotation_demo2.py index 0825359cac5a..0c2b357405fe 100644 --- a/examples/pylab_examples/annotation_demo2.py +++ b/examples/pylab_examples/annotation_demo2.py @@ -98,7 +98,7 @@ ax.annotate('fancy', xy=(2., -1), xycoords='data', xytext=(-100, 60), textcoords='offset points', size=20, - #bbox=dict(boxstyle="round", fc="0.8"), + # bbox=dict(boxstyle="round", fc="0.8"), arrowprops=dict(arrowstyle="fancy", fc="0.6", ec="none", patchB=el, @@ -108,7 +108,7 @@ ax.annotate('simple', xy=(2., -1), xycoords='data', xytext=(100, 60), textcoords='offset points', size=20, - #bbox=dict(boxstyle="round", fc="0.8"), + # bbox=dict(boxstyle="round", fc="0.8"), arrowprops=dict(arrowstyle="simple", fc="0.6", ec="none", patchB=el, @@ -118,7 +118,7 @@ ax.annotate('wedge', xy=(2., -1), xycoords='data', xytext=(-100, -100), textcoords='offset points', size=20, - #bbox=dict(boxstyle="round", fc="0.8"), + # bbox=dict(boxstyle="round", fc="0.8"), arrowprops=dict(arrowstyle="wedge,tail_width=0.7", fc="0.6", ec="none", patchB=el, From 0f726dcb4200316db7c4f3fe2ddc01d8cb9c7ae5 Mon Sep 17 00:00:00 2001 From: Michiel de Hoon Date: Fri, 17 Jul 2015 18:45:58 +0900 Subject: [PATCH 20/24] Implement draw_idle --- lib/matplotlib/backends/backend_macosx.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/matplotlib/backends/backend_macosx.py b/lib/matplotlib/backends/backend_macosx.py index b848533bed28..a12079618b73 100755 --- a/lib/matplotlib/backends/backend_macosx.py +++ b/lib/matplotlib/backends/backend_macosx.py @@ -309,6 +309,9 @@ def __init__(self, figure): self.renderer = RendererMac(figure.dpi, width, height) _macosx.FigureCanvas.__init__(self, width, height) + def draw_idle(self, *args, **kwargs): + self.invalidate() + def resize(self, width, height): self.renderer.set_width_height(width, height) dpi = self.figure.dpi From 708468bb4f37ed9a0c51e61d45608333778b6088 Mon Sep 17 00:00:00 2001 From: David Reed Date: Sun, 19 Jan 2014 12:22:42 -0500 Subject: [PATCH 21/24] NF - added new feature to colors called shade_color to lighten or darken a given color by a given percentage --- lib/matplotlib/colors.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 29b4e9bb3bcc..dfc44d56f800 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -57,6 +57,7 @@ import numpy as np from numpy import ma import matplotlib.cbook as cbook +from colorsys import rgb_to_hls as rgb2hls, hls_to_rgb as hls2rgb cnames = { 'aliceblue': '#F0F8FF', @@ -1908,3 +1909,38 @@ def from_levels_and_colors(levels, colors, extend='neither'): norm = BoundaryNorm(levels, ncolors=n_data_colors) return cmap, norm + +def shade_color(color, percent): + """Shade Color + + This color utility function allows the user to easily darken or lighten a color for + plotting purposes. + + Parameters + ---------- + color : string, list, hexvalue + Any acceptable Matplotlib color value, such as 'red', 'slategrey', '#FFEE11', (1,0,0) + + percent : the amount by which to brighten or darken the color. + + Returns + ------- + color : tuple of floats + tuple representing converted rgb values + + """ + + cc = CC() + + rgb = cc.to_rgb(color) + + h,l,s = rgb2hls(*rgb) + + l *= 1 + float(percent)/100 + + l = min(1, l) + l = max(0, l) + + r,g,b = hls2rgb(h,l,s) + + return r,g,b \ No newline at end of file From 1c70b15f0b757acec3be2b5b9cd989ace8412119 Mon Sep 17 00:00:00 2001 From: David Reed Date: Sun, 19 Jan 2014 12:33:27 -0500 Subject: [PATCH 22/24] NF - added new feature to colors called shade_color to lighten or darken a given color by a given percentage (altered CC to ColorConverter) --- lib/matplotlib/colors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index dfc44d56f800..5876516bffbc 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -1930,7 +1930,7 @@ def shade_color(color, percent): """ - cc = CC() + cc = ColorConverter() rgb = cc.to_rgb(color) From badd1f88af022c4d3d8a7656932462b8cace06f1 Mon Sep 17 00:00:00 2001 From: David Reed Date: Sat, 16 May 2015 12:11:35 -0400 Subject: [PATCH 23/24] NF - added new feature to colors called shade_color to lighten or darken a given color by a given percentage, added modifications noted in PR #2745 which included fixing PEP8 errors and adding a test. --- lib/matplotlib/colors.py | 25 ++++++++++++++----------- lib/matplotlib/tests/test_colors.py | 27 ++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 5876516bffbc..ae265e0ce741 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -54,10 +54,12 @@ import warnings import re +from colorsys import rgb_to_hls, hls_to_rgb + import numpy as np from numpy import ma import matplotlib.cbook as cbook -from colorsys import rgb_to_hls as rgb2hls, hls_to_rgb as hls2rgb + cnames = { 'aliceblue': '#F0F8FF', @@ -1910,11 +1912,15 @@ def from_levels_and_colors(levels, colors, extend='neither'): norm = BoundaryNorm(levels, ncolors=n_data_colors) return cmap, norm + def shade_color(color, percent): - """Shade Color + """ + A color helper utility to either darken or lighten given color. This color utility function allows the user to easily darken or lighten a color for - plotting purposes. + plotting purposes. This function first converts the given color to RGB using + ColorConverter and then to HSL. The saturation is modified according to the given + percentage and converted back to RGB. Parameters ---------- @@ -1930,17 +1936,14 @@ def shade_color(color, percent): """ - cc = ColorConverter() - - rgb = cc.to_rgb(color) + rgb = colorConverter.to_rgb(color) - h,l,s = rgb2hls(*rgb) + h, l, s = rgb_to_hls(*rgb) l *= 1 + float(percent)/100 - l = min(1, l) - l = max(0, l) + l = np.clip(l, 0, 1) - r,g,b = hls2rgb(h,l,s) + r, g, b = hls_to_rgb(h, l, s) - return r,g,b \ No newline at end of file + return r, g, b diff --git a/lib/matplotlib/tests/test_colors.py b/lib/matplotlib/tests/test_colors.py index 0ac08a7db8c5..8a4e65d3e749 100644 --- a/lib/matplotlib/tests/test_colors.py +++ b/lib/matplotlib/tests/test_colors.py @@ -8,7 +8,7 @@ from nose.tools import assert_raises, assert_equal import numpy as np -from numpy.testing.utils import assert_array_equal, assert_array_almost_equal +from numpy.testing.utils import assert_array_equal, assert_array_almost_equal, assert_equal import matplotlib.colors as mcolors import matplotlib.cm as cm @@ -471,6 +471,31 @@ def _azimuth2math(azimuth, elevation): return theta, phi +def _shade_test_helper(color, shade, expected): + sc = mcolors.shade_color(color, shade) + assert_equal(sc, expected) + + +def test_color_shading(): + test_colors = ( + 'white', + 'red', + 'black', + [0, .5, .9], + 'slategrey', + ) + test_shade = (0, .5, 1, -.5, -1) + known_shaded_result = ( + (1.0, 1.0, 1.0), + (1.0, 0.0049999999999998934, 0.0049999999999998934), + (0.0, 0.0, 0.0), + (0.0, 0.49749999999999983, 0.89549999999999996), + (0.43433441408059281, 0.49694117647058816, 0.55954793886058363) + ) + for color, shade, expected in zip(test_colors, test_shade, known_shaded_result): + _shade_test_helper(color, shade, expected) + + if __name__ == '__main__': import nose nose.runmodule(argv=['-s', '--with-doctest'], exit=False) From 49b9b257da981702589078db2dd405bd5720730f Mon Sep 17 00:00:00 2001 From: David Reed Date: Sat, 16 May 2015 12:40:50 -0400 Subject: [PATCH 24/24] NF - added new feature to colors called shade_color to lighten or darken a given color by a given percentage, added modifications noted in PR #2745 which included fixing PEP8 errors and adding a test. --- doc/users/whats_new.rst | 10 ++++++++++ examples/color/color_shade_demo.py | 21 +++++++++++++++++++++ lib/matplotlib/tests/test_colors.py | 16 ++++++++++------ 3 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 examples/color/color_shade_demo.py diff --git a/doc/users/whats_new.rst b/doc/users/whats_new.rst index a597628f62a6..4072558faa22 100644 --- a/doc/users/whats_new.rst +++ b/doc/users/whats_new.rst @@ -567,6 +567,16 @@ Testing New plotting features --------------------- +Easy color shading +`````````````````` +Often we would like the ability add subtle color variation to our plots, +especially when similar element are plotted in close proximity to one another. +:func:`matplotlib.colors.shade_color` can be used to take an existing color and +slightly darken it, by giving a negative percentage, or to slightly lighten it, +by giving a positive percentage. + +.. plot:: mpl_examples/color/color_shade_demo.py + `xkcd`-style sketch plotting ```````````````````````````` To give your plots a sense of authority that they may be missing, diff --git a/examples/color/color_shade_demo.py b/examples/color/color_shade_demo.py new file mode 100644 index 000000000000..39b278d4f325 --- /dev/null +++ b/examples/color/color_shade_demo.py @@ -0,0 +1,21 @@ +""" +Demo of shade_color utility. + +""" +import matplotlib.pyplot as plt +import matplotlib.colors as mcolors + +fig = plt.figure() +ax = fig.add_subplot(111) + +lightened_color = mcolors.shade_color('blue', -50) +darkened_color = mcolors.shade_color('blue', +50) + +ax.fill_between([0,1], [0,0], [1,1], facecolor=darkened_color) +ax.fill_between([0,1], [0,0], [.66, .66], facecolor='blue') +ax.fill_between([0,1], [0,0], [.33, .33], facecolor=lightened_color) + +plt.xlim([0, 1]) +plt.ylim([0, 1]) + +plt.show() \ No newline at end of file diff --git a/lib/matplotlib/tests/test_colors.py b/lib/matplotlib/tests/test_colors.py index 8a4e65d3e749..db3eb4e67247 100644 --- a/lib/matplotlib/tests/test_colors.py +++ b/lib/matplotlib/tests/test_colors.py @@ -478,19 +478,23 @@ def _shade_test_helper(color, shade, expected): def test_color_shading(): test_colors = ( - 'white', 'red', - 'black', + 'red', + 'red', + 'red', + 'red', [0, .5, .9], 'slategrey', ) - test_shade = (0, .5, 1, -.5, -1) + test_shade = (0, 50, 100, -50, -100, 20, -20) known_shaded_result = ( + (1.0, 0.0, 0.0), + (1.0, 0.5, 0.5), (1.0, 1.0, 1.0), - (1.0, 0.0049999999999998934, 0.0049999999999998934), + (0.5, 0.0, 0.0), (0.0, 0.0, 0.0), - (0.0, 0.49749999999999983, 0.89549999999999996), - (0.43433441408059281, 0.49694117647058816, 0.55954793886058363) + (0.080000000000000071, 0.59111111111111092, 1.0), + (0.35097730430754981, 0.40156862745098038, 0.45215995059441105) ) for color, shade, expected in zip(test_colors, test_shade, known_shaded_result): _shade_test_helper(color, shade, expected)