0

I have been struggling for days with what comes below. I plotted a bar chart with y-axis value of interest (in my case 40602) and then draw a horizontal line based on this value as well as confidence interval. Now I need to color the bar based on this value and the confidence interval, using a color scale based on the below rule: red if they are definitely above this value (given the confidence interval), blue if they are definitely below this value, or white if they contain this value. Please see my code below. I don't know if the issue comes from my function "getColor" with the if else statement, or if it is an issue of mapping this function to the plotting function plt.colorbar. What am I missing or doing wrong here?

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from math import sqrt
import matplotlib.cm as cm
from matplotlib.colors import Normalize
        
np.random.seed(12345)
df = pd.DataFrame([np.random.normal(32000,200000,3650), 
                       np.random.normal(43000,100000,3650), 
                       np.random.normal(43500,140000,3650), 
                       np.random.normal(48000,70000,3650)], 
                      index=[1992,1993,1994,1995])

x = df.index
y = df.mean(axis=1)

df_means = pd.DataFrame({'1992':[np.random.normal(32000,200000,3650).mean() for i in range(1000)],
                        '1993':[np.random.normal(43000,100000,3650).mean() for i in range(1000)],
                        '1994':[np.random.normal(43500,140000,3650).mean() for i in range(1000)],
                        '1995':[np.random.normal(48000,70000,3650).mean() for i in range(1000)]})
y_mean= np.mean(y)
df_std = df.std(axis=1)/np.sqrt(df.shape[1])
yerr = df_std*1.96

def getColor(val, mean, ci):
    if val <= (mean-ci):
        return 'darkred'
    elif (mean-ci) < val and val < mean:
        return 'red'
    elif val==mean:
        return 'white'
    elif mean< val and val < (mean+ci):
        return 'blue'
    else:
        return 'darkblue'

#plt.figure()
fig, ax=plt.subplots()
#plt.figure(figsize=(8,8))
ax.axhline(y=y_mean, color="black", linestyle='--', linewidth=0.5)
plt.bar(x, y, tick_label=x, yerr=df_means.std(axis=0),capsize=10)#, color=colormap(getColor)color=colormap(colors)
#cbar = plt.colorbar(plt.cm.ScalarMappable(cmap=colormap), orientation='horizontal')
plt.colorbar(plt.get_cmap(getColor), orientation='horizontal')

#cbar.set_label('Color', labelpad=5)
plt.show()

The only part not work in my output is the coloring part:

enter image description here

thank you for your help!

Mr. T
  • 11,960
  • 10
  • 32
  • 54
  • [colorbar](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.colorbar.html) doesn't color your bars. – Jody Klymak Feb 01 '22 at 12:14
  • 1
    Does this answer your question? [vary the color of each bar in bargraph using particular value](https://stackoverflow.com/questions/18903750/vary-the-color-of-each-bar-in-bargraph-using-particular-value) – Jody Klymak Feb 01 '22 at 12:15

0 Answers0