Python matplotlib 颜色条的位置和大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16702479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
matplotlib colorbar placement and size
提问by mgilson
I'm using quadmeshto create a simple polar projection plot. Here's a minimal script which produces basically what I'm trying to do:
我正在使用quadmesh创建一个简单的极坐标投影图。这是一个最小的脚本,它基本上产生了我想要做的事情:
from __future__ import unicode_literals
import numpy as np
import matplotlib.pyplot as plt
def make_plot(data,fig,subplot):
nphi,nt = data.shape
phi_coords = np.linspace(0,np.pi*2,nphi+1) - np.pi/2.
theta_coords = np.linspace(0,np.radians(35),nt+1)
ax = fig.add_subplot(subplot,projection='polar')
ax.set_thetagrids((45,90,135,180,225,270,315,360),(9,12,15,18,21,24,3,6))
ax.set_rgrids(np.arange(10,35,10),fmt='%s\u00b0')
theta,phi = np.meshgrid(phi_coords,theta_coords)
quadmesh = ax.pcolormesh(theta,phi,data)
ax.grid(True)
fig.colorbar(quadmesh,ax=ax)
return fig,ax
a = np.zeros((360,71)) + np.arange(360)[:,None]
b = np.random.random((360,71))
fig = plt.figure()
t1 = make_plot(a,fig,121)
t2 = make_plot(b,fig,122)
fig.savefig('test.png')
The above script creates a plot which looks like this:
上面的脚本创建了一个如下所示的图:


I would like the colorbars to:
我希望颜色条:
- Not overlap the 6 label.
- be scaled such that they are approximately the same height as the plot.
- 不重叠 6 标签。
- 进行缩放,使它们与绘图的高度大致相同。
Is there any trick to make this work properly? (Note that this layout isn't the only one I will be using -- e.g. I might use a 1x2 layout, or a 4x4 layout ... It seems like there should be some way to scale the colorbar to the same height as the associated plot...)
有什么技巧可以使它正常工作吗?(请注意,此布局不是我将使用的唯一布局——例如,我可能会使用 1x2 布局或 4x4 布局......似乎应该有某种方法可以将颜色条缩放到与相关情节...)
回答by tacaswell
You can do this with a combination of the pad, shrink, and aspectkwargs:
您可以使用的组合做到这一点pad,shrink和aspectkwargs:
from __future__ import unicode_literals
import numpy as np
import matplotlib.pyplot as plt
def make_plot(data,fig,subplot):
nphi,nt = data.shape
phi_coords = np.linspace(0,np.pi*2,nphi+1) - np.pi/2.
theta_coords = np.linspace(0,np.radians(35),nt+1)
ax = fig.add_subplot(subplot,projection='polar')
ax.set_thetagrids((45,90,135,180,225,270,315,360),(9,12,15,18,21,24,3,6))
ax.set_rgrids(np.arange(10,35,10),fmt='%s\u00b0')
theta,phi = np.meshgrid(phi_coords,theta_coords)
quadmesh = ax.pcolormesh(theta,phi,data)
ax.grid(True)
cb = fig.colorbar(quadmesh,ax=ax, shrink=.5, pad=.2, aspect=10)
return fig,ax,cb
a = np.zeros((360,71)) + np.arange(360)[:,None]
b = np.random.random((360,71))
fig = plt.figure()
t1 = make_plot(a,fig,121)
t2 = make_plot(b,fig,122)
The best value for these parameters will depend on the aspect ratio of the axes.
这些参数的最佳值取决于轴的纵横比。
The size of the axes seems to not get shrink-wrapped to the polar plot, thus in the 1x2 arrangement there is a lot of space above and below the plot that are part in the axes object, but empty. The size of the color bar is keyed off of the rectangular size, not the round size, hence why the default values are not working well. There is probably a way to do the shrink-wrapping, but I do not know how to do that.
轴的大小似乎没有被收缩包裹到极坐标图中,因此在 1x2 排列中,图的上方和下方有很多空间是轴对象的一部分,但是是空的。颜色条的大小与矩形大小无关,而不是圆形大小,因此默认值不能正常工作。可能有一种方法可以进行收缩包装,但我不知道该怎么做。
An alternate method is to force your figure to be the right aspect ratio ex:
另一种方法是强制您的图形具有正确的纵横比,例如:
fig.set_size_inches(10, 4) # for 1x2
fig.set_size_inches(4, 10) # for 2x1
which makes the sub plots square, so the default values more-or-less work.
这使子图呈方形,因此默认值或多或少起作用。
回答by skytaker
This combination (and values near to these) seems to "magically" work for me to keep the colorbar scaled to the plot, no matter what size the display.
这种组合(以及接近这些值的值)似乎“神奇地”为我工作,无论显示大小如何,都可以使颜色条与绘图保持一致。
plt.colorbar(im,fraction=0.046, pad=0.04)

