Log labels on colorbar matplotlib

I have an imshow logarithmic chart, and when a color bar is created, its labels and label labels are logarithmic, but due to the small range (0-50) of values, the color bar looks like this:

enter image description here

and I would like it to display as 0, 5, 10, 20, 50, spaced along the axis (log interval).

I can't seem to get this to work.

Thanks for any help.

+3


source to share


1 answer


Use the class LogFormatter

and set labelOnlyBase

to False

:



import matplotlib.pyplot as plt
import numpy as np
import matplotlib.colors
from matplotlib.ticker import LogFormatter 

A = np.random.rand(50,50)*50
plt.imshow(A, norm=matplotlib.colors.LogNorm())
formatter = LogFormatter(10, labelOnlyBase=False) 
cb = plt.colorbar(ticks=[1,5,10,20,50], format=formatter)
plt.show()

      

+6


source







All Articles