使用极坐标投影向 pcolormesh 添加颜色条 [英] Adding a colorbar to a pcolormesh with polar projection

查看:39
本文介绍了使用极坐标投影向 pcolormesh 添加颜色条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将色条添加到具有极坐标投影的pcolormesh图中.如果我不指定极坐标投影,则代码可以正常工作.指定极坐标投影后,会生成一个很小的图,并且没有颜色条.我在做一些愚蠢的事情,还是这是一个错误?我在Fedora 20上使用的是matplotlib 1.3.1.

I am trying to add a colorbar to a pcolormesh plot with polar projection. The code works fine if I don't specify a polar projection. With polar projection specified, a tiny plot results, and the colorbar is absent. Am I doing something stupid, or is this a bug? I am using matplotlib 1.3.1 on Fedora 20.

import matplotlib.pyplot as plot
import mpl_toolkits.axes_grid1 as axes_grid1
import numpy as np

t = np.linspace(0.0, 2.0 * np.pi, 360)
r = np.linspace(0,100,200)
rg, tg = np.meshgrid(r,t)
c = rg * np.sin(tg)

# If I remove the projection="polar" argument here the 
ax = plot.subplot2grid((1, 1), (0, 0), projection="polar", aspect=1.)
im = ax.pcolormesh(t, r, c.T)
divider = axes_grid1.make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
plot.colorbar(im, cax=cax)
plot.show()

推荐答案

在执行此操作时, cax 轴实际上处于 polar 投影中.您可以通过以下方式进行验证:

In the way you are doing it, the cax axis is actually in polar projection. You can verify it by:

cax = divider.append_axes("right", size="200%", pad=0.5)
#plot.colorbar(im, cax=cax)
cax.pcolormesh(t, r, c.T)

虽然这可能是一个错误,但我认为实现它的更简洁的方法可能是使用 GridSpec:

While this might be a bug, I think a cleaner way to achieve it might be to use GridSpec:

gs = gridspec.GridSpec(1, 2,
                       width_ratios=[10,1],
                       )

ax1 = plt.subplot(gs[0], projection="polar", aspect=1.)
ax2 = plt.subplot(gs[1])

t = np.linspace(0.0, 2.0 * np.pi, 360)
r = np.linspace(0,100,200)
rg, tg = np.meshgrid(r,t)
c = rg * np.sin(tg)

im = ax1.pcolormesh(t, r, c.T)
plot.colorbar(im, cax=ax2)

这篇关于使用极坐标投影向 pcolormesh 添加颜色条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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