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

时间:2018-09-18 11:19:43

标签: python data-visualization seaborn aesthetics

给出以下数据框:

JavaCheckVerifier

我想使用 color和仅color 来区分线图中的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)) }) ,并在logic上标记点。具体来说,这是我想要的输出(由R ggplot2绘制):

value

desired output

我尝试用seaborn.lineplot做同样的事情,我指定了ggplot(aes(x = n_index, y = value, color = logic), data = df) + geom_line() + geom_point() ,但是没有标记:

markers=True

sns no markers

然后我尝试在代码中添加import seaborn as sns sns.set() sns.lineplot(x="n_index", y="value", hue="logic", markers=True, data=df) ,现在标记出现了:

style="logic"

sns with markers 1

我还试图将标记设置为相同样式:

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

sns with markers 2

似乎我必须先指定sns.lineplot(x="n_index", y="value", hue="logic", style="logic", markers=["o", "o"], data=df) 才能使用标记。但是,由于我不想在一个数据维度上使用两个美学维度,因此会导致不希望的绘图输出。这违反了美学映射的原理。

是否可以通过style或Python可视化使线条和点具有相同的样式,但具有不同的颜色? (首选seaborn-我不喜欢seaborn的循环方式。)

4 个答案:

答案 0 :(得分:2)

看到的问题是,人们对“标记”和“标记”感到困惑。要启用“标记”,请设置“ marker ='o”而不是标记。

sns.lineplot(x=range(1,100),y=err,marker='o')

答案 1 :(得分:1)

您需要将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

答案 2 :(得分:1)

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

通过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

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

通过数据透视的熊猫

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

答案 3 :(得分:1)

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

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