Python 每个子图中的 matplotlib 颜色条

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23876588/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:35:20  来源:igfitidea点击:

matplotlib colorbar in each subplot

pythonmatplotlibcolorbar

提问by Cokes

I would like to add a separate colorbar to each subplot in a 2x2 plot.

我想为 2x2 图中的每个子图添加一个单独的颜色条。

fig , ( (ax1,ax2) , (ax3,ax4)) = plt.subplots(2, 2,sharex = True,sharey=True)
z1_plot = ax1.scatter(x,y,c = z1,vmin=0.0,vmax=0.4)
plt.colorbar(z1_plot,cax=ax1)
z2_plot = ax2.scatter(x,y,c = z2,vmin=0.0,vmax=40)
plt.colorbar(z1_plot,cax=ax2)
z3_plot = ax3.scatter(x,y,c = z3,vmin=0.0,vmax=894)
plt.colorbar(z1_plot,cax=ax3)
z4_plot = ax4.scatter(x,y,c = z4,vmin=0.0,vmax=234324)
plt.colorbar(z1_plot,cax=ax4)
plt.show()

I thought that this is how you do it, but the resulting plot is really messed up; it just has an all grey background and ignores the set_xlim , set_ylim commands I have (not shown here for simplicity). + it shows no color bars. Is this the right way to do it?

我以为这就是你的做法,但结果情节真的一团糟;它只是有一个全灰色的背景,并忽略了我拥有的 set_xlim 、 set_ylim 命令(为了简单起见,这里没有显示)。+ 它没有显示颜色条。这是正确的方法吗?

I also tried getting rid of the "cax = ...", but then the colorbar all goes on the bottom right plot and not to each separate plot!

我也尝试摆脱“cax = ...”,但是颜色条都在右下角的图上,而不是每个单独的图!

采纳答案by Cokes

Please have a look at this matplotlib example page. There it is shown how to get the following plot with four individual colorbars for each subplot: enter image description here

请看一下这个matplotlib 示例页面。那里展示了如何为每个子图使用四个单独的颜色条获得以下图:在此处输入图片说明

I hope this helps.
You can further have a look here, where you can find a lot of what you can do with matplotlib.

我希望这有帮助。
您可以进一步查看这里,在那里您可以找到很多您可以使用的内容matplotlib

回答by jayesef

In plt.colorbar(z1_plot,cax=ax1), use ax=instead of cax=, i.e. plt.colorbar(z1_plot,ax=ax1)

plt.colorbar(z1_plot,cax=ax1),使用ax=代替cax=,即plt.colorbar(z1_plot,ax=ax1)

回答by Ramon Martinez

This can be easily solved with the the utility make_axes_locatable. I provide a minimal example that shows how this works and should be readily adaptable:

使用该实用程序可以轻松解决此问题make_axes_locatable。我提供了一个最小的例子来展示它是如何工作的并且应该很容易适应:

bar to each image

每个图像的条形图

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

import numpy as np

m1 = np.random.rand(3, 3)
m2 = np.arange(0, 3*3, 1).reshape((3, 3))

fig = plt.figure(figsize=(16, 12))
ax1 = fig.add_subplot(121)
im1 = ax1.imshow(m1, interpolation='None')

divider = make_axes_locatable(ax1)
cax = divider.append_axes('right', size='5%', pad=0.05)
fig.colorbar(im1, cax=cax, orientation='vertical')

ax2 = fig.add_subplot(122)
im2 = ax2.imshow(m2, interpolation='None')

divider = make_axes_locatable(ax2)
cax = divider.append_axes('right', size='5%', pad=0.05)
fig.colorbar(im2, cax=cax, orientation='vertical');

回答by Patrick Sanan

Specify the axargument to matplotlib.pyplot.colorbar(), e.g.

ax参数指定为matplotlib.pyplot.colorbar(),例如

import numpy as np
import matplotlib.pyplot as plt

fig,ax = plt.subplots(2,2)
for i in range(2) :
    for j in range(2) :
         data = np.array([[i,j],[i+0.5,j+0.5]])
         im = ax[i,j].imshow(data)
         plt.colorbar(im,ax=ax[i,j])

plt.show()

enter image description here

在此处输入图片说明

回答by uqzzhao

Try to use the func below to add colorbar:

尝试使用下面的函数来添加颜色条:

def add_colorbar(mappable):
    from mpl_toolkits.axes_grid1 import make_axes_locatable
    import matplotlib.pyplot as plt
    last_axes = plt.gca()
    ax = mappable.axes
    fig = ax.figure
    divider = make_axes_locatable(ax)
    cax = divider.append_axes("right", size="5%", pad=0.05)
    cbar = fig.colorbar(mappable, cax=cax)
    plt.sca(last_axes)
    return cbar

Then you codes need to be modified as:

然后你的代码需要修改为:

fig , ( (ax1,ax2) , (ax3,ax4)) = plt.subplots(2, 2,sharex = True,sharey=True)
z1_plot = ax1.scatter(x,y,c = z1,vmin=0.0,vmax=0.4)
add_colorbar(z1_plot)