2017-06-20 44 views
0

我想擺脫右側的圖像和顏色條值的重疊,但似乎沒有任何工作。我已經嘗試了「緊縮」命令並縮小了彩條文本的大小。前者不起作用,後者似乎根本不起作用。 (這可能嗎?)我需要一種方法來讀取這些數字,這並不重要。 (只要它們不重疊)Python修復了陰謀中的顏色條

enter image description here

gs = gridspec.GridSpec(1, 2) 
gs0 = gridspec.GridSpecFromSubplotSpec(3, 3, subplot_spec=gs[0]) 
gs1 = gridspec.GridSpecFromSubplotSpec(2, 2, subplot_spec=gs[1]) 

fig = plt.figure() 


ax = fig.add_subplot(gs0[0, 0]) 
plt.imshow(getpoly(seg1),origin="lower") 
ax.set_xticks([]); ax.set_yticks([]) 
ax = fig.add_subplot(gs0[0, 1]) 
plt.imshow(getpoly(seg2),origin="lower") 
ax.set_xticks([]); ax.set_yticks([]) 
ax = fig.add_subplot(gs0[0, 2]) 
plt.imshow(getpoly(seg3),origin="lower") 
ax.set_xticks([]); ax.set_yticks([]) 
ax = fig.add_subplot(gs0[1, 0]) 
plt.imshow(getpoly(seg4),origin="lower") 
ax.set_xticks([]); ax.set_yticks([]) 
ax = fig.add_subplot(gs0[1, 1]) 
plt.imshow(getpoly(seg5),origin="lower") 
ax.set_xticks([]); ax.set_yticks([]) 
ax = fig.add_subplot(gs0[1, 2]) 
plt.imshow(getpoly(seg6),origin="lower") 
ax.set_xticks([]); ax.set_yticks([]) 
ax = fig.add_subplot(gs0[2, 1]) 
plt.imshow(getpoly(seg7),origin="lower") 
ax.set_xticks([]); ax.set_yticks([]) 
ax = fig.add_subplot(gs0[2, 2]) 
plt.imshow(getpoly(seg8),origin="lower") 
ax.set_xticks([]); ax.set_yticks([]) 
ax = fig.add_subplot(gs0[2, 0]) 
plt.imshow(getpoly(seg9),origin="lower") 
ax.set_xticks([]); ax.set_yticks([]) 



ax = fig.add_subplot(gs1[0, 0]) 
plt.imshow(h1,origin="lower") 
plt.colorbar(fraction=0.046, pad=0.04) 
ax.set_xticks([]); ax.set_yticks([]) 

ax = fig.add_subplot(gs1[0, 1]) 
plt.imshow(h2,origin="lower") 
plt.colorbar(fraction=0.046, pad=0.04) 
ax.set_xticks([]); ax.set_yticks([]) 

ax = fig.add_subplot(gs1[1, 0]) 
plt.imshow(getpoly(h2),origin="lower") 
plt.colorbar(fraction=0.046, pad=0.04) 
ax.set_xticks([]); ax.set_yticks([]) 

ax = fig.add_subplot(gs1[1, 1]) 
plt.imshow(h1-getpoly(h2),origin="lower") 
plt.colorbar(fraction=0.046, pad=0.04) 
ax.set_xticks([]); ax.set_yticks([]) 

plt.tight_layout() 
+0

這是完整的代碼或它的一部分? – user1767754

回答

2

你可以看看colorbar-whose-height-or-width-in-sync-with-the-master-axes -example 。

這個想法是使用mpl_toolkits.axes_grid1.make_axes_locatable削減一些軸,並使用fig.colorbar(im, cax=cax)創建一個新的可以放置色條的軸。

import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.gridspec as gridspec 
from mpl_toolkits.axes_grid1 import make_axes_locatable 

gs = gridspec.GridSpec(1, 2) 
gs0 = gridspec.GridSpecFromSubplotSpec(3, 3, subplot_spec=gs[0]) 
gs1 = gridspec.GridSpecFromSubplotSpec(2, 2, subplot_spec=gs[1]) 

fig = plt.figure() 

for i in range(9): 
    ax = fig.add_subplot(gs0[i//3, i%3]) 
    ax.imshow(np.random.rand(4,4)) 
    ax.set_xticks([]); ax.set_yticks([]) 


for i in range(4): 
    ax = fig.add_subplot(gs1[i//2, i%2]) 
    im = ax.imshow(np.random.rand(4,4)) 
    ax.set_xticks([]); ax.set_yticks([]) 
    divider = make_axes_locatable(ax) 
    cax = divider.append_axes("right", size="5%", pad=0.05) 
    fig.colorbar(im, ax=ax, cax=cax) 

plt.show() 

enter image description here

0

我發現它更容易add the colorbar separately

cax = fig.add_axes([0.125, 0.925, 0.775, 0.0725]) 
    #the numbers in fig.add_axes are all percentages 
    norm = mpl.colors.Normalize(vmin=low_val, vmax=high_val) 
    mpl.colorbar.ColorbarBase(cax, cmap='rainbow', norm=norm, orientation='horizontal') 

fig.add_axes details

+0

我不認爲有任何理由不使用該圖像來創建顏色條。這裏的問題與定位顏色條有關。所以問題是,你會如何找到放置'cax'的好座標? – ImportanceOfBeingErnest