2016-09-27 61 views
2

我需要为一排子图共享相同的颜色条。每个子图都对颜色函数进行对称缩放。这些任务中的每一个都有一个很好的解决方案,这里的解决方案是:For sharing the color barfor nicely formatted symmetric logarithmic scaling对称对数缩放中的几个子图的一个颜色条

但是,当我在相同的代码中组合两个技巧时,颜色条“忘记”是假定为对称的对数。有没有办法解决这个问题?

测试代码是下面,我结合上面明显的方式将两个引用:

import numpy as np                     
import matplotlib.pyplot as plt                  
from mpl_toolkits.axes_grid1 import ImageGrid              
from matplotlib import colors, ticker                

# Set up figure and image grid                  
fig = plt.figure(figsize=(9.75, 3))                 

grid = ImageGrid(fig, 111,   # as in plt.subplot(111)           
       nrows_ncols=(1,3),                 
       axes_pad=0.15,                  
       share_all=True,                  
       cbar_location="right",                
       cbar_mode="single",                 
       cbar_size="7%",                  
       cbar_pad=0.15,                  
       )                     

data = np.random.normal(size=(3,10,10))                
vmax = np.amax(np.abs(data))                   

logthresh=4                       
logstep=1                       
linscale=1                       

maxlog=int(np.ceil(np.log10(vmax)))                 

#generate logarithmic ticks                   
tick_locations=([-(10**x) for x in xrange(-logthresh, maxlog+1, logstep)][::-1]      
       +[0.0]                    
       +[(10**x) for x in xrange(-logthresh,maxlog+1, logstep)])       

# Add data to image grid                    
for ax, z in zip(grid,data):                   
    print z                       
    im = ax.imshow(z, vmin=-vmax, vmax=vmax,               
        norm=colors.SymLogNorm(10**-logthresh, linscale=linscale))      

# Colorbar                       
ax.cax.colorbar(im,ticks=tick_locations, format=ticker.LogFormatter())        
ax.cax.toggle_label(True)                   

#plt.tight_layout() # Works, but may still require rect paramater to keep colorbar labels visible 
plt.show() 

生成的输出如下: enter image description here

回答

0

这是你要实现的目标?

import numpy as np                     
import matplotlib.pyplot as plt                  
from mpl_toolkits.axes_grid1 import ImageGrid              
from matplotlib import colors, ticker                

# Set up figure and image grid                  
fig = plt.figure(figsize=(9.75, 3))                 

grid = ImageGrid(fig, 111,   # as in plt.subplot(111)           
       nrows_ncols=(1,3),                 
       axes_pad=0.15,                  
       share_all=True                                   
       )                     

data = np.random.normal(size=(3,10,10))                
vmax = np.amax(np.abs(data))                   

logthresh=4                       
logstep=1                       
linscale=1                       

maxlog=int(np.ceil(np.log10(vmax)))                 

#generate logarithmic ticks                   
tick_locations=([-(10**x) for x in xrange(-logthresh, maxlog+1, logstep)][::-1]      
       +[0.0]                    
       +[(10**x) for x in xrange(-logthresh,maxlog+1, logstep)])       


# Add data to image grid                    
for ax, z in zip(grid,data):                   
    print z                       
    im = ax.imshow(z, vmin=-vmax, vmax=vmax,               
        norm=colors.SymLogNorm(10**-logthresh, linscale=linscale)) 

cbaxes = fig.add_axes([0.9, 0.125, 0.02, 0.77])      
fig.colorbar(im, format=ticker.LogFormatter(), ticks=tick_locations, cax = cbaxes) 
ax.cax.toggle_label(True) 


#plt.tight_layout() # Works, but may still require rect paramater to keep colorbar labels visible 
plt.show() 

输出: enter image description here

+0

是的,这是我试图达到的极端情况,谢谢!根据您的回答,我设法修改了我的原始示例代码,使其无需像解决方案中那样手动调整轴尺寸即可运行。我将把它作为一个单独的答案发布,因为我认为这将是实现这一目标的首选方法。 – mjo

0

基于由Erba的艾特巴耶夫该溶液中,我发现,它足以替代最初发布由线中的示例代码行

ax.cax.colorbar(im,ticks=tick_locations, format=ticker.LogFormatter()) 

fig.colorbar(im,ticks=tick_locations, format=ticker.LogFormatter(), cax = ax.cax) 

,一切工作无需为颜色条指定明确的尺寸。不过,我不知道为什么一个人工作,另一个人不工作。最好添加相应的评论in the post on sharing colorbars。我检查了这个例子中的线性色阶,如果在上面两个替代方法中的第二个中调用了颜色条,它仍然可以工作。 (我没有足够的声望在那里添加评论。)

相关问题