matplotlib颜色条更改轴大小 [英] matplotlib colorbar change axis size

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

问题描述

我有一个函数(在代码中请参见plot_fualt_axis()),在给定的轴上绘制矩形补丁,我试图在图形上绘制3个这样的轴,并在图形上添加一个颜色条.当我尝试添加颜色条时,它正在更改相邻轴的大小,但找不到任何围绕它的方法.

我的代码

从inversion_utilities导入

  *将numpy导入为np导入matplotlib.pyplot作为plt从mpl_toolkits.axes_grid1导入make_axes_locatableinv = build_inversion('resample_test/resample_test')grad_solutions = np.load('结果/noise_test/grad_1000.npy')grad_std = np.std(grad_solutions,0)cn_solutions = np.load('结果/noise_test/resample_1000.npy')cn_std = np.std(cn_solutions,0)uniform_solutions = np.load('结果/noise_test/uniform_1000.npy')uniform_std = np.std(uniform_solutions,0)f,axarr = plt.subplots(3,图大小=(15,10))plot_fualt_axis(inv.plains,axarr [0],uniform_std,False)plot_fualt_axis(inv.plains,axarr [1],grad_std,False)plot_fualt_axis(inv.plains,axarr [2],cn_std,False)分隔线= make_axes_locatable(axarr [2])cax = Divider.append_axes("bottom",size ="10%",pad = 0.3)规范= matplotlib.colors.Normalize(0,0.001)my_cmap = cm.get_cmap('jet')cmmapable = cm.ScalarMappable(规范,my_cmap)cmmapable.set_array(np.linspace(0,0.001))色彩条(cmmapable,orientation ='horizo​​ntal',cax = cax)plt.show() 

从中我得到这个

I have a function (plot_fualt_axis() see in the code) that plotting rectangle paches on a given axis, I'm trying to plot 3 such an axis on a figure and add a colorbar to the figure. when I'm trying to add the colorbar it is changing the size of the adjacent axis and I didn't find any way around it.

my code

from inversion_utilities import *
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable


inv = build_inversion('resample_test/resample_test')
grad_solutions = np.load('results/noise_test/grad_1000.npy')
grad_std = np.std(grad_solutions, 0)
cn_solutions = np.load('results/noise_test/resample_1000.npy')
cn_std = np.std(cn_solutions, 0)
uniform_solutions = np.load('results/noise_test/uniform_1000.npy')
uniform_std = np.std(uniform_solutions, 0)
f, axarr = plt.subplots(3, figsize=(15, 10))
plot_fualt_axis(inv.plains, axarr[0], uniform_std, False)
plot_fualt_axis(inv.plains, axarr[1], grad_std, False)
plot_fualt_axis(inv.plains, axarr[2], cn_std, False)
divider = make_axes_locatable(axarr[2])
cax = divider.append_axes("bottom", size="10%", pad=0.3)
norm = matplotlib.colors.Normalize(0, 0.001)
my_cmap = cm.get_cmap('jet')
cmmapable = cm.ScalarMappable(norm, my_cmap)
cmmapable.set_array(np.linspace(0, 0.001))
colorbar(cmmapable, orientation='horizontal', cax=cax)
plt.show()

from that i get this figure where the two top axis both larger than the bottem one. how do i make all of them be the same size?

解决方案

I was not familiar with make_axes_locatable, but from the documentation it seems to be dividing up your axarr[2] to make space for the colorbar. You can add a colorbar axes to the figure directly. Obviously I can not reproduce your plots but here is an example using contour plots:

import matplotlib.pyplot as plt
import numpy as np

# Create some fake data and define levels for a contour plot.
a = np.arange(12)[:,np.newaxis] * np.ones(8)
levels = np.arange(1.5, 10, 2)

# Create figure and axes array.
f, axarr = plt.subplots(3, figsize=(15, 10))

# Make three identical contour plots.
for i in range(3):
    plt.sca(axarr[i])
    myplot = plt.contourf(a, cmap='RdYlBu', levels=levels, extend='both')

# Find position of last contour plot and use this to set position of new
# colorbar axes.    
left, bottom, width, height = axarr[2].get_position().bounds
cax = f.add_axes([left, 0.03, width, height * 0.1])
plt.colorbar(myplot, orientation='horizontal', cax=cax)

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

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