【问题标题】:moving colorbar with gridspec使用 gridspec 移动颜色条
【发布时间】:2018-05-19 06:16:55
【问题描述】:

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

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 shrinkpad 时,我收到以下消息:

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

【问题讨论】:

  • 附注:永远不要相信那些声称找到了“最好的方法”的教程。这肯定不是真的。

标签: python matplotlib colorbar


【解决方案1】:

我认为直接在链接中创建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 无关。是否要使用 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()

【讨论】:

  • Note that this is independend of gridspec. 抱歉,我不明白如何使用 gridspec 来完成(我的情节很复杂(即 3*3 或 3*9 带颜色条),这就是我认为使用 gridspec 有意义的原因) .无论如何,我想我会放弃,只是用一个子情节来做......
  • 您的问题有一个子图,它询问了收缩参数。当然,如果你有一个更大的网格,gridspec 可能是有意义的。如果你想用你遇到的实际问题来编辑你的问题(这显然不是收缩论点),我很乐意编辑我的答案。
  • 好的,但是现在有什么问题,因为我已经解释了为什么你不能使用收缩参数?
  • 我已经编辑了这个问题。附带说明 - 我现在让它与子图一起工作。我仍然很感兴趣它是如何使用 gridspec 完成的,因为我确实看到了他们的论点 gain: total control over the plot accuracy grace
  • 或许你可以使用gridspec.GridSpecFromSubplotSpec,是的。但是,对于问题中的情况,我建议使用轴分隔器。我更新了答案。
猜你喜欢
  • 2020-05-05
  • 2018-07-15
  • 2021-10-16
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 2012-11-09
  • 2018-06-03
相关资源
最近更新 更多