如何在其他子图中绘制pcolor colorbar-Matplotlib [英] How to plot pcolor colorbar in a different subplot - matplotlib

查看:513
本文介绍了如何在其他子图中绘制pcolor colorbar-Matplotlib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将图分成不同的子图..我想要实现的是将一个子图的颜色条放在另一个子图中. 现在我正在使用:

I am trying to split my plots in different subplots.. What I want to achieve is to put a colorbar for a subplot in a different subplot. Right now I am using:

# first graph
axes = plt.subplot2grid((4, 2), (0, 0), rowspan=3)
pc = plt.pcolor(df1, cmap='jet')

# second graph
axes = plt.subplot2grid((4, 2), (3, 0))
plt.pcolor(df2, cmap='Greys')

# colorbar
plt.subplot2grid((4, 2), (0, 1), rowspan=3)
plt.colorbar(pc) 

但是结果如下(注意,不需要的空白图形留在了颜色栏上):

But the result is the following (notice the unwanted empty graph left to the colorbar):

我该怎么做,仅打印颜色条而不显示左图?

What can I do to print only the colorbar without the left plot?

谢谢

推荐答案

colorbar()接受cax关键字参数,该参数允许您指定将在其上绘制颜色条的axes对象.

colorbar() accepts a cax keyword argument that allows you to specify the axes object that the colorbar will be drawn on.

根据您的情况,您可以将颜色条调用更改为以下内容:

In your case, you would change your colorbar call to the following:

# colorbar
axes = plt.subplot2grid((4, 2), (0, 1), rowspan=3)
plt.colorbar(pc, cax=axes)

这将占用subplot2grid给定的整个空间;您可以通过使主轴占用比色条轴多得多的列,或者通过显式设置gridspec来将其调整为更加合理.例如,您的身材可能更容易通过以下方式进行调整:

This will take up the whole space given by subplot2grid; you can adjust this to be more reasonable either by having the main axes take up many more columns than the colorbar axes, or by setting up the gridspec explicitly. For example, your figure may be easier to tweak with the following:

from matplotlib import gridspec
gs = gridspec.GridSpec(2, 2, height_ratios=(3, 1), width_ratios=(9, 1))

# first graph
axes = plt.subplot(gs[0,0])
pc = plt.pcolor(df1, cmap='jet')

# second graph
axes = plt.subplot(gs[1,0])
plt.pcolor(df2, cmap='Greys')

# colorbar
axes = plt.subplot(gs[0,1])
plt.colorbar(pc, cax=axes)

然后,您可以根据自己的喜好更改height_ratioswidth_ratios.

Then you can just change height_ratios and width_ratios to your liking.

这篇关于如何在其他子图中绘制pcolor colorbar-Matplotlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆