1

The issue

Hello, I try to create a figure with two sub-plots in one row and a color bar next to it. That works, but my issue is that I don't find a way to create two equally side sub plots and a slim color bar next to them.

My first attempt was to try:

import matplotlib.pyplot as plt
import numpy as np
X, Y = np.meshgrid([0.05, 0.1, 0.2, 0.3], [0,1,3,5])
fig, (ax1, ax2) = plt.subplots(figsize=(10,4), ncols = 2, sharey = True)
CS_18_14 = ax1.contour(X, Y, Bene_R18_14, levels, linewidths=2)
ax1.set_xlabel('level')
ax1.set_title('gross benefit 2')
ax1.set_ylabel('test')

CS_18_28 = ax2.contour(X, Y, Bene_R18_28, levels, linewidths=2, linestyles = '--')
ax2.set_xlabel('level')
ax2.set_title('gross benefit 2')
CB = plt.colorbar(CS_18_14, shrink=0.8)
plt.show()

The color bar is ok, but the second sub-plot is squished. 1st not correct example

And the second was:

fig = plt.figure(figsize=(10,4))
ax1 = plt.subplot2grid((1,13), (0,0), colspan = 6)
ax2 = plt.subplot2grid((1,13), (0,6), colspan = 6, adjustable='box-forced', sharex = ax1, sharey= ax1)
axCB = plt.subplot2grid((1,13), (0,12), colspan = 1)
CS_18_14 = ax1.contour(X, Y, Bene_R18_14, levels, linewidths=2)
ax1.set_xlabel('level')#r'proof load level in quantile of $\mu(R)$')
ax1.set_title('gross benefit 1'))
ax1.set_ylabel('test')

CS_18_28 = ax2.contour(X, Y, Bene_R18_28, levels, linewidths=2, linestyles = '--')
ax2.set_xlabel('level')
ax2.set_title('gross benefit 2')
CB = fig.colorbar(CS_18_14, cax = axCB)
plt.show()

This results in a plot that is too wide and the color bar is also not great: 2nd not correct example

The question

How do I get a rather nice color bar like in example 1, but also sub-plots with identical size?

Cheers

Community
  • 1
  • 1
Henning
  • 95
  • 1
  • 1
  • 7

0 Answers0