2017-05-05 32 views
0

我该如何解决这个情节?如何使用gridspec调整列宽?

我想:

  1. 两个彩条不重叠。
  2. 使它们的高度等于情节。

这里是我的代码:

combined = (...) # some irrelevant to question data praparation - this variable contain square matrix with RGBA chanels 

plt.figure(dpi=300, figsize=(2.1,1.9)) 
gs = gridspec.GridSpec(1, 3, width_ratios=[20,1,1]) 
ax1 = plt.subplot(gs[0]) 
ax2 = plt.subplot(gs[1]) 
ax3 = plt.subplot(gs[2]) 

cax = ax1.imshow(combined, interpolation="None") 
mpl.colorbar.ColorbarBase(ax2, cmap=cmap_top, norm=norm_top) 
mpl.colorbar.ColorbarBase(ax3, cmap=cmap_top, norm=norm_top) 

我使用python 3.6和2.0 matplotlib。

broken plot

回答

0

一个建议是不要在这种情况下使用gridspec。您可以使用mpl_toolkits.axes_grid1中的make_axes_locatable类创建新的彩条轴。

然后,您需要为分隔线的填充以及数字边距(使用subplots_adjust)找到一些拟合参数。

import matplotlib.pyplot as plt 
from mpl_toolkits.axes_grid1 import make_axes_locatable 
import matplotlib.colorbar 
import numpy as np; np.random.seed(1) 

a = -np.log10(np.random.rand(25,25)) 

fig, ax = plt.subplots(dpi=300, figsize=(2.1,1.9)) 
fig.subplots_adjust(left=0.15,right=.78) 

cmap=plt.cm.terrain 
norm = matplotlib.colors.LogNorm(1e-3, 4) 

im = ax.imshow(a, interpolation="None", cmap=cmap, norm=norm) 

divider = make_axes_locatable(ax) 
cax = divider.new_horizontal(size="5%", pad=0.05) 
cax2 = divider.new_horizontal(size="5%", pad=0.45) 

fig.add_axes(cax) 
fig.add_axes(cax2) 

plt.colorbar(im, cax=cax) 
plt.colorbar(im, cax=cax2) 

plt.show() 

enter image description here

制造足够的空间,使得彩条ticklabels不重叠发生在这种情况下,几乎一半的数字宽度,但我想这是你想要的。

0

我会考虑问题1的两种可能性: a。修改wspace参数,该参数控制图形之间的水平间距,即:

gs = gridspec.GridSpec(1, 3, width_ratios=[20,1,1]) 
gs.update(wspace=0.05) 

b。添加充当一些空隙空间的第一和第二颜色条之间的一个额外的列:

gs = gridspec.GridSpec(1, 4, width_ratios=[20,1,0.15,1]) 

(0.15这里只是举个例子)

对于问题2号,我想不同的写(给定它的工作原理相当好对我来说:

ax2=plt.subplot(gs[0,1] ) 
cb1 = matplotlib.colorbar.ColorbarBase(ax2, cmap="RdBu_r") 

希望它可以帮助