Python 以不同的色调绘制点标记和线条,但与 seaborn 风格相同

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52385428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:06:11  来源:igfitidea点击:

Plot point markers and lines in different hues but the same style with seaborn

pythondata-visualizationseabornaesthetics

提问by ytu

Given the data frame below:

鉴于以下数据框:

import pandas as pd
df = pd.DataFrame({
    "n_index": list(range(5)) * 2,
    "logic": [True] * 5 + [False] * 5,
    "value": list(range(5)) + list(range(5, 10))
})

I'd like to use color and only colorto distinguish logicin a line plot, and mark points on values. Specifically, this is my desired output (plotted by R ggplot2):

我想在线图中使用颜色和仅颜色来区分logic,并在values上标记点。具体来说,这是我想要的输出(由 R ggplot2绘制):

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

desired output

期望的输出

I tried to do the same thing with seaborn.lineplot, and I specified markers=Truebut there was no marker:

我试图用 做同样的事情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

sns 没有标记

I then tried adding style="logic"in the code, now the markers showed up:

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

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

sns with markers 1

带有标记的 sns 1

Also I tried forcing the markers to be in the same style:

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

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

sns with markers 2

带有标记的 sns 2

It seems like that I have to specify stylebefore I can have markers. However, that causes undesired plot output since I don't want to use two aesthetic dimensions on one data dimension. That violates the principles of aesthetic mapping.

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

Is there any way I can have the lines and points all in the same style but in different colors with seabornor Python visualization? (seabornis preferred - I don't like the looping way ofmatplotlib.)

有什么方法可以使线条和点都具有相同的样式但颜色不同,并且seaborn可以使用 Python 可视化?(seaborn是首选 - 我不喜欢 . 的循环方式matplotlib。)

采纳答案by ImportanceOfBeingErnest

You can directly use pandas for plotting.

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

pandas via groupby

熊猫通过 groupby

fig, ax = plt.subplots()
df.groupby("logic").plot(x="n_index", y="value", marker="o", ax=ax)
ax.legend(["False","True"])

enter image description here

在此处输入图片说明

The drawback here would be that the legend needs to be created manually.

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

pandas via pivot

大熊猫通过枢轴

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

enter image description here

在此处输入图片说明

seaborn lineplot

seaborn 线图

For seaborn lineplot it seems a single marker is enough to get the desired result.

对于seaborn lineplot,似乎一个标记就足以获得所需的结果。

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

enter image description here

在此处输入图片说明

回答by Dani Mesejo

You need to set dashesparameter to Falsealso specify the style of the grid to "darkgrid":

您需要设置dashes参数以False还将网格的样式指定为"darkgrid"

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

df = pd.DataFrame({
    "n_index": list(range(5)) * 2,
    "logic": [True] * 5 + [False] * 5,
    "value": list(range(5)) + list(range(5, 10))
})

sns.set_style("darkgrid")
sns.lineplot(x="n_index", dashes=False, y="value", hue="logic", style="logic", markers=["o", "o"], data=df)
plt.show()

enter image description here

在此处输入图片说明

回答by Akshay Jagadeesh

You can set marker='o' in sns.linePlot to draw the marker as a circle for all the different hues, in the appropriate color.

您可以在 sns.linePlot 中设置 marker='o' 以将标记绘制为所有不同色调的圆形,并使用适当的颜色。

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