How to Set Plot Scale Range in Python matplotlib
Code1
You can fix the scale of the image using plt.clim()
. However, plt.imshow()
does not print the color bar by default, so you need to add plt.colorbar()
to see it.
import numpy as np
import matplotlib.pyplot as plt
A = np.random.rand(4,4)
plt.imshow(A)
plt.colorbar()
plt.show()
plt.imshow(A)
plt.colorbar()
plt.clim(0,1)
plt.show()
The results are as follows. Unlike the first image, in the second image, you can see that the range of the color bar is fixed from 0 to 1.
Environment
- OS: Windows10
- Version: Python 3.9.2, matplotlib 3.4.2