对称对数缩放中的几个子图的一个颜色条

时间:2016-09-27 16:25:00

标签: python matplotlib scaling subplot colorbar

我需要为一行子图共享相同的颜色条。每个子图具有对称颜色函数的对数缩放。这些任务中的每一个都有一个很好的解决方案,在stackoverflow上解释: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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

根据Erba Aitbayev的解决方案,我发现更换线路就足够了

ax.cax.colorbar(im,ticks=tick_locations, format=ticker.LogFormatter())
最初由行

发布的示例代码中的

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

并且一切正常,无需为colorbar指定显式尺寸。我不知道为什么一个有效,另一个无所不知。最好添加相应的评论in the post on sharing colorbars。我检查过,如果在上面两个备选方案中的第二个中调用了colorbar,那么该示例中的线性色标仍然有效。 (我没有足够的声誉在那里添加评论。)