如何在垂直方向的颜色栏的左侧和右侧添加刻度?

时间:2018-10-15 00:51:24

标签: python-3.x matplotlib subplot colorbar colormap

我有一个nrows=2 by ncols=2子图。我试图制作一个单独的orientation='vertical'颜色条,其左刻度线对应于第一行,而右刻度线对应于第二行。我不知道如何进行双面勾。下面是制作颜色条的示例代码。

import numpy as np
import matplotlib.pyplot as plt

# Initialize Data Parameters
xmu, ymu = 5, 50 # Normal Distribution values for first row
xsig, ysig = 2, 10 # Normal Distribution values for second row
size = 100 # Number of datapoints
shp = (10, 10) # shape of Z array
levels1 = np.linspace(0, 10, 11, dtype=int) # ticks of first colorbar
levels2 = np.linspace(20, 80, 11, dtype=int) # ticks of second colorbar

# Initialize Data
x1 = np.random.normal(xmu, xsig, size) # row=1 x col=1
x2 = np.random.normal(xmu, xsig, size) # row=1 x col=2
y1 = np.random.normal(ymu, ysig, size) # row=2 x col=1
y2 = np.random.normal(ymu, ysig, size) # row=2 x col=2
data = [x1, x2, y1, y2]
levels = [levels1, levels1, levels2, levels2]

def get_Z(datum, shape=shp):
    """ returns Z array, applied in zip routine below """
    return datum.reshape(shape)

fig, axes = plt.subplots(nrows=2, ncols=2)
for ax, datum, level in zip(axes.ravel(), data, levels):
    im = ax.imshow(get_Z(datum), vmin=min(level), vmax=max(level))

# create axes for colorbar and set colorbar ticks
cax = fig.add_axes([0.9, 0.1, 0.03, 0.8])
cbar = fig.colorbar(im, cax=cax)
cbar.set_ticks(levels2)
cbar.set_ticklabels(levels2)

plt.show()
plt.close(fig)

Example Subplot

是否有一种简单的matplotlib方法将第一行和第二行的刻度分别添加到颜色栏的左侧和右侧?

(我的目标是应用matplotlib example来校正颜色条的值;仍在使用该部件。不确定是否与问题相关。我的实际用例是制作两个单独的子图;第一个是曲面图的子图,第二个是轮廓图的子图。)

0 个答案:

没有答案