@@ -55,6 +55,41 @@ def test_colormap_vcenter_rejects_norm_instance(via_norm_kw):
5555 ax .pcolor (np .array ([[0.0 , 1.0 ], [2.0 , 7.0 ]]), norm = Normalize (), ** kwargs )
5656
5757
58+ @pytest .mark .parametrize ("method" , ["contourf" , "pcolormesh" ])
59+ @pytest .mark .parametrize ("cmap_name" , ["RdBu" , "fire" ])
60+ @pytest .mark .parametrize ("extend" , ["neither" , "min" , "max" , "both" ])
61+ @pytest .mark .parametrize (
62+ ("under" , "over" ),
63+ [(None , None ), ("black" , None ), (None , "green" ), ("black" , "green" )],
64+ )
65+ def test_colormap_extreme_colors_reserve_correct_end (
66+ method , cmap_name , extend , under , over
67+ ):
68+ """Custom extremes free their own endpoint without consuming the opposite one."""
69+ cmap = uplt .Colormap (cmap_name ).with_extremes (under = under , over = over )
70+ samples = np .linspace (0 , 1 , 256 )
71+ original_colors = cmap (samples ).copy ()
72+ levels = np .linspace (- 1 , 1 , 7 )
73+ centers = 0.5 * (levels [:- 1 ] + levels [1 :])
74+ data = np .tile (np .linspace (- 1.5 , 1.5 , 9 ), (9 , 1 ))
75+ _ , ax = uplt .subplots ()
76+ mappable = getattr (ax , method )(data , cmap = cmap , levels = levels , extend = extend )
77+
78+ # An automatic extension must remain distinct from the adjacent normal bin.
79+ # A custom extension lets that normal bin use the full colormap endpoint.
80+ for index , custom , extensions , endpoint in (
81+ (0 , under , ("min" , "both" ), 0.0 ),
82+ (- 1 , over , ("max" , "both" ), 1.0 ),
83+ ):
84+ uses_endpoint = np .allclose (mappable .to_rgba (centers [index ]), cmap (endpoint ))
85+ reserved = custom is None and extend in extensions
86+ assert uses_endpoint == (not reserved )
87+
88+ np .testing .assert_allclose (mappable .to_rgba (- 2.0 ), cmap .get_under ())
89+ np .testing .assert_allclose (mappable .to_rgba (2.0 ), cmap .get_over ())
90+ np .testing .assert_array_equal (mappable .cmap (samples ), original_colors )
91+
92+
5893@pytest .mark .mpl_image_compare
5994def test_auto_diverging1 (rng ):
6095 """
0 commit comments