a.smiet
a.smiet

Reputation: 1825

Get tick values of colorbar in matplotlib

How can I extract the currently displayed ticks of a matplotlib colorbar as a list? With this example I should get the the 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()

Upvotes: 8

Views: 8475

Answers (2)

pcu
pcu

Reputation: 1214

In version 2.1 of matplotlib colorbar.get_ticks() method was introduced.

Upvotes: 9

Serenity
Serenity

Reputation: 36635

Get ticklabels of colorbar axis (it is may be x or y axes):

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 minus is a little bit different and I replace it with usual minus symbol (or it matplotlib behavior).

Output

[-0.6, -0.4, -0.2, 0.0, 0.2, 0.4, 0.6]

Upvotes: 7

Related Questions