我需要为一排子图共享相同的颜色条。每个子图都对颜色函数进行对称缩放。这些任务中的每一个都有一个很好的解决方案,这里的解决方案是:For sharing the color bar和for 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()
是的,这是我试图达到的极端情况,谢谢!根据您的回答,我设法修改了我的原始示例代码,使其无需像解决方案中那样手动调整轴尺寸即可运行。我将把它作为一个单独的答案发布,因为我认为这将是实现这一目标的首选方法。 – mjo