代码之家  ›  专栏  ›  技术社区  ›  ytu

以不同的颜色绘制点标记和线,但与Seaborn的样式相同

  •  5
  • ytu  · 技术社区  · 7 年前

    根据下面的数据框架:

    导入熊猫为pd df=pd.数据帧({ “n_index”:列表(范围(5))*2, “logic”:[true]*5+[false]*5, “value”:list(range(5))+list(range(5,10))。 }) < /代码>

    我想使用 color and only color 来区分 logic in a line plot,并在 value s上标记点。具体来说,这是我想要的输出(由r ggplot2 )进行绘制:

    ggplot(aes(x=n_index,y=value,color=logic),data=df)+geom_line()+geom_point())
    < /代码> 
    
    

    我试着用seaborn.lineplot>,and I specifiedmarkers=true.>code>but there was no marker:(我指定了markers=true),做了同样的事情。

    import seaborn as sns
    SnS()
    sns.lineplot(x=“n_index”,y=“value”,hue=“logic”,markers=true,data=df)
    < /代码> 
    
    

    然后我尝试在代码中添加style=“logic”,现在标记出现了:

    sns.lineplot(x=“n_index”,y=“value”,hue=“logic”,style=“logic”,markers=true,data=df)
    < /代码> 
    
    

    另外,我还尝试强制标记采用相同的样式:

    sns.lineplot(x=“n_index”,y=“value”,hue=“logic”,style=“logic”,markers=[“o”,“o”],data=df)
    < /代码> 
    
    

    似乎我必须先指定样式才能使用标记。但是,这会导致不希望的绘图输出,因为我不想在一个数据维度上使用两个美学维度。这违反了美学映射的原则。

    是否有任何方法可以使线条和点都具有相同的样式,但具有不同的颜色(使用seaborn或python visualization)?(seabornis preferred-I don't like the looping way ofmatplotlib.>code>)

    我想使用颜色和唯一的颜色辨别logic在线图中,标记点value特别是,这是我想要的输出(由R绘制ggplot2):

    ggplot(aes(x = n_index, y = value, color = logic), data = df) + geom_line() + geom_point()
    

    desired output

    我也试着做同样的事seaborn.lineplot,我指定了markers=True但没有标记:

    import seaborn as sns
    sns.set()
    sns.lineplot(x="n_index", y="value", hue="logic", markers=True, data=df)
    

    sns no markers

    然后我尝试添加style="logic"在代码中,现在标记出现了:

    sns.lineplot(x="n_index", y="value", hue="logic", style="logic", markers=True, data=df)
    

    sns with markers 1

    另外,我尝试强制标记采用相同的样式:

    sns.lineplot(x="n_index", y="value", hue="logic", style="logic", markers=["o", "o"], data=df)
    

    sns with markers 2

    好像我必须说明style在我有记号笔之前。但是,这会导致不需要的绘图输出,因为我不想在一个数据维度上使用两个美学维度。这违反了美学绘图的原则。

    有没有什么方法可以让线条和点都是同一样式,但颜色不同seaborn还是Python可视化?(西伯恩是首选-我不喜欢matplotlib)

    2 回复  |  直到 7 年前
        1
  •  1
  •   ImportanceOfBeingErnest    7 年前

    您可以直接使用熊猫进行绘图。

    Pandas via Groupby

    fig,ax=plt.subflots()。
    df.groupby(“logic”).plot(x=“n_index”,y=“value”,marker=“o”,ax=ax)
    ax.图例([“假”,“真”])
    < /代码> 
    
    

    这里的缺点是需要手动创建图例。

    熊猫via pivot

    df.pivot_table(“value”,“n_index”,“logic”).plot(marker=“o”)
    < /代码> 
    
    

    seaborn lineplot

    对于Seaborn lineplot,单个标记似乎足以获得所需的结果。

    sns.lineplot(x=“n_index”,y=“value”,hue=“logic”,data=df,marker=“o”)
    < /代码> 
    
    

    enter image description here

    这里的缺点是需要手动创建图例。

    大熊猫经支点

    df.pivot_table("value", "n_index", "logic").plot(marker="o")
    

    enter image description here

    海岸线图

    对于Seaborn测线图来说,一个标记就足以获得所需的结果。

    sns.lineplot(x="n_index", y="value", hue="logic", data=df, marker="o")
    

    enter image description here

        2
  •  1
  •   Dani Mesejo    7 年前

    您需要将 短划线设置为 parameter to false also specify the style of the grid to “darkgrid” :。

    导入熊猫为pd 导入Seaborn作为SNS 将matplotlib.pyplot导入为plt df=pd.数据帧({ “n_index”:列表(范围(5))*2, “logic”:[true]*5+[false]*5, “value”:list(range(5))+list(range(5,10))。 }) sns.set_-style(“黑色网格”) sns.lineplot(x=“n_index”,dashes=false,y=“value”,hue=“logic”,style=“logic”,markers=[“o”,“o”],data=df) 显示() < /代码>

    enter image description here