@@ -864,7 +864,32 @@ def __init__(self, ax, *args, **kwargs):
864864 ncolors = len (self .levels )
865865 if self .filled :
866866 ncolors -= 1
867- cmap = colors .ListedColormap (self .colors , N = ncolors )
867+ i0 = 0
868+
869+ # Handle the case where colors are given for the extended
870+ # parts of the contour.
871+ extend_min = self .extend in ['min' , 'both' ]
872+ extend_max = self .extend in ['max' , 'both' ]
873+ use_set_under_over = False
874+ # if we are extending the lower end, and we've been given enough
875+ # colors then skip the first color in the resulting cmap. For the
876+ # extend_max case we don't need to worry about passing more colors
877+ # than ncolors as ListedColormap will clip.
878+ total_levels = ncolors + int (extend_min ) + int (extend_max )
879+ if (len (self .colors ) == total_levels and
880+ any ([extend_min , extend_max ])):
881+ use_set_under_over = True
882+ if extend_min :
883+ i0 = 1
884+
885+ cmap = colors .ListedColormap (self .colors [i0 :None ], N = ncolors )
886+
887+ if use_set_under_over :
888+ if extend_min :
889+ cmap .set_under (self .colors [0 ])
890+ if extend_max :
891+ cmap .set_over (self .colors [- 1 ])
892+
868893 if self .filled :
869894 self .collections = cbook .silent_list ('mcoll.PathCollection' )
870895 else :
@@ -1172,16 +1197,16 @@ def _process_levels(self):
11721197 # (Colorbar needs this even for line contours.)
11731198 self ._levels = list (self .levels )
11741199
1175- if not self .filled :
1176- self .layers = self .levels
1177- return
1178-
11791200 if self .extend in ('both' , 'min' ):
11801201 self ._levels .insert (0 , min (self .levels [0 ], self .zmin ) - 1 )
11811202 if self .extend in ('both' , 'max' ):
11821203 self ._levels .append (max (self .levels [- 1 ], self .zmax ) + 1 )
11831204 self ._levels = np .asarray (self ._levels )
11841205
1206+ if not self .filled :
1207+ self .layers = self .levels
1208+ return
1209+
11851210 # layer values are mid-way between levels
11861211 self .layers = 0.5 * (self ._levels [:- 1 ] + self ._levels [1 :])
11871212 # ...except that extended layers must be outside the
@@ -1526,9 +1551,7 @@ def _check_xyz(self, args, kwargs):
15261551 if y .shape != z .shape :
15271552 raise TypeError ("Shape of y does not match that of z: found "
15281553 "{0} instead of {1}." .format (y .shape , z .shape ))
1529-
15301554 else :
1531-
15321555 raise TypeError ("Inputs x and y must be 1D or 2D." )
15331556
15341557 return x , y , z
0 commit comments