Matching graph sizes in matplotlib using make_axes_locatable-> divider colorbars

I have 4 charts in a 2x2 grid, but I only need the two on the right to have bars (the scale is applied line by line). I use the form

divider = make_axes_locatable(ax) cax = divider.append_axes("right", size="5%", pad=0.05) plt.colorbar(im, cax=cax) 

so that the color panels fit the size of these two graphs. However, this makes two with color panels of different sizes from two without, which both look bad and exclude the possibility of placing the axis only at the outer edges. I tried just not to have a colorbar call (with a separator call), but of course this leaves an empty white box and numbers on the side. How to get all charts with and without colored banks to have a consistent size without putting bars on all of them?

+4
source share
1 answer

Use GridSpec to create your own set of subtasks, then add a colored row to the last column.

Example:

 import numpy as np import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec fig = plt.figure() gs = gridspec.GridSpec(2, 3, width_ratios=[10,10,1]) ax = np.empty([2,3],dtype=np.object) im = np.empty([2,2],dtype=np.object) for m in range(2): for n in range(2): ax[m,n] = plt.subplot(gs[m,n]) im[m,n] = ax[m,n].imshow(np.random.rand(10,10)) for i in range(2): ax[i,2] = plt.subplot(gs[i,2]) plt.colorbar(im[i,1],cax=ax[i,2]) plt.show() 

See also: Python / Matplotlib - Resize Subtitle Relative Size

+1
source

Source: https://habr.com/ru/post/1496868/


All Articles