shaare.it

ValueError: x must be increasing for contour

ValueError: x must be increasing for contour

$ python -c "import numpy as np, matplotlib.pyplot as plt; X=[2,1,0]; Y=[0,1,2]; Z=np.random.rand(3,3); plt.contour(X,Y,Z)"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ValueError: x must be increasing

Why this happens

Non-monotonic axes are invalid for some contour routines.

Fix

Sort axes or provide increasing sequences.

Wrong code

import numpy as np
import matplotlib.pyplot as plt
X = [2,1,0]
Y = [0,1,2]
Z = np.random.rand(3,3)
plt.contour(X, Y, Z)

Fixed code

import numpy as np
import matplotlib.pyplot as plt
X = [0,1,2]
Y = [0,1,2]
Z = np.random.rand(3,3)
plt.contour(X, Y, Z)
plt.show()