Exemplo n.º 1
0
    def explain_all(self,
                    pat='',
                    stacked=True,
                    kind='bar',
                    top=0.9,
                    title='',
                    use='level',
                    threshold=0.0,
                    resample='',
                    axvline=None):
        if self.go:
            years = mdates.YearLocator()  # every year
            months = mdates.MonthLocator()  # every month
            years_fmt = mdates.DateFormatter('%Y')

            selected = GetAllImpact(self.res[use], pat)
            grouped = selected.stack().groupby(level=[0])
            fig, axis = plt.subplots(nrows=len(grouped),
                                     ncols=1,
                                     figsize=(10, 5 * len(grouped)),
                                     constrained_layout=False)
            width = 0.5  # the width of the barsser
            ntitle = f'Attribution, {use}' if title == '' else title
            laxis = axis if isinstance(axis, numpy.ndarray) else [axis]
            for j, ((name, dfatt), ax) in enumerate(zip(grouped, laxis)):
                dfatt.index = [i[1] for i in dfatt.index]
                if resample == '':
                    tempdf = mc.cutout(dfatt.T, threshold).T
                else:
                    tempdf = mc.cutout(dfatt.T,
                                       threshold).T.resample(resample).mean()
#                pdb.set_trace()
                tempdf.plot(ax=ax,
                            kind=kind,
                            stacked=stacked,
                            title=self.desdic.get(name, name))
                ax.set_ylabel(name, fontsize='x-large')
#                ax.set_xticklabels(tempdf.index.tolist(), rotation = 45,fontsize='x-large')
##                ax.xaxis.set_minor_locator(plt.NullLocator())
##                ax.tick_params(axis='x', labelleft=True)
#                ax.xaxis.set_major_locator(years)
#                ax.xaxis_date()
#                ax.xaxis.set_major_formatter(years_fmt)
#                ax.xaxis.set_minor_locator(months)
#                ax.tick_params(axis='x', labelrotation=45,right = True)
            if type(axvline) != type(None): axis.axvline(axvline)
            fig.suptitle(ntitle, fontsize=20)
            if 1:
                plt.tight_layout()
                fig.subplots_adjust(top=top)

            return fig
Exemplo n.º 2
0
def water(serxinput,sort=False,ascending =True,autosum=False,allsort=False,threshold=0.0):
    ''' Creates a dataframe with information for a watrfall diagram 
    
    :serx:  the input serie of values
    :sort:  True if the bars except the first and last should be sorted (default = False)
    :allsort:  True if all bars should be sorted (default = False)
    :autosum:  True if a Total bar are added in the end  
    :ascending:  True if sortorder = ascending  
    
    Returns a dataframe with theese columns:
    
    :hbegin: Height of the first bar
    :hend: Height of the last bar
    :hpos: Height of positive bars
    :hneg: Height of negative bars
    :start: Ofset at which each bar starts 
    :height: Height of each bar (just for information)
    '''
    # get the height of first and last column 
    
   
    total=serxinput.sum() 
    serx = mc.cutout(serxinput,threshold)
    if sort or allsort :   # sort rows except the first and last
        endslice   = None if allsort else -1
        startslice = None if allsort else  1
        i = serx[startslice:endslice].sort_values(ascending =ascending ).index
        if allsort:            
            newi =i.tolist() 
        else:
            newi =[serx.index.tolist()[0]]  + i.tolist() + [serx.index.tolist()[-1]]# Get the head and tail 
        ser=serx[newi]
    else:
        ser=serx.copy()
        
    if autosum:
        ser['Total'] = total
        
    hbegin = ser.copy()
    hbegin[1:]=0.0
    hend   = ser.copy()
    hend[:-1] = 0.0
         
    
    height = ser 
    start = ser.cumsum().shift().fillna(0.0)  # the  starting point for each bar
    start[-1] = start[-1] if allsort and not autosum else 0     # the last bar should start at 0
    end = start + ser

    hpos= height*(height>=0.0)
    hneg= height*(height<=0.0)
    dfatt = pd.DataFrame({'start':start,'hbegin':hbegin,'hpos':hpos,'hneg':hneg,'hend':hend,'height':height}).loc[ser.index,:]

    return dfatt