2017-05-05 38 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") 

希望它可以幫助