@@ -5603,7 +5603,7 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
5603
5603
return im
5604
5604
5605
5605
@staticmethod
5606
- def _pcolorargs (funcname , * args , allmatch = False ):
5606
+ def _pcolorargs (funcname , * args , allmatch = False , dropdata = True ):
5607
5607
# If allmatch is True, then the incoming X, Y, C must have matching
5608
5608
# dimensions, taking into account that X and Y can be 1-D rather than
5609
5609
# 2-D. This perfect match is required for Gouraud shading. For flat
@@ -5665,14 +5665,34 @@ def _pcolorargs(funcname, *args, allmatch=False):
5665
5665
raise TypeError ('Dimensions of C %s are incompatible with'
5666
5666
' X (%d) and/or Y (%d); see help(%s)' % (
5667
5667
C .shape , Nx , Ny , funcname ))
5668
- C = C [:Ny - 1 , :Nx - 1 ]
5668
+
5669
+ if dropdata :
5670
+ C = C [:Ny - 1 , :Nx - 1 ]
5671
+ else :
5672
+ def _interp_grid (X ):
5673
+ # helper for below
5674
+ dX = np .diff (X , axis = 1 )/ 2.
5675
+ X = np .hstack ((X [:, [0 ]] - dX [:, [0 ]],
5676
+ X [:, :- 1 ] + dX ,
5677
+ X [:, [- 1 ]] + dX [:, [- 1 ]])
5678
+ )
5679
+ return X
5680
+
5681
+ if ncols == Nx :
5682
+ X = _interp_grid (X )
5683
+ Y = _interp_grid (Y )
5684
+
5685
+ if nrows == Ny :
5686
+ X = _interp_grid (X .T ).T
5687
+ Y = _interp_grid (Y .T ).T
5688
+
5669
5689
C = cbook .safe_masked_invalid (C )
5670
5690
return X , Y , C
5671
5691
5672
5692
@_preprocess_data ()
5673
5693
@docstring .dedent_interpd
5674
5694
def pcolor (self , * args , alpha = None , norm = None , cmap = None , vmin = None ,
5675
- vmax = None , ** kwargs ):
5695
+ vmax = None , dropdata = None , ** kwargs ):
5676
5696
r"""
5677
5697
Create a pseudocolor plot with a non-regular rectangular grid.
5678
5698
@@ -5686,7 +5706,9 @@ def pcolor(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
5686
5706
5687
5707
``pcolor()`` can be very slow for large arrays. In most
5688
5708
cases you should use the similar but much faster
5689
- `~.Axes.pcolormesh` instead. See there for a discussion of the
5709
+ `~.Axes.pcolormesh` instead. See
5710
+ :ref:`Differences between pcolor() and pcolormesh()
5711
+ <differences-pcolor-pcolormesh>` for a discussion of the
5690
5712
differences.
5691
5713
5692
5714
Parameters
@@ -5711,7 +5733,8 @@ def pcolor(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
5711
5733
5712
5734
The dimensions of *X* and *Y* should be one greater than those of
5713
5735
*C*. Alternatively, *X*, *Y* and *C* may have equal dimensions, in
5714
- which case the last row and column of *C* will be ignored.
5736
+ which case the last row and column of *C* will be ignored if
5737
+ *dropdata* is True (see below).
5715
5738
5716
5739
If *X* and/or *Y* are 1-D arrays or column vectors they will be
5717
5740
expanded as needed into the appropriate 2-D arrays, making a
@@ -5751,6 +5774,13 @@ def pcolor(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
5751
5774
snap : bool, default: False
5752
5775
Whether to snap the mesh to pixel boundaries.
5753
5776
5777
+ dropdata : bool, default: :rc:`pcolor.dropdata`
5778
+ If True (default), and *X* and *Y* are the same size as C in their
5779
+ respective dimensions, drop the last element of C in both
5780
+ dimensions. If False, *X* and *Y* are assumed to be the midpoints
5781
+ of the quadrilaterals, and their edges calculated by linear
5782
+ interpolation.
5783
+
5754
5784
Returns
5755
5785
-------
5756
5786
collection : `matplotlib.collections.Collection`
@@ -5801,12 +5831,19 @@ def pcolor(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
5801
5831
``pcolor()`` displays all columns of *C* if *X* and *Y* are not
5802
5832
specified, or if *X* and *Y* have one more column than *C*.
5803
5833
If *X* and *Y* have the same number of columns as *C* then the last
5804
- column of *C* is dropped. Similarly for the rows.
5834
+ column of *C* is dropped if *dropdata* is True. Similarly for the rows.
5835
+ If *dropdata* is False, then *X* and *Y* are assumed to be at the
5836
+ middle of the quadrilaterals, and the edges of the quadrilaterals are
5837
+ linearly interpolated.
5805
5838
5806
- Note: This behavior is different from MATLAB's ``pcolor()``, which
5839
+ This behavior is different from MATLAB's ``pcolor()``, which
5807
5840
always discards the last row and column of *C*.
5808
5841
"""
5809
- X , Y , C = self ._pcolorargs ('pcolor' , * args , allmatch = False )
5842
+ if dropdata is None :
5843
+ dropdata = rcParams ['pcolor.dropdata' ]
5844
+
5845
+ X , Y , C = self ._pcolorargs ('pcolor' , * args , dropdata = dropdata ,
5846
+ allmatch = False )
5810
5847
Ny , Nx = X .shape
5811
5848
5812
5849
# unit conversion allows e.g. datetime objects as axis values
@@ -5903,7 +5940,8 @@ def pcolor(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
5903
5940
@_preprocess_data ()
5904
5941
@docstring .dedent_interpd
5905
5942
def pcolormesh (self , * args , alpha = None , norm = None , cmap = None , vmin = None ,
5906
- vmax = None , shading = 'flat' , antialiased = False , ** kwargs ):
5943
+ vmax = None , shading = 'flat' , antialiased = False ,
5944
+ dropdata = None , ** kwargs ):
5907
5945
"""
5908
5946
Create a pseudocolor plot with a non-regular rectangular grid.
5909
5947
@@ -5913,9 +5951,9 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
5913
5951
5914
5952
*X* and *Y* can be used to specify the corners of the quadrilaterals.
5915
5953
5916
- .. note ::
5954
+ .. hint ::
5917
5955
5918
- `~Axes.pcolormesh` is similar to `~Axes.pcolor`. It's much faster
5956
+ `~Axes.pcolormesh` is similar to `~Axes.pcolor`. It is much faster
5919
5957
and preferred in most cases. For a detailed discussion on the
5920
5958
differences see :ref:`Differences between pcolor() and pcolormesh()
5921
5959
<differences-pcolor-pcolormesh>`.
@@ -5942,7 +5980,8 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
5942
5980
5943
5981
The dimensions of *X* and *Y* should be one greater than those of
5944
5982
*C*. Alternatively, *X*, *Y* and *C* may have equal dimensions, in
5945
- which case the last row and column of *C* will be ignored.
5983
+ which case the last row and column of *C* will be ignored, unless
5984
+ *dropdata* is *False* (see below).
5946
5985
5947
5986
If *X* and/or *Y* are 1-D arrays or column vectors they will be
5948
5987
expanded as needed into the appropriate 2-D arrays, making a
@@ -5991,6 +6030,13 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
5991
6030
snap : bool, default: False
5992
6031
Whether to snap the mesh to pixel boundaries.
5993
6032
6033
+ dropdata : bool, default: :rc:`pcolor.dropdata`
6034
+ If True (default), and *X* and *Y* are the same size as C in their
6035
+ respective dimensions, drop the last element of C in both
6036
+ dimensions. If False, *X* and *Y* are assumed to be the midpoints
6037
+ of the quadrilaterals, and their edges calculated by linear
6038
+ interpolation.
6039
+
5994
6040
Returns
5995
6041
-------
5996
6042
mesh : `matplotlib.collections.QuadMesh`
@@ -6062,7 +6108,11 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
6062
6108
6063
6109
allmatch = (shading == 'gouraud' )
6064
6110
6065
- X , Y , C = self ._pcolorargs ('pcolormesh' , * args , allmatch = allmatch )
6111
+ if dropdata is None :
6112
+ dropdata = rcParams ['pcolor.dropdata' ]
6113
+
6114
+ X , Y , C = self ._pcolorargs ('pcolormesh' , * args ,
6115
+ allmatch = allmatch , dropdata = dropdata )
6066
6116
Ny , Nx = X .shape
6067
6117
X = X .ravel ()
6068
6118
Y = Y .ravel ()
0 commit comments