Baz
Baz

Reputation: 13135

Ticks not showing correctly on colorbar

With the following code:

import matplotlib.cm as cm
from matplotlib import colors
import matplotlib.pyplot as plt

colormap = cm.YlOrRd

norm = colors.LogNorm(vmin=100, vmax=10000)
sm = plt.cm.ScalarMappable(cmap=colormap, norm=norm)
sm.set_array([])
cb = plt.colorbar(sm, ticks = [1,10,100,1000,10000,100000]) 
plt.show()

I would expect the ticks to be "100 1000 10000" but instead I see powers of 10 instead. How do I fix this?

Upvotes: 1

Views: 3240

Answers (2)

Sheldore
Sheldore

Reputation: 39052

Your vmin is 100 but you are setting a tick at 1 and 10. The vmax is 10000 but the highest tick is at 100000. I have corrected the tick values to match with your desired color bar.

While using format is the best solution, an alternative way could be the following

tick_list = [100,1000,10000]
cb = plt.colorbar(sm, ticks = tick_list)
cb.set_ticklabels(list(map(str, tick_list)))

enter image description here

Upvotes: 0

nassim
nassim

Reputation: 1555

well there is a way

cb = plt.colorbar(sm, ticks = [1,10,100,1000,10000,100000], format='%.0f') 

this takes off the 10^m notation, you just need to precise the format

format='%.0f'

Upvotes: 2

Related Questions