Waterfall plot python?

后端 未结 4 1407
独厮守ぢ
独厮守ぢ 2020-12-05 05:23

Is there a python module that will do a waterfall plot like MATLAB does? I googled \'numpy waterfall\', \'scipy waterfall\', and \'matplotlib waterfall\', but did not find a

4条回答
  •  情话喂你
    2020-12-05 05:39

    The Wikipedia type of Waterfall chart one can obtain also like this:

    import numpy as np
    import pandas as pd
    
    def waterfall(series):
        df = pd.DataFrame({'pos':np.maximum(series,0),'neg':np.minimum(series,0)})
        blank = series.cumsum().shift(1).fillna(0)
        df.plot(kind='bar', stacked=True, bottom=blank, color=['r','b'])
        step = blank.reset_index(drop=True).repeat(3).shift(-1)
        step[1::3] = np.nan
        plt.plot(step.index, step.values,'k')
    
    test = pd.Series(-1 + 2 * np.random.rand(10), index=list('abcdefghij'))
    waterfall(test)
    

提交回复
热议问题