How to add Matplotlib Colorbar Ticks

61,734

Solution 1

Update the ticks and the tick labels:

cbar.set_ticks([mn,md,mx])
cbar.set_ticklabels([mn,md,mx])

Solution 2

A working example (for any value range) with five ticks along the bar is:

m0=int(np.floor(field.min()))            # colorbar min value
m4=int(np.ceil(field.max()))             # colorbar max value
m1=int(1*(m4-m0)/4.0 + m0)               # colorbar mid value 1
m2=int(2*(m4-m0)/4.0 + m0)               # colorbar mid value 2
m3=int(3*(m4-m0)/4.0 + m0)               # colorbar mid value 3
cbar.set_ticks([m0,m1,m2,m3,m4])
cbar.set_ticklabels([m0,m1,m2,m3,m4])

Solution 3

treenick answer got me started but if your colorbar is scaled between 0 and 1, that code will not plot the ticks if your fields is not scaled between 0 and 1. So instead I used

m0=int(np.floor(field.min()))            # colorbar min value
m4=int(np.ceil(field.max()))             # colorbar max value
num_ticks = 10
# to get ticks
ticks = np.linspace(0, 1, num_ticks)
# get labels
labels = np.linspace(m0, m1, num_ticks)

If you want spaced out labels you can do python list indexing like so: assuming skipping every other ticks

ticks = ticks[::2]
labels = labels[::2]

Solution 4

you can try something like

from pylab import *
from matplotlib.colors import LogNorm
import matplotlib.pyplot as plt

f = np.arange(0,101)                 # frequency 
t = np.arange(11,245)                # time
z = 20*np.sin(f**0.56)+22            # function
z = np.reshape(z,(1,max(f.shape)))   # reshape the function
Z = z*np.ones((max(t.shape),1))      # make the single vector to a mxn matrix
T, F = meshgrid(f,t)
fig = plt.figure()
ax = fig.add_subplot(111)
plt.pcolor(F,T,Z, norm=LogNorm(vmin=z.min(),vmax=z.max()))
plt.xlim((t.min(),t.max()))
v1 = np.linspace(Z.min(), Z.max(), 8, endpoint=True)
cbar=plt.colorbar(ticks=v1)              # the mystery step ???????????
cbar.ax.set_yticklabels(["{:4.2f}".format(i) for i in v1]) # add the labels
plt.show()

enter image description here

Share:
61,734
sequoia
Author by

sequoia

Updated on December 03, 2020

Comments

  • sequoia
    sequoia over 3 years

    There are many matplotlib colorbar questions on stack overflow, but I can't make sense of them in order to solve my problem.

    How do I set the yticklabels on the colorbar?

    Here is some example code:

    from pylab import *
    from matplotlib.colors import LogNorm
    import matplotlib.pyplot as plt
    
    f = np.arange(0,101)                 # frequency 
    t = np.arange(11,245)                # time
    z = 20*np.sin(f**0.56)+22            # function
    z = np.reshape(z,(1,max(f.shape)))   # reshape the function
    Z = z*np.ones((max(t.shape),1))      # make the single vector to a mxn matrix
    T, F = meshgrid(f,t)
    fig = plt.figure()
    ax = fig.add_subplot(111)
    plt.pcolor(F,T,Z, norm=LogNorm(vmin=z.min(),vmax=z.max()))
    plt.xlim((t.min(),t.max()))
    mn=int(np.floor(Z.min()))        # colorbar min value
    mx=int(np.ceil(Z.max()))         # colorbar max value
    md=(mx-mn)/2                     # colorbar midpoint value
    cbar=plt.colorbar()              # the mystery step ???????????
    cbar.set_yticklabels([mn,md,mx]) # add the labels
    plt.show()
    
  • FaCoffee
    FaCoffee about 7 years
    How come my mx ticks is the only one not to be visualized in the colorbar? How can this happen?
  • Jason
    Jason over 4 years
    This will in most cases give numbers with weird digits like 0.12349956
  • Alexander
    Alexander over 3 years
    @FaCoffee You need to map the ticks mn, md, mx to the interval between 0 and 1 in order to display all tick labels.
  • Ruli
    Ruli over 3 years
    Welcome to StackOverflow. While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.