Colormap整个子图

Ash*_*ton 2 python matplotlib color-mapping

我在使用彩色地图时遇到了一些麻烦.基本上,我想要制作的内容与下图相似.

在底部的插曲,我想能够绘制相应的颜色,但跨越subplot.ie它只是看起来像一个colourmap在整个剧情的整个背景,没有绘制的点或线.它应该仍然对应于散点图中显示的颜色.

是否有可能做到这一点?我理想的做法是将此背景置于顶部子图下.(y刻度是不同的单位)

在此输入图像描述

谢谢你的帮助.

底部散点子图的代码:

x = np.arange(len(wind))
y = wind
t = y
plt.scatter(x, y, c=t)
Run Code Online (Sandbox Code Playgroud)

风是一维阵列

Rut*_*ies 5

您可以使用imshow来显示风阵列.它需要重新整形为2D数组,但"高度"尺寸可以是长度1.将顶部轴的尺寸设置为与其对齐.

wind = np.random.randn(100) + np.random.randn(100).cumsum() * 0.5
x = np.arange(len(wind))
y = wind
t = y

fig, ax = plt.subplots(2,1,figsize=(10,6))

ax[0].plot(x,y)

ax[1].plot(x, 100- y * 10, lw=2, c='black')

ymin, ymax = ax[1].get_ybound()
xmin, xmax = ax[1].get_xbound()

im = ax[1].imshow(y.reshape(1, y.size), extent=[xmin,xmax,ymin,ymax], interpolation='none', alpha=.5, cmap=plt.cm.RdYlGn_r)
ax[1].set_aspect(ax[0].get_aspect())

cax = fig.add_axes([.95,0.3,0.01,0.4])
cb = plt.colorbar(im, cax=cax)
cb.set_label('Y parameter [-]')
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

如果你想将它用作"背景",你应该首先绘制你想要的任何东西.然后抓住底部图的范围并将其设置为范围imshow.您还可以imshow使用您想要的任何色彩映射cmap=.