Home > Mobile >  Matplotlib imshow ticks are wrong with negative values
Matplotlib imshow ticks are wrong with negative values

Time:09-27

import matplotlib.pyplot as plt
import numpy as np


a = np.random.randn(5,5)
plt.imshow(a)
plt.xticks(range(5))
plt.yticks([i-2 for i in range(5)])
plt.show()

results in

enter image description here

??

Also imagine I had 500 instead of 5 ticks, how could I pass the ticks but have less be displayed (for example every 10th)?

CodePudding user response:

Use the extent parameter, and no need to use xticks or yticks:

plt.imshow(a, extent=(-0.5, 4.5, -2.5, 2.5))

Output:

enter image description here

Use enter image description here

  • Related