@@ -745,6 +745,26 @@ def contourf(x, y, z, ax, **kwargs):
745745 return primitive
746746
747747
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+
748768def _infer_interval_breaks (coord , axis = 0 ):
749769 """
750770 >>> _infer_interval_breaks(np.arange(5))
@@ -754,6 +774,15 @@ def _infer_interval_breaks(coord, axis=0):
754774 [ 2.5, 3.5, 4.5]])
755775 """
756776 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+
757786 deltas = 0.5 * np .diff (coord , axis = axis )
758787 if deltas .size == 0 :
759788 deltas = np .array (0.0 )
0 commit comments