@@ -745,6 +745,26 @@ def contourf(x, y, z, ax, **kwargs):
745
745
return primitive
746
746
747
747
748
+ def _is_monotonic (coord , axis = 0 ):
749
+ """
750
+ >>> _is_monotonic(np.array([0, 1, 2]))
751
+ True
752
+ >>> _is_monotonic(np.array([2, 1, 0]))
753
+ True
754
+ >>> _is_monotonic(np.array([0, 2, 1]))
755
+ False
756
+ """
757
+ if coord .shape [axis ] < 3 :
758
+ return True
759
+ else :
760
+ n = coord .shape [axis ]
761
+ delta_pos = (coord .take (np .arange (1 , n ), axis = axis ) >=
762
+ coord .take (np .arange (0 , n - 1 ), axis = axis ))
763
+ delta_neg = (coord .take (np .arange (1 , n ), axis = axis ) <=
764
+ coord .take (np .arange (0 , n - 1 ), axis = axis ))
765
+ return np .all (delta_pos ) or np .all (delta_neg )
766
+
767
+
748
768
def _infer_interval_breaks (coord , axis = 0 ):
749
769
"""
750
770
>>> _infer_interval_breaks(np.arange(5))
@@ -754,6 +774,15 @@ def _infer_interval_breaks(coord, axis=0):
754
774
[ 2.5, 3.5, 4.5]])
755
775
"""
756
776
coord = np .asarray (coord )
777
+
778
+ if not _is_monotonic (coord , axis = axis ):
779
+ raise ValueError ("The input coordinate is not sorted in increasing "
780
+ "order along axis %d. This can lead to unexpected "
781
+ "results. Consider calling the `sortby` method on "
782
+ "the input DataArray. To plot data with categorical "
783
+ "axes, consider using the `heatmap` function from "
784
+ "the `seaborn` statistical plotting library." % axis )
785
+
757
786
deltas = 0.5 * np .diff (coord , axis = axis )
758
787
if deltas .size == 0 :
759
788
deltas = np .array (0.0 )
0 commit comments