import pandas as pd
import numpy as np
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])
df
from scipy import stats
year_avg = df.mean(axis=1)
year_std = df.std(axis=1)
yerr = year_std / np.sqrt(df.shape[1]) * stats.t.ppf(1 - 0.05/2, df.shape[1]-1)
import matplotlib.pyplot as plt
plt.figure()
bars = plt.bar(range(df.shape[0]), year_avg, yerr = yerr, capsize=15, color = 'lightgrey')
plt.show()
yerr
year_avg
fig = plt.gcf()
threshold = 42000
plt.axhline(y = threshold, color = 'red', alpha = 0.7)
import matplotlib.colors as mcol
import matplotlib.cm as cm
cm1 = mcol.LinearSegmentedColormap.from_list("MyCmapName", ["darkblue","white","darkred"])
cpick = cm.ScalarMappable(cmap=cm1)
cpick.set_array([])
cpick
percentages = []
for bar, yerr_ in zip(bars, yerr):
low = bar.get_height() - yerr_
high = bar.get_height() + yerr_
percentage = (high - threshold) / (high - low)
if percentage > 1: percentage = 1
if percentage < 0: percentage = 0
percentages.append(percentage)
percentages
bars[0].get_height()