我正在制作一个包含多个图形和相应颜色条的绘图。本页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 shrink
和或 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()
在这里,shrink
工作得很好,因为您希望颜色条所在的轴比它所属的轴 ax
小 0.8 倍。
现在,如果您指定颜色条应驻留在其中的轴,则 shrink
没有任何意义,因为不需要在颜色条函数内部创建轴,而是您在外部提供它。
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()
根据上述内容,您可以不使用此轴分隔符来缩小颜色条,但像往常一样,创建颜色条并使用 shrink
参数。
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()
关于python - 使用 gridspec 移动颜色条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47656119/