簡體   English   中英

matplotlib colorbar更改軸大小

[英]matplotlib colorbar change axis size

我有一個函數(plot_fualt_axis()在代碼中看到)繪制給定軸上的矩形paches,我試圖在圖上繪制3個這樣的軸,並在圖中添加一個顏色條。 當我試圖添加顏色條時,它正在改變相鄰軸的大小,我沒有找到任何方法。

我的代碼

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

從那里我得到這個數字 ,其中兩個頂部軸都大於bottem一個。 我如何使他們所有的大小相同?

我不熟悉make_axes_locatable ,但是從文檔中看來它似乎正在划分你的axarr[2]以為colorbar騰出空間。 您可以直接在圖中添加顏色條軸。 顯然我無法重現你的情節,但這里是一個使用等高線圖的例子:

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)

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM