//  [python]
def plot_probability_distance_from_clusters(distance_dict, outpng = 'bhattacharyya.png', set_inf_to=5):
    
    df = pd.DataFrame.from_dict(distance_dict)
    
    #set inf values
    df.replace(inf, set_inf_to, inplace=True)
    df_color_dict = {}
    
    for n in df.columns:
        df_color_dict[n] = sum(df[n])

    df['position'] = range(1,df.shape[0]+1)
    df = df.set_index('position')

    totals_dict = {}
    for name in df.columns:
        total = sum(df[name])
        totals_dict[name] = total
    sorted_totals_dict = {k: v for k, v in sorted(totals_dict.items(), key=lambda item: item[1])}

    min_total = min(totals_dict.values())
    max_total = max(totals_dict.values())
        
    color_dict = {}
    cmap = matplotlib.cm.get_cmap('coolwarm')
    
    fig = plt.gcf()
    fig.set_size_inches(6, 3)
    max_bd = 0.0
    for name in sorted_totals_dict.keys():
        rel_total = (totals_dict[name] - min_total)/(max_total - min_total)
        color_dict[name] = cmap(rel_total)
        ax = plt.plot(df[name], color=color_dict[name])
        max_new_bd = max(list(df[name]))
        if max_new_bd > max_bd and (max_new_bd != np.Infinity):
            max_bd = max_new_bd
    lgd = fig.legend(sorted_totals_dict.keys(), bbox_to_anchor=(1.35, 1))

    plt.xlabel('Amino acid position in loop')
    plt.ylabel('distance')
    plt.ylim(0, min(math.ceil(max_bd + 0.1), 5.5))
    plt.tight_layout()
    
    fig.savefig(outpng, dpi=300, bbox_extra_artists=(lgd,), bbox_inches='tight')
    plt.close()

    #rainbow
    cmap = matplotlib.cm.get_cmap('rainbow')
    fig = plt.gcf()
    fig.set_size_inches(6, 3)
    max_bd = 0.0
    for name in sorted_totals_dict.keys():
        rel_total = (totals_dict[name] - min_total)/(max_total - min_total)
        color_dict[name] = cmap(rel_total)
        ax = plt.plot(df[name], color=color_dict[name])
        max_new_bd = max(list(df[name]))
        if max_new_bd > max_bd and (max_new_bd != np.Infinity):
            max_bd = max_new_bd
    lgd = fig.legend(sorted_totals_dict.keys(), bbox_to_anchor=(1.35, 1))

    plt.xlabel('Amino acid position in loop')
    plt.ylabel('distance')
    plt.ylim(0, min(math.ceil(max_bd + 0.1), 5.5))
    plt.tight_layout()
    
    fig.savefig(outpng.split('.png')[0]+'_rainbow.png', dpi=300, bbox_extra_artists=(lgd,), bbox_inches='tight')
    plt.close()

    #top3 only
    cmap = matplotlib.cm.get_cmap('coolwarm')
    fig = plt.gcf()
    fig.set_size_inches(6, 3)
    max_bd = 0.0
    top_3_dict = {k: sorted_totals_dict[k] for k in list(sorted_totals_dict.keys())[:3]}
    min_total = min(top_3_dict.values())
    max_total = max(top_3_dict.values())
    for name in top_3_dict:
        rel_total = (totals_dict[name] - min_total)/(max_total - min_total)
        color_dict[name] = cmap(rel_total)
        ax = plt.plot(df[name], color=color_dict[name])
        max_new_bd = max(list(df[name]))
        if max_new_bd > max_bd and (max_new_bd != np.Infinity):
            max_bd = max_new_bd
    lgd = fig.legend(top_3_dict, bbox_to_anchor=(1.35, 1))

    plt.xlabel('Amino acid position in loop')
    plt.ylabel('distance')
    plt.ylim(0,math.ceil(max_bd))
    plt.tight_layout()
    
    fig.savefig(outpng.split('.png')[0]+'_top3.png', dpi=300, bbox_extra_artists=(lgd,), bbox_inches='tight')
    plt.close()
    
    return sorted_totals_dict