Coolcrab
Coolcrab

Reputation: 2723

Python Fix colorbar in plot

I am trying to get rid of the overlap of the images and colorbar values on the right side, but nothing seems to work. I have tried the 'tight' command and to shrink the size of the color bar text. The former does not help and the latter does not seem to work at all. (Is this possible?) I need a way to read these numbers, it does not really matter how. (As long as they don't overlap)

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

Upvotes: 0

Views: 798

Answers (2)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339745

You may look at the colorbar-whose-height-or-width-in-sync-with-the-master-axes-example.

The idea is to cut a bit of the axes using mpl_toolkits.axes_grid1.make_axes_locatable and create a new axes into which the colorbar can be placed, using 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

Upvotes: 2

mauve
mauve

Reputation: 2763

I've found it easier to 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

Upvotes: 0

Related Questions