Home > Mobile >  How to decrease the space between grouped bar-subplots in matplotlib?
How to decrease the space between grouped bar-subplots in matplotlib?

Time:05-08

import matplotlib
import matplotlib.pyplot as plt
import numpy as np

a_means, a_CI = (70, 60), (2.7, 1.9)
b_means, b_CI = (85, 83), (2.6, 1.2)
c_means, c_CI = (66, 64), (3.5, 1.8)

ind = np.arange(2)
width = 0.35

fig, ax = plt.subplots(figsize=(10, 4), facecolor='w', edgecolor='k')
rects1 = ax.bar(ind - 2*width/4, a_means, width/4, yerr=a_CI, label='a')
rects2 = ax.bar(ind - width/4, b_means, width/4, yerr=b_CI, label='b')
rects3 = ax.bar(ind, c_means, width/4, yerr=c_CI, label='c')

ax.set_xticks(ind)
ax.set_ylim([50, 100])
ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.1),
          fancybox=True, shadow=True, ncol=3)

plt.show()

This is what I get: old

This is what I want to get: new

Any ideas how I can achieve this? Thank you in advance!

CodePudding user response:

You can change ind = np.arange(2) to something like:

ind = np.arange(2) / 2.5

Play with the number, then you can eventually change tick labels if they don't satisfy your needs.

CodePudding user response:

Make the bars wider (you can do this easily since you already have a width variable for this) and the figure size narrower:

width = 1

fig, ax = plt.subplots(figsize=(6, 4), facecolor='w', edgecolor='k')
  • Related