亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么 Seaborn 繪制兩個圖例,我如何刪除一個并修復另一個?

為什么 Seaborn 繪制兩個圖例,我如何刪除一個并修復另一個?

汪汪一只貓 2022-04-23 17:06:57
當我運行下面顯示的代碼時,我得到一個包含 2 個圖例的圖形。我無法弄清楚為什么要繪制兩個并且我無法刪除其中一個。我的目標是保留圖例之外的圖例,刪除圖例內部的圖例,并以某種方式停止切斷圖例右側圖例的奇怪裁剪。我之前有一個問題問過類似的問題,但是通過使用 seaborns scatterplot 而不是 relplot 解決了這個問題??杀氖牵谠搯栴}中有效的答案在這里都不起作用。如果這個問題是由繪制我試圖制作的圖形類型的“非常規”方式引起的,請告訴我。正確地做到這一點比破解解決方案要好...  import matplotlib.pyplot as plt    import seaborn as sns    import numpy as np    import pandas as pd    #setup    sns.set(font_scale=2)    sns.set_context('poster')    #figure and axes    fig = plt.figure(figsize=(20,20))    axs = {i:fig.add_subplot(330+i) for i in range(1,10)}    #create random data    r = np.random.randint    N=10    df = pd.DataFrame(columns=['No.','x1','x2','x3','y1','y2','y3'])    for i in range(N):        df.loc[i] = i+1,r(50,high=100),r(50,high=100),r(50,high=100),r(50,high=100),r(50,high=100),r(50,high=100)    #create axes labels    x_labels = ['x1','x2','x3']    y_labels = ['y1','y2','y3']    xy_labels = [(x,y) for y in y_labels for x in x_labels ]    #plot on axes    for i,(x_label,y_label) in enumerate(xy_labels):        if i ==0:#if statement so only one of the plots has legend='full'            a = sns.scatterplot(                data=df,                x=x_label,                y=y_label,                legend='full', #create the legend                ax=axs[i+1],                hue='No.',                palette=sns.color_palette("hls", N)            )            fig.legend(bbox_to_anchor=(1, 0.7), loc=2, borderaxespad=0.) #Move the legend outside the plot            a.legend_.remove() #attempt to remove the legend        else:            a = sns.scatterplot(                data=df,                x=x_label,                y=y_label,                legend=False,                ax=axs[i+1],                hue='No.',                palette=sns.color_palette("hls", N)            )        #remove axes labels from specific plots        if i not in [0,3,6]: axs[i+1].set_ylabel('')        if i not in [6,7,8]: axs[i+1].set_xlabel('')
查看完整描述

1 回答

?
紅顏莎娜

TA貢獻1842條經驗 獲得超13個贊

您可以添加legend=False代碼的最后一部分。


#setup

sns.set(font_scale=2)

sns.set_context('poster')


#figure and axes

fig = plt.figure(figsize=(20,20))

axs = {i:fig.add_subplot(330+i) for i in range(1,10)}


#create axes labels

x_labels = ['x1','x2','x3']

y_labels = ['y1','y2','y3']

xy_labels = [(x,y) for y in y_labels for x in x_labels ]


#plot on axes

for i,(x_label,y_label) in enumerate(xy_labels):

    if i ==0:#if statement so only one of the plots has legend='full'

        a = sns.scatterplot(

            data=df,

            x=x_label,

            y=y_label,

            legend='full', #create the legend

            ax=axs[i+1],

            hue='No.',

            palette=sns.color_palette("hls", N)

       )

        fig.legend(bbox_to_anchor=(1, 0.7), loc=2, borderaxespad=0.) #Move the legend outside the plot

        a.legend_.remove() #attempt to remove the legend

    else:

        a = sns.scatterplot(

            data=df,

            x=x_label,

            y=y_label,

            legend=False,

            ax=axs[i+1],

            hue='No.',

            palette=sns.color_palette("hls", N)

        )


    #remove axes labels from specific plots

    if i not in [0,3,6]: axs[i+1].set_ylabel('')

    if i not in [6,7,8]: axs[i+1].set_xlabel('')


#add line plots and set limits        

for ax in axs.values():

    sns.lineplot(x=range(50,100),y=range(50,100), ax=ax, linestyle='-', legend=False)

    ax.set_xlim([50,100])

    ax.set_ylim([50,100])


fig.tight_layout()

結果:

http://img1.sycdn.imooc.com//6263c1eb00010b8114281296.jpg

查看完整回答
反對 回復 2022-04-23
  • 1 回答
  • 0 關注
  • 406 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號