首页 > 解决方案 > 子图色调的相同颜色定义

问题描述

我希望将图例颜色的相同定义应用于 3 个子图,因此我只需要在顶部子图中显示图例。使用 seabrons “hue” 时这可能吗?还是我必须使用 matplotlib?

具有不同颜色含义的散点图

def plotverweildauern_with_age(df):

    def rand_float_range(start, end):
        return random.random() * (end - start) + start

    df['years']=df['years'].astype('float64')
    df['age']=df['age'].astype(int)


    sns.set()
    #sharex=True: x-Achse für alle Subplots geteilt
    #fig,ax= plt.subplots(): tuple containing figure and axes object
    fig, ax = plt.subplots(nrows=df['cs'].max(), sharex=True)
    nrows=len(ax)
    ax[df['cs'].max()-1].set_xlabel('Verweildauer in Jahren')

    sns.color_palette("Blues")

    s=[(df["cs"] == t).sum() for t in range(1, df['cs'].max()+1)]
    s.insert(0, 0)
    print(s)


    for i in range(1, df['cs'].max()+1):
        verweildauer_i = df[df['cs'] == i]['years']
        cs_bandwith_i = df[df['cs'] == i]['cs_bandwith']
        age_i = df[df['cs'] == i]['age']

        width=(10*nrows)/3
        height=(7*nrows)/3
        fig.set_size_inches(width,height)
        plt.subplots_adjust(hspace=0)

        sns.scatterplot(x=verweildauer_i, y=cs_bandwith_i-i, hue=age_i, ax=ax[i-1])

        ymin, ymax = ax[i-1].set_ylim()

        if ymax<1:
            ax[i-1].set_ylim(-.5, max(1, ymax))
        else:
            ax[i-1].set_ylim(-1.5, max(1, ymax))

        ax[i-1].set_xlim(-.5, round(df['years'].max(),0)+5)
        ax[i-1].set_ylabel('ZK ' + str(i))
        ax[i-1].set_yticks([])


        handles, labels = ax[i-1].get_legend_handles_labels()
        # create the legend again skipping this first entry
        leg = ax[i-1].legend(handles[1:], labels[1:])

        ax2=ax[i-1]
        ax2 = ax[i-1].twinx()
        ax2.grid(False)
        ax2.set_ylabel("n = {}".format(s[i]), rotation=0, labelpad=25)
        ax2.set_yticks([])

标签: pythonseabornscatter-plothue

解决方案


推荐阅读