@@ -2068,12 +2068,32 @@ def add_contour_set(
20682068 art3d .line_collection_2d_to_3d (linec , z , zdir = zdir )
20692069
20702070 def add_contourf_set (self , cset , zdir = 'z' , offset = None ):
2071+ self ._add_contourf_set (cset , zdir = zdir , offset = offset )
2072+
2073+ def _add_contourf_set (self , cset , zdir = 'z' , offset = None ):
2074+ """
2075+ Returns
2076+ -------
2077+ levels : numpy.ndarray
2078+ Levels at which the filled contours are added.
2079+ """
20712080 zdir = '-' + zdir
2072- for z , linec in zip (cset .levels , cset .collections ):
2081+
2082+ midpoints = cset .levels [:- 1 ] + np .diff (cset .levels ) / 2
2083+ # Linearly interpolate to get levels for any extensions
2084+ if cset ._extend_min :
2085+ min_level = cset .levels [0 ] - np .diff (cset .levels [:2 ]) / 2
2086+ midpoints = np .insert (midpoints , 0 , min_level )
2087+ if cset ._extend_max :
2088+ max_level = cset .levels [- 1 ] + np .diff (cset .levels [- 2 :]) / 2
2089+ midpoints = np .append (midpoints , max_level )
2090+
2091+ for z , linec in zip (midpoints , cset .collections ):
20732092 if offset is not None :
20742093 z = offset
20752094 art3d .poly_collection_2d_to_3d (linec , z , zdir = zdir )
20762095 linec .set_sort_zpos (z )
2096+ return midpoints
20772097
20782098 @_preprocess_data ()
20792099 def contour (self , X , Y , Z , * args ,
@@ -2168,6 +2188,16 @@ def tricontour(self, *args,
21682188 self .auto_scale_xyz (X , Y , Z , had_data )
21692189 return cset
21702190
2191+ def _auto_scale_contourf (self , X , Y , Z , zdir , levels , had_data ):
2192+ # Autoscale in the zdir based on the levels added, which are
2193+ # different from data range if any contour extensions are present
2194+ dim_vals = {'x' : X , 'y' : Y , 'z' : Z , zdir : levels }
2195+ # Input data and levels have different sizes, but auto_scale_xyz
2196+ # expected same-size input, so manually take min/max limits
2197+ limits = [(np .nanmin (dim_vals [dim ]), np .nanmax (dim_vals [dim ]))
2198+ for dim in ['x' , 'y' , 'z' ]]
2199+ self .auto_scale_xyz (* limits , had_data )
2200+
21712201 @_preprocess_data ()
21722202 def contourf (self , X , Y , Z , * args , zdir = 'z' , offset = None , ** kwargs ):
21732203 """
@@ -2195,9 +2225,9 @@ def contourf(self, X, Y, Z, *args, zdir='z', offset=None, **kwargs):
21952225
21962226 jX , jY , jZ = art3d .rotate_axes (X , Y , Z , zdir )
21972227 cset = super ().contourf (jX , jY , jZ , * args , ** kwargs )
2198- self .add_contourf_set (cset , zdir , offset )
2228+ levels = self ._add_contourf_set (cset , zdir , offset )
21992229
2200- self .auto_scale_xyz (X , Y , Z , had_data )
2230+ self ._auto_scale_contourf (X , Y , Z , zdir , levels , had_data )
22012231 return cset
22022232
22032233 contourf3D = contourf
@@ -2246,9 +2276,9 @@ def tricontourf(self, *args, zdir='z', offset=None, **kwargs):
22462276 tri = Triangulation (jX , jY , tri .triangles , tri .mask )
22472277
22482278 cset = super ().tricontourf (tri , jZ , * args , ** kwargs )
2249- self .add_contourf_set (cset , zdir , offset )
2279+ levels = self ._add_contourf_set (cset , zdir , offset )
22502280
2251- self .auto_scale_xyz (X , Y , Z , had_data )
2281+ self ._auto_scale_contourf (X , Y , Z , zdir , levels , had_data )
22522282 return cset
22532283
22542284 def add_collection3d (self , col , zs = 0 , zdir = 'z' ):
0 commit comments