|
|
|
import pandas |
|
import numpy |
|
import matplotlib.pyplot as plt |
|
|
|
def run_fun(): |
|
numpy.random.seed(seed = 69) |
|
dat = pandas.DataFrame(numpy.random.randn(5, 2), |
|
index = [letter for letter in 'abcde'], |
|
columns = [0, 1]) |
|
|
|
color_a = ["#7F8392", "#B08B47", "#52A479", "#C6778A", "#CF7B5E"] |
|
color_b = ["#829554", "#A78071", "#AD8AB2", "#679486", "#65A1BC"] |
|
|
|
fig = plt.figure() |
|
ax = plt.subplot2grid((1,1), (0,0)) |
|
|
|
stacked_barchart(ax, 0, dat.loc[:, 0], color_a) |
|
stacked_barchart(ax, 1, dat.loc[:, 1], color_b) |
|
ax.plot([.25, .75], [0., 2.2], color = '#353535', ls = '--') |
|
ax.plot([.25, .75], [-1.6, -1.2], color = '#353535', ls = '--') |
|
|
|
def stacked_barchart(ax, tick, series, colors): |
|
neg_sum = series[series < 0].sum() |
|
pos_sum = 0. |
|
for i, val in enumerate(series): |
|
if val < 0: |
|
neg_sum -= val |
|
ax.bar(tick, val, bottom = neg_sum, color = colors[i], |
|
align = 'center', width = 0.5, |
|
lw = .25, label = series.index[i]) |
|
else: |
|
ax.bar(tick, val, bottom = pos_sum, align = 'center', |
|
width = 0.5, color = colors[i], |
|
label = series.index[i], lw = .25) |
|
pos_sum += val |
|
return ax |