waterfalls graph for two data frame in pandas

126 views Asked by At

I have two data frames which have totally the same rows and columns. df1 shows the percentage % of Mode :

                    pt      car     bike    walk
Equilibrium         28.80   36.82   3.55    30.83
No information      28.80   36.82   3.55    30.83
start               28.82   36.83   3.55    30.80
Equilibrium2        28.51   36.95   3.56    30.98

and df2 is Travel Time(minutes)

                    pt          car         bike    walk
Equilibrium         384651.50   216673.23   24136.57    88602.10
No information      397068.27   216640.15   24133.03    88565.93
start               386008.27   216664.17   24136.57    88521.93
Equilibrium2        383788.73   215751.85   26638.87    89602.90

I want to have such a following diagram that x-axis shows the percentage of Mode (df1) and the Y-axis shows the Travel Time (df2) for each column name such as: enter image description here

1

There are 1 answers

8
Quang Hoang On BEST ANSWER

Let's try plotting the bars with bottoms:

colors = [f'C{i}' for i in range(4)]
fig, axes = plt.subplots(2,2, figsize=(8,8))

for idx, ax in zip(df1.index, axes.ravel()):
    s1, s2 = df1.loc[idx].cumsum(),df2.loc[idx].cumsum()
    ax.bar(s1.shift(fill_value=0),df2.loc[idx],width=df1.loc[idx],
            bottom=s2.shift(fill_value=0), align='edge', color=colors)
    ax.set_title(idx)        

fig.tight_layout()

Output:

enter image description here