Python修复了情节

时间:2017-06-20 13:52:42

标签: python matplotlib

我试图摆脱右侧图像和颜色条值的重叠,但似乎没有任何效果。我尝试过'tight'命令并缩小彩条文本的大小。前者没有帮助,后者似乎根本不起作用。 (这可能吗?)我需要一种方法来阅读这些数字,这并不重要。 (只要它们不重叠)

enter image description here

gs = gridspec.GridSpec(1, 2)
gs0 = gridspec.GridSpecFromSubplotSpec(3, 3, subplot_spec=gs[0])
gs1 = gridspec.GridSpecFromSubplotSpec(2, 2, subplot_spec=gs[1])

fig = plt.figure()


ax = fig.add_subplot(gs0[0, 0])
plt.imshow(getpoly(seg1),origin="lower")
ax.set_xticks([]); ax.set_yticks([])
ax = fig.add_subplot(gs0[0, 1])
plt.imshow(getpoly(seg2),origin="lower")
ax.set_xticks([]); ax.set_yticks([])
ax = fig.add_subplot(gs0[0, 2])
plt.imshow(getpoly(seg3),origin="lower")
ax.set_xticks([]); ax.set_yticks([])
ax = fig.add_subplot(gs0[1, 0])
plt.imshow(getpoly(seg4),origin="lower")
ax.set_xticks([]); ax.set_yticks([])
ax = fig.add_subplot(gs0[1, 1])
plt.imshow(getpoly(seg5),origin="lower")
ax.set_xticks([]); ax.set_yticks([])
ax = fig.add_subplot(gs0[1, 2])
plt.imshow(getpoly(seg6),origin="lower")
ax.set_xticks([]); ax.set_yticks([])
ax = fig.add_subplot(gs0[2, 1])
plt.imshow(getpoly(seg7),origin="lower")
ax.set_xticks([]); ax.set_yticks([])
ax = fig.add_subplot(gs0[2, 2])
plt.imshow(getpoly(seg8),origin="lower")
ax.set_xticks([]); ax.set_yticks([])
ax = fig.add_subplot(gs0[2, 0])
plt.imshow(getpoly(seg9),origin="lower")
ax.set_xticks([]); ax.set_yticks([])



ax = fig.add_subplot(gs1[0, 0])
plt.imshow(h1,origin="lower")
plt.colorbar(fraction=0.046, pad=0.04)
ax.set_xticks([]); ax.set_yticks([])

ax = fig.add_subplot(gs1[0, 1])
plt.imshow(h2,origin="lower")
plt.colorbar(fraction=0.046, pad=0.04)
ax.set_xticks([]); ax.set_yticks([])

ax = fig.add_subplot(gs1[1, 0])
plt.imshow(getpoly(h2),origin="lower")
plt.colorbar(fraction=0.046, pad=0.04)  
ax.set_xticks([]); ax.set_yticks([])

ax = fig.add_subplot(gs1[1, 1])
plt.imshow(h1-getpoly(h2),origin="lower")
plt.colorbar(fraction=0.046, pad=0.04)
ax.set_xticks([]); ax.set_yticks([])

plt.tight_layout()

2 个答案:

答案 0 :(得分:2)

您可以查看colorbar-whose-height-or-width-in-sync-with-the-master-axes - 示例。

我们的想法是使用mpl_toolkits.axes_grid1.make_axes_locatable剪切一些轴,然后使用fig.colorbar(im, cax=cax)创建一个可以放置颜色条的新轴。

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

gs = gridspec.GridSpec(1, 2)
gs0 = gridspec.GridSpecFromSubplotSpec(3, 3, subplot_spec=gs[0])
gs1 = gridspec.GridSpecFromSubplotSpec(2, 2, subplot_spec=gs[1])

fig = plt.figure()

for i in range(9):
    ax = fig.add_subplot(gs0[i//3, i%3])
    ax.imshow(np.random.rand(4,4))
    ax.set_xticks([]); ax.set_yticks([])


for i in range(4):
    ax = fig.add_subplot(gs1[i//2, i%2])
    im = ax.imshow(np.random.rand(4,4))
    ax.set_xticks([]); ax.set_yticks([])
    divider = make_axes_locatable(ax)
    cax = divider.append_axes("right", size="5%", pad=0.05)
    fig.colorbar(im, ax=ax, cax=cax)

plt.show()

enter image description here

答案 1 :(得分:0)

我发现add the colorbar separately

更容易了
    cax = fig.add_axes([0.125, 0.925, 0.775, 0.0725])   
    #the numbers in fig.add_axes are all percentages
    norm = mpl.colors.Normalize(vmin=low_val, vmax=high_val)
    mpl.colorbar.ColorbarBase(cax, cmap='rainbow', norm=norm, orientation='horizontal')

fig.add_axes details