如何在mpl_toolkits.axes_grid1.ImageGrid中设置离散的色条刻度?

时间:2018-10-28 13:29:10

标签: python matplotlib label colorbar colormap

我想在ImageGrid中设置离散色条。

ImageGrid

这是一个例子:

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()

这是结果: Test_grid

如您所见,刻度线的位置是错误的。 如果刻度线位于每个色块的边界,则第二个数字看起来正确。

子图

然后,我测试了subplots。很好!

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()

这是结果: Subplots

我用一个数字测试了我的脚本。很好!

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()

这是jetdefined的单个数字的结果: Jet defined

1 个答案:

答案 0 :(得分:1)

一种解决方法是手动设置标签。

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