Get check marks in colorbar in matplotlib
How can I extract the currently displayed ticks of a colored bar matplotlib
as a list? In this example, I have to get a list with floats[-0.6, -0.4, -0.2, 0, 0.2, 0.4, 0.6]
import matplotlib.pyplot as plt
import numpy as np
img = np.random.normal(0, 0.2, size=(100,100))
plt.imshow(img)
plt.colorbar()
plt.show()
+3
source to share
2 answers
Get the labels of the color axis labels (this can be the x or y axis):
cb=plt.colorbar()
for t in cb.ax.get_yticklabels(): print(t.get_text())
or
ticks = [float(t.get_text().replace('−','-')) for t in cb.ax.get_yticklabels()]
print (ticks)
In my locale, the minus is slightly different and I replace it with the usual minus character (or is this matplotlib behavior).
Output
[-0.6, -0.4, -0.2, 0.0, 0.2, 0.4, 0.6]
+3
source to share