0

I'd like to draw a colorbar beside one of my subplots using axes_grid1. The problem is, that the call of make_axes_locatable manipulates my subplot so that it doesn't have the same size of the other plots anymore.

Here is a minimal working example of my issue.

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

fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(15,5))
_ax = ax[0,:].flatten('C')
values_x = np.linspace(0, 2 * np.pi, 100)
values_y = np.sin(values_x)
vmax = np.amax(values_x)
vmin = np.amin(values_x)
s = 1
ticks_rotation = 10
ticks_num = 5
_ax = ax[0,:].flatten('C')
plot_w = _ax[0].scatter(values_x, values_y, c=values_x, cmap='rainbow', vmin=vmin, vmax=vmax, s=s)
plot_v = _ax[1].scatter(values_x, values_y, c=values_x, cmap='rainbow', vmin=vmin, vmax=vmax, s=s)
_ax = ax[1,:].flatten('C')
plot_w = _ax[0].scatter(values_x, values_y, c=values_x, cmap='rainbow', vmin=vmin, vmax=vmax, s=s)
plot_v = _ax[1].scatter(values_x, values_y, c=values_x, cmap='rainbow', vmin=vmin, vmax=vmax, s=s)

divider = make_axes_locatable(_ax[1])
cax = divider.append_axes('right', size='5%', pad=0.05)
v_delta = (vmax - vmin)*0.1
ticks = np.linspace(vmin+v_delta,vmax-v_delta,ticks_num)
cbar = fig.colorbar(plot_v, cax=cax, orientation='vertical', ticks=ticks)
cbar.ax.set_yticklabels(np.around(ticks,decimals=1), rotation=ticks_rotation,rotation_mode='default')
plt.show()

enter image description here

What I want is actually all subplots of the same size with a nice color beside it. Any suggestions?

Edit: I really just want to have the colorbar besides the lower right subplot, and not like this.

Tik0
  • 2,499
  • 4
  • 35
  • 50
  • Have a look at [this](https://matplotlib.org/2.0.2/mpl_toolkits/axes_grid/users/overview.html)? Specifically the function `demo_grid_with_single_cbar(fig)` – Rick M. Sep 18 '18 at 09:59
  • Yes, I already had a look at this but without any satisfactory results (or maybe it is me). Btw. I haven't found an explanation for `demo_grid_with_single_cbar` on this side. – Tik0 Sep 18 '18 at 10:22

1 Answers1

1

If you are not attached to using axes_grid1, I personally prefer to use a GridSpec for this kind of jobs.

gridspec is a module which specifies the location of the subplot in the figure.

GridSpec

specifies the geometry of the grid that a subplot will be placed. The number of rows and number of columns of the grid need to be set. Optionally, the subplot layout parameters (e.g., left, right, etc.) can be tuned.

import matplotlib.gridspec as gs

gs0 = gs.GridSpec(2, 3, width_ratios=[20,20,1])
fig = plt.figure(figsize=(10,4))
ax1 = fig.add_subplot(gs0[0,0])
ax2 = fig.add_subplot(gs0[0,1])
ax3 = fig.add_subplot(gs0[1,0])
ax4 = fig.add_subplot(gs0[1,1])
cax = fig.add_subplot(gs0[:,2])


values_x = np.linspace(0, 2 * np.pi, 100)
values_y = np.sin(values_x)

for ax in [ax1,ax2,ax3,ax4]:
    plot_w = ax.scatter(values_x, values_y, c=values_x, cmap='rainbow', s=1)

cbar = fig.colorbar(plot_w, cax=cax, orientation='vertical')

enter image description here

Diziet Asahi
  • 38,379
  • 7
  • 60
  • 75