Colorbar粘在轴上

时间:2014-08-12 13:34:05

标签: python matplotlib

我使用matplotlib定位颜色条时遇到了一个小问题。我正在绘制几个子图,其中一个是图像。我希望这个图像有一个颜色条,但我希望它“粘住”到图形上,这样两个轴之间就没有空间(图中的那个和颜色条中的那个)。即使调整了图形,颜色条也应始终粘在图像轴上。

PS - 我不介意ax3(我的图像的轴)是否变形。

这就是我现在所拥有的:

# Imports
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from scipy import *

# Generating data
data = (rand(2048,2048), rand(2048,2048)+1000)
colorplot = "blue"
metadata = ("Test1", "Test2", "spectral")

# Generating figure and subplots
fig = plt.figure()
fig.subplots_adjust(right = 0.8)

gs1 = gridspec.GridSpec(3, 5)
gs1.update(left=0.05,\
           right=0.95,\
           top=0.95,\
           bottom=0.05,\
           wspace=0.2,\
           hspace=0.05)

ax1 = fig.add_subplot(gs1[0,0])
ax2 = fig.add_subplot(gs1[0,1])
ax3 = fig.add_subplot(gs1[1:3,0:2])
ax4 = fig.add_subplot(gs1[:,2:])

list_axes = [ax1, ax2, ax3, ax4]     

for i in list_axes:
    i.autoscale_view(tight=False, scalex=False, scaley=True)

# Misc computation
array = data[1]-data[0]
mean_value = np.mean(array)
std_value = np.std(array)
nb_sigma = 5

ax1.imshow(data[0], interpolation = "nearest", cmap = metadata[2])
ax2.imshow(data[1], interpolation = "nearest", cmap = metadata[2])

im = ax3.imshow(array, vmin = np.min(array[array>mean_value-nb_sigma*std_value]), vmax = np.max(array[array<mean_value+nb_sigma*std_value]), interpolation = "nearest", cmap = metadata[2])

ax3.set_adjustable('box-forced')
# Creating axes for the colorbar
axes_cb = fig.add_axes([ax3.get_position().bounds[0],ax3.get_position().bounds[1], ax3.get_position().bounds[2], 0.05])
fig.colorbar(im, cax = axes_cb, orientation = 'horizontal')
axes_cb.yaxis.tick_left()

n, bins, patches = ax4.hist(array.flatten(), color = colorplot, bins = 50, normed = True)

plt.show()

谢谢!

0 个答案:

没有答案