@@ -5599,7 +5599,7 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
5599
5599
return im
5600
5600
5601
5601
@staticmethod
5602
- def _pcolorargs (funcname , * args , allmatch = False ):
5602
+ def _pcolorargs (funcname , * args , allmatch = False , dropdata = True ):
5603
5603
# If allmatch is True, then the incoming X, Y, C must have matching
5604
5604
# dimensions, taking into account that X and Y can be 1-D rather than
5605
5605
# 2-D. This perfect match is required for Gouraud shading. For flat
@@ -5661,14 +5661,34 @@ def _pcolorargs(funcname, *args, allmatch=False):
5661
5661
raise TypeError ('Dimensions of C %s are incompatible with'
5662
5662
' X (%d) and/or Y (%d); see help(%s)' % (
5663
5663
C .shape , Nx , Ny , funcname ))
5664
- C = C [:Ny - 1 , :Nx - 1 ]
5
8000
664
+
5665
+ if dropdata :
5666
+ C = C [:Ny - 1 , :Nx - 1 ]
5667
+ else :
5668
+ def _interp_grid (X ):
5669
+ # helper for below
5670
+ dX = np .diff (X , axis = 1 )/ 2.
5671
+ X = np .hstack ((X [:, [0 ]] - dX [:, [0 ]],
5672
+ X [:, :- 1 ] + dX ,
5673
+ X [:, [- 1 ]] + dX [:, [- 1 ]])
5674
+ )
5675
+ return X
5676
+
5677
+ if ncols == Nx :
5678
+ X = _interp_grid (X )
5679
+ Y = _interp_grid (Y )
5680
+
5681
+ if nrows == Ny :
5682
+ X = _interp_grid (X .T ).T
5683
+ Y = _interp_grid (Y .T ).T
5684
+
5665
5685
C = cbook .safe_masked_invalid (C )
5666
5686
return X , Y , C
5667
5687
5668
5688
@_preprocess_data ()
5669
5689
@docstring .dedent_interpd
5670
5690
def pcolor (self , * args , alpha = None , norm = None , cmap = None , vmin = None ,
5671
- vmax = None , ** kwargs ):
5691
+ vmax = None , dropdata = True , ** kwargs ):
5672
5692
r"""
5673
5693
Create a pseudocolor plot with a non-regular rectangular grid.
5674
5694
@@ -5682,7 +5702,9 @@ def pcolor(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
5682
5702
5683
5703
``pcolor()`` can be very slow for large arrays. In most
5684
5704
cases you should use the similar but much faster
5685
- `~.Axes.pcolormesh` instead. See there for a discussion of the
5705
+ `~.Axes.pcolormesh` instead. See
5706
+ :ref:`Differences between pcolor() and pcolormesh()
5707
+ <differences-pcolor-pcolormesh>` for a discussion of the
5686
5708
differences.
5687
5709
5688
5710
Parameters
@@ -5707,7 +5729,8 @@ def pcolor(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
5707
5729
5708
5730
The dimensions of *X* and *Y* should be one greater than those of
5709
5731
*C*. Alternatively, *X*, *Y* and *C* may have equal dimensions, in
5710
- which case the last row and column of *C* will be ignored.
5732
+ which case the last row and column of *C* will be ignored if
5733
+ *dropdata* is True (see below).
5711
5734
5712
5735
If *X* and/or *Y* are 1-D arrays or column vectors they will be
5713
5736
expanded as needed into the appropriate 2-D arrays, making a
@@ -5747,6 +5770,13 @@ def pcolor(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
5747
5770
snap : bool, default: False
5748
5771
Whether to snap the mesh to pixel boundaries.
5749
5772
5773
+ dropdata : bool, default: True
5774
+ If True (default), and *X* and *Y* are the same size as C in their
5775
+ respective dimensions, drop the last element of C in both
5776
+ dimensions. If False, *X* and *Y* are assumed to the the midpoints
5777
+ of the quadrilaterals, and their edgese are calculated by linear
5778
+ interpolation.
5779
+
5750
5780
Returns
5751
5781
-------
5752
5782
collection : `matplotlib.collections.Collection`
@@ -5797,12 +5827,16 @@ def pcolor(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
5797
5827
``pcolor()`` displays all columns of *C* if *X* and *Y* are not
5798
5828
specified, or if *X* and *Y* have one more column than *C*.
5799
5829
If *X* and *Y* have the same number of columns as *C* then the last
5800
- column of *C* is dropped. Similarly for the rows.
5830
+ column of *C* is dropped if *dropdata* is True. Similarly for the rows.
5831
+ If *dropdata* is false, then *X* and *Y* are assumed to be at the
5832
+ middle of the quadrilaterals, and the edges of the quadrilaterals are
5833
+ linearly interpolated.
5801
5834
5802
- Note: This behavior is different from MATLAB's ``pcolor()``, which
5835
+ This behavior is different from MATLAB's ``pcolor()``, which
5803
5836
always discards the last row and column of *C*.
5804
5837
"""
5805
- X , Y , C = self ._pcolorargs ('pcolor' , * args , allmatch = False )
5838
+ X , Y , C = self ._pcolorargs ('pcolor' , * args , dropdata = dropdata ,
5839
+ allmatch = False )
5806
5840
Ny , Nx = X .shape
5807
5841
5808
5842
# unit conversion allows e.g. datetime objects as axis values
@@ -5899,7 +5933,8 @@ def pcolor(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
5899
5933
@_preprocess_data ()
5900
5934
@docstring .dedent_interpd
5901
5935
def pcolormesh (self , * args , alpha = None , norm = None , cmap = None , vmin = None ,
5902
- vmax = None , shading = 'flat' , antialiased = False , ** kwargs ):
5936
+ vmax = None , shading = 'flat' , antialiased = False ,
5937
+ dropdata = True , ** kwargs ):
5903
5938
"""
5904
5939
Create a pseudocolor plot with a non-regular rectangular grid.
5905
5940
@@ -5909,9 +5944,9 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
5909
5944
5910
5945
*X* and *Y* can be used to specify the corners of the quadrilaterals.
5911
5946
5912
- .. note ::
5947
+ .. hint ::
5913
5948
5914
- `~Axes.pcolormesh` is similar to `~Axes.pcolor`. It's much faster
5949
+ `~Axes.pcolormesh` is similar to `~Axes.pcolor`. It is much faster
5915
5950
and preferred in most cases. For a detailed discussion on the
5916
5951
differences see :ref:`Differences between pcolor() and pcolormesh()
5917
5952
<differences-pcolor-pcolormesh>`.
@@ -5938,7 +5973,8 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
5938
5973
5939
5974
The dimensions of *X* and *Y* should be one greater than those of
5940
5975
*C*. Alternatively, *X*, *Y* and *C* may have equal dimensions, in
5941
- which case the last row and column of *C* will be ignored.
5976
+ which case the last row and column of *C* will be ignored, unless
5977
+ *dropdata* is *False* (see below).
5942
5978
5943
5979
If *X* and/or *Y* are 1-D arrays or column vectors they will be
5944
5980
expanded as needed into the appropriate 2-D arrays, making a
@@ -5987,6 +6023,13 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
5987
6023
snap : bool, default: False
5988
6024
Whether to snap the mesh to pixel boundaries.
5989
6025
6026
+ dropdata : bool, default: True
6027
+ If True (default), and *X* and *Y* are the same size as C in their
6028
+ respective dimensions, drop the last element of C in both
6029
+ dimensions. If False, *X* and *Y* are assumed to the the midpoints
6030
+ of the quadrilaterals, and their edgese are calculated by linear
6031
+ interpolation.
6032
+
5990
6033
Returns
5991
6034
-------
5992
6035
mesh : `matplotlib.collections.QuadMesh`
@@ -6058,7 +6101,8 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
6058
6101
6059
6102
allmatch = (shading == 'gouraud' )
6060
6103
6061
- X , Y , C = self ._pcolorargs ('pcolormesh' , * args , allmatch = allmatch )
6104
+ X , Y , C = self ._pcolorargs ('pcolormesh' , * args ,
6105
+ allmatch = allmatch , dropdata = dropdata )
6062
6106
Ny , Nx = X .shape
6063
6107
X = X .ravel ()
6064
6108
Y = Y .ravel ()
0 commit comments