繁体   English   中英

当在同一图上绘制多个图时,图例无法完全打印

[英]legends not print fully when multiple plots are plotted on same figure

我有如下代码在同一图上绘制多个图

fig, ax = plt.subplots(figsize=(25, 10))
def wl_ratioplot(wavelength1,wavelength2, dataframe, x1=0.1,x2=1.5,y1=-500,y2=25000):
    a=dataframe[['asphalt_index','layer_thickness',wavelength1,wavelength2]].copy()
    sns.scatterplot(x=a[wavelength1]/a[wavelength2],y=a['layer_thickness'],data=a)
    ax.set_xlim(x1,x2)
    ax.set_ylim(y1,y2)
    leg = "{} vs {}".format(wavelength1,wavelength2)
    print(leg) #this line is only to see the variable legend has the proper content
    ax.legend(leg)
wl_ratioplot(wave_lengths[2],wave_lengths[0],dataframe=train_df_wo_outliers,x1=-.1,x2=3)
wl_ratioplot(wave_lengths[0],wave_lengths[1],dataframe=train_df_wo_outliers,x1=-.1,x2=3)
wl_ratioplot(wave_lengths[3],wave_lengths[1],dataframe=train_df_wo_outliers,x1=-.1,x2=3)
wl_ratioplot(wave_lengths[3],wave_lengths[0],dataframe=train_df_wo_outliers,x1=-.1,x2=3)
wl_ratioplot(wave_lengths[2],wave_lengths[1],dataframe=train_df_wo_outliers,x1=-.1,x2=3)

我得到的情节如下图,其中图例似乎分别是前 5 个字母,即使变量图例具有正确的内容在此处输入图片说明

还有另一个类似的问题,解决方案是在变量图例中加上方括号。 我用下面的代码试过这个。

fig, ax = plt.subplots(figsize=(25, 10))
def wl_ratioplot(wavelength1,wavelength2, dataframe, x1=0.1,x2=1.5,y1=-500,y2=25000):
    a=dataframe[['asphalt_index','layer_thickness',wavelength1,wavelength2]].copy()
    sns.scatterplot(x=a[wavelength1]/a[wavelength2],y=a['layer_thickness'],data=a)
    ax.set_xlim(x1,x2)
    ax.set_ylim(y1,y2)
    leg = "{} vs {}".format(wavelength1,wavelength2)
    print(leg)#this line is only to see the variable legend has the proper content
    ax.legend([leg])
wl_ratioplot(wave_lengths[2],wave_lengths[0],dataframe=train_df_wo_outliers,x1=-.1,x2=3)
wl_ratioplot(wave_lengths[0],wave_lengths[1],dataframe=train_df_wo_outliers,x1=-.1,x2=3)
wl_ratioplot(wave_lengths[3],wave_lengths[1],dataframe=train_df_wo_outliers,x1=-.1,x2=3)
wl_ratioplot(wave_lengths[3],wave_lengths[0],dataframe=train_df_wo_outliers,x1=-.1,x2=3)
wl_ratioplot(wave_lengths[2],wave_lengths[1],dataframe=train_df_wo_outliers,x1=-.1,x2=3)

现在我得到了完整的图例,但只有第一个图例显示如下图在此处输入图片说明

有人可以让我知道如何获得所有情节的完整图例吗? 谢谢。

虚拟数据(图片中的情节不匹配)

14nm    15nm    16nm    17nm    18nm    19nm    layer_thickness
1   2   3   4   5   6   0
1   2   3   4   5   6   0
3   5   7   9   11  13  5700
1   2   3   4   5   6   0
3   5   7   9   11  13  8600
1   2   3   4   5   6   0
3   5   7   9   11  13  5000
1   2   3   4   5   6   0
45  55  65  75  85  95  100
1   2   3   4   5   6   0
8   15  22  29  36  43  16600

wave_lengths=['15nm','16nm','14nm','18nm']

答案更新基于 Quang Hoang 的答案。 使用 matplotlib 和 sns.scatterplot 散点图的输出图片

在此处输入图片说明

在此处输入图片说明

每次调用函数 wl_ratioplot 时,图例都会被重置为最终值。 使用数组存储所有图例,然后通过循环访问它。

ax.legend([leg]) #it is resetting the legend after each call.

使用图例 = [];

legends.append([leg])

在所有函数调用之后,以不同的方式绘制图例

ax.legend(legends)

使用plt很自然:

def wl_ratioplot(wavelength1,wavelength2, dataframe, 
                 x1=0.1,x2=1.5,y1=-500,y2=25000,
                 ax=None):
    leg = "{} vs {}".format(wavelength1,wavelength2)
    
    # set the label here, and let plt deal with it 
    # also, you don't need to copy the dataframe:
    ax.scatter(x=dataframe[wavelength1]/dataframe[wavelength2],
               y=dataframe['layer_thickness'],label=leg)
    ax.set_xlim(x1,x2)
    ax.set_ylim(y1,y2)

fig, ax = plt.subplots(figsize=(25, 10))
wl_ratioplot(wave_lengths[2],wave_lengths[0],dataframe=df,x1=-.1,x2=3, ax=ax)
wl_ratioplot(wave_lengths[0],wave_lengths[1],dataframe=df,x1=-.1,x2=3, ax=ax)
wl_ratioplot(wave_lengths[3],wave_lengths[1],dataframe=df,x1=-.1,x2=3, ax=ax)
wl_ratioplot(wave_lengths[3],wave_lengths[0],dataframe=df,x1=-.1,x2=3, ax=ax)
wl_ratioplot(wave_lengths[2],wave_lengths[1],dataframe=df,x1=-.1,x2=3, ax=ax)
ax.legend()

输出:

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM