Search code examples
pythonmatplotliblabelcolorbarcolormap

How to set discrete colorbar ticks in mpl_toolkits.axes_grid1.ImageGrid?


I want to set discrete colorbar in ImageGrid.

ImageGrid

Here's an example:

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import ImageGrid
import numpy as np
import matplotlib

lon,lat = np.meshgrid(np.arange(-180, 180, 10), np.arange(-85, 90, 10))
data = np.sort(np.random.rand(18, 36),axis=1)

fig = plt.figure()
grid = ImageGrid(fig, 111,
                nrows_ncols=(2, 1),
                axes_pad=(0.35, 0.35),
                label_mode="1",
                share_all=True,
                cbar_location="right",
                cbar_mode="each",
                cbar_size="5%",
                cbar_pad="6%",
                )

# Settings
bounds = [0,0.01,0.04,0.07,0.1,0.13,0.16,0.2,0.25,0.35,0.45,0.6,0.9]
colors = ['#390231','#7F1CAB','#0047FD','#0072FE','#019EFF','#00C4FF','#01EDFF',\
        '#00FFFB','#00FFC8','#29F905','#FBDD03','#FA0F00']

# Original colorbar
p = grid[0].pcolormesh(lon,lat,data, vmin=0, vmax=0.9, cmap='jet')
cb = grid.cbar_axes[0].colorbar(p)

# Defined colorbar
cmap = matplotlib.colors.ListedColormap(colors)
norm = matplotlib.colors.BoundaryNorm(bounds, cmap.N)
p = grid[1].pcolormesh(lon,lat,data, cmap=cmap, norm=norm)
cb = grid.cbar_axes[1].colorbar(p, ticks=bounds)

grid[0].set_title('jet')
grid[1].set_title('Defined')
plt.show()

This is the result: Test_grid

As you can see, the location of ticks are wrong. If ticks are at boundaries of each color block, the second figure will look correct.

Subplots

Then, I tested subplots. It works fine!

import matplotlib.pyplot as plt
import numpy as np
import matplotlib

lon,lat = np.meshgrid(np.arange(-180, 180, 10), np.arange(-85, 90, 10))
data = np.sort(np.random.rand(18, 36),axis=1)

f, (ax1, ax2) = plt.subplots(1, 2,sharey=True)

# Settings
bounds = [0,0.01,0.04,0.07,0.1,0.13,0.16,0.2,0.25,0.35,0.45,0.6,0.9]
colors = ['#390231','#7F1CAB','#0047FD','#0072FE','#019EFF','#00C4FF','#01EDFF',\
        '#00FFFB','#00FFC8','#29F905','#FBDD03','#FA0F00']

# Defined colorbar
cmap = matplotlib.colors.ListedColormap(colors)
norm = matplotlib.colors.BoundaryNorm(bounds, cmap.N)

# Jet
p = ax1.pcolormesh(lon,lat,data, vmin=0, vmax=0.9, cmap='jet')
f.colorbar(p,ax=ax1)
ax1.set_title('jet')

# Defined
p = ax2.pcolormesh(lon,lat,data, cmap=cmap, norm=norm)
f.colorbar(p,ax=ax2,ticks=bounds)
ax2.set_title('defined')

plt.show()

This is the result: Subplots

Single

I tested my script in single figure. It works fine!

import matplotlib.pyplot as plt
import numpy as np
import matplotlib

lon,lat = np.meshgrid(np.arange(-180, 180, 10), np.arange(-85, 90, 10))
data = np.sort(np.random.rand(18, 36),axis=1)

fig = plt.figure()

# Settings
bounds = [0,0.01,0.04,0.07,0.1,0.13,0.16,0.2,0.25,0.35,0.45,0.6,0.9]
colors = ['#390231','#7F1CAB','#0047FD','#0072FE','#019EFF','#00C4FF','#01EDFF',\
        '#00FFFB','#00FFC8','#29F905','#FBDD03','#FA0F00']

# Defined colorbar
cmap = matplotlib.colors.ListedColormap(colors)
norm = matplotlib.colors.BoundaryNorm(bounds, cmap.N)

# Jet
plt.pcolormesh(lon,lat,data, vmin=0, vmax=0.9, cmap='jet')
plt.colorbar()
plt.show()

# Defined
p = plt.pcolormesh(lon,lat,data, cmap=cmap, norm=norm)
plt.colorbar(p, ticks=bounds)
plt.title('Single fig')

plt.show()

This is the result of single figure of jet and defined: Jet defined


Solution

  • A workaround would be to set the labels manually.

    ticks=np.linspace(bounds[0],bounds[-1], len(bounds))
    cb = grid.cbar_axes[1].colorbar(p, ticks=ticks)
    cb.ax.set_yticklabels(bounds)
    

    enter image description here