Home > Blockchain >  matplotlib stacked bar chart change position of error bar
matplotlib stacked bar chart change position of error bar

Time:01-07

The error bars overlap in my stacked bar chart. Is there a way to change the x position of the error bar but keep the main bars at the same position?

stacked bar chart

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
import numpy as np
from statistics import mean, stdev, median

colors3 = ['#7ac0f8', '#2196f3', '#1a78c2']
width = 0.6
results = {'Group 1': {'Type A': [24.21, 32.08], 'Type B': [11.35, 6.59], 'Type C': [45.64, 21.87]}, 'Group 2': {'Type A': [19.41, 17.39], 'Type B': [10.16, 8.72], 'Type C': [21.25, 11.57]}, 'Group 3': {'Type A': [11.4, 9.75], 'Type B': [5.73, 6.98], 'Type C': [6.4, 13.38]}}
types = ['Type A', 'Type B', 'Type C']

bottom = [0, 0, 0]
fig, ax = plt.subplots()

for i in range(0, len(types)):
    means = list(map(lambda x: results[x][types[i]][0], results.keys()))
    errs = list(map(lambda x: results[x][types[i]][1], results.keys()))
    ax.bar(results.keys(), means, width, yerr=errs, label=types[i], bottom=bottom, color=colors3[i], error_kw=dict(capsize=5))
    for k in range(0, len(means)):
        bottom[k] = bottom[k]   means[k]

ax.grid(True)
ax.set_axisbelow(True)
plt.legend(types, loc='upper right')
#plt.savefig('img/StackOverflow.png', bbox_inches='tight', dpi=300)
plt.show()

CodePudding user response:

Do this by plotting the bars on their own, then plot the errorbars separately, using enter image description here

  • Related