matplotlib颜色条的位置和大小 [英] matplotlib colorbar placement and size

查看:253
本文介绍了matplotlib颜色条的位置和大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用quadmesh创建一个简单的极坐标投影图.这是一个最小的脚本,基本上可以生成我想做的事情:

I'm using quadmesh to create a simple polar projection plot. Here's a minimal script which produces basically what I'm trying to do:

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:

我希望颜色条:

  1. 不与6个标签重叠.
  2. 按比例缩放,使其与图的高度大致相同.

是否有任何技巧可以使其正常工作? (请注意,这种布局并不是我将要使用的唯一布局-例如,我可能使用1x2布局或4x4布局...似乎应该有某种方法可以将颜色栏缩放到与相关情节...)

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...)

推荐答案

无论显示大小如何,这种组合(以及与之接近的值)似乎对我来说都是神奇"的,可以使颜色条按比例缩放.

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)

这篇关于matplotlib颜色条的位置和大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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