python - 使用 gridspec 移动颜色条

标签 python matplotlib colorbar

我正在制作一个包含多个图形和相应颜色条的绘图。本页http://www.sc.eso.org/~bdias/pycoffee/codes/20160407/gridspec_demo.html声称知道这样做的最佳方法。我倾向于相信他们。
然而,当我按照自己的方式处理颜色条的 kwags 时,我遇到了问题:

from matplotlib.colorbar import Colorbar
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt 
import numpy as np
median = np.zeros((100,100))
map0 = np.zeros((100,100))
map1 =  map0
map2 =  map0

fig = plt.figure()
plt.tight_layout()
gs = gridspec.GridSpec(2,6)

ax  = plt.subplot(gs[0,0])
cbax = plt.subplot(gs[0,1])

ax1 = plt.subplot(gs[0,2])
cbax1 = plt.subplot(gs[0,3])

ax2 = plt.subplot(gs[0,4])
cbax2 = plt.subplot(gs[0,5])

ax3 = plt.subplot(gs[1,0])
cbax3 = plt.subplot(gs[1,1])

ax4 = plt.subplot(gs[1,2])
cbax4 = plt.subplot(gs[1,3])

ax5 = plt.subplot(gs[1,4])
cbax5 = plt.subplot(gs[1,5])

cax = ax.imshow(map0)
ax.contour(median)
cb = Colorbar(ax = cbax,mappable = cax,shrink=0.8)

cax1 = ax1.imshow(map1)
ax1.contour(median)
cb1 = Colorbar(ax = cbax1,mappable = cax1)

cax2 = ax2.imshow(map2)
ax2.contour(median)
cb2 = Colorbar(ax = cbax2,mappable = cax2)

cax3 = ax3.imshow(map0/median)
ax3.contour(median)
cb3 = Colorbar(ax = cbax3,mappable = cax3)

cax4 = ax4.imshow(map1/median)
ax4.contour(median)
cb4 = Colorbar(ax = cbax4,mappable = cax4)

cax5 = ax5.imshow(map2)
ax5.contour(median)
cb5 = Colorbar(ax = cbax5,mappable = cax5)

当我现在调用 kwargs shr​​ink 和或 pad 时,我收到以下消息:

Traceback (most recent call last):
  File "plot_integratedMaps.py", line 173, in <module>
    main()
  File "plot_integratedMaps.py", line 171, in main
    plot_integratedMaps(map630,map869,mapTot,median)
  File "plot_integratedMaps.py", line 129, in plot_integratedMaps
    cb = Colorbar(ax = cbax,mappable = cax,shrink=0.8)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/colorbar.py", line 943, in __init__
    ColorbarBase.__init__(self, ax, **kw)
TypeError: __init__() got an unexpected keyword argument 'shrink'

我想我无法在 gs[0,1] 中填充颜色条,而必须移动 gs[0,1] ,这是有道理的。但我不明白为什么收缩不起作用?

正在使用Python 2.7.12

最佳答案

我认为像链接中那样直接创建Colorbar没有用;相反,可以使用fig.colorbar()。然而,这只是与问题无关的。

首先考虑在绘图旁边创建一个颜色栏。

import matplotlib.pyplot as plt 
import numpy as np

median = np.zeros((100,100))
map0 = np.zeros((100,100))

fig, ax = plt.subplots()

im = ax.imshow(map0)
ax.contour(median)
cb = fig.colorbar(im, ax=ax, shrink=0.8)

plt.show()

在这里,shr​​ink 工作得很好,因为您希望颜色条所在的轴比它所属的轴 ax 小 0.8 倍。

现在,如果您指定颜色条应驻留在其中的轴,则 shr​​ink 没有任何意义,因为不需要在颜色条函数内部创建轴,而是您在外部提供它。

import matplotlib.pyplot as plt 
import numpy as np

median = np.zeros((100,100))
map0 = np.zeros((100,100))

fig, (ax,cax) = plt.subplots(ncols=2)

im = ax.imshow(map0)
ax.contour(median)

#using `shrink` here would produce an error, 
# because the colorbar axes (cax) already exists
# instead of 
# cb = fig.colorbar(im, cax=cax, shrink=0.8)
# you need
cb = fig.colorbar(im, cax=cax)

plt.show()

请注意,这与网格规范无关。是否要使用 gridspec 也是一个品味问题,但对于简单的绘图来说肯定不需要。

如果你有更多的情节,这又取决于你想展示什么。问题中编辑后的示例看起来更像是常规网格。在这里,可以通过 make_axes_locatable 有效地为每个子图创建颜色条轴。

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

data = np.random.rand(6,6)

fig, axes = plt.subplots(nrows=2, ncols=3)

for ax in axes.flatten():
    im = ax.imshow(data)
    div = make_axes_locatable(ax)
    cax = div.append_axes("right", size="5%", pad=0.1)
    cbar = fig.colorbar(im, cax=cax)

plt.tight_layout()    
plt.show()

enter image description here

根据上述内容,您可以不使用此轴分隔符来缩小颜色条,但像往常一样,创建颜色条并使用 shr​​ink 参数。

import matplotlib.pyplot as plt 
import numpy as np

data = np.random.rand(6,6)

fig, axes = plt.subplots(nrows=2, ncols=3)

for ax in axes.flatten():
    im = ax.imshow(data)
    cbar = fig.colorbar(im, ax=ax, shrink=0.4)

plt.tight_layout()    
plt.show()

enter image description here

关于python - 使用 gridspec 移动颜色条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47656119/

相关文章:

python - python 中 testdome 的训练组合

Python、Numpy、OpenCV——创建修改后的(同样快的) "addWeighted"函数

python - 获取由 scatter() 创建的 PathCollection 中的点的位置

matplotlib basemap 颜色条文本位置

matplotlib - 当 DataFrame-Column 缺少值时 PyPlot 会抛出错误

python - 设置两个 matplotlib imshow 图具有相同的颜色图比例

matlab - 如何使颜色条引用 3D 绘图中的标记而不是 Matlab 中的表面

python - matplotlib 中图像网格上的两个颜色条

python - 为什么我的 Python 脚本在我的 Raspberry Pi 启动时通过将命令添加到/etc/profile 来尝试在后台运行两次?

Python如何使用ArgumentParser.convert_arg_line_to_args跳过文件中的注释行