用不同的颜色绘制点标记和线条,但与Seaborn的样式相同 [英] Plot point markers and lines in different hues but the same style with seaborn
问题描述
给出以下数据框:
将熊猫作为pd
df = pd.DataFrame({
n_index:list(range(5))* 2,
logic:[True] * 5 + [False] * 5,
value:list(range(5) )+ list(range(5,10))
})
我想要使用 color和仅color 来区分线图中的逻辑
,并在 value $ c $上标记点c> s。具体来说,这是我想要的输出(由R
我尝试执行与
然后我尝试了addi ng 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 =逻辑,样式=逻辑,标记= [ o, o],数据= df)
似乎我必须指定样式
在我有了标记之前。但是,由于我不想在一个数据维度上使用两个美学维度,因此会导致不良的绘图输出。
有什么办法可以用使线条和点具有相同的样式,但是具有不同的颜色? seaborn
还是Python可视化? (首选 seaborn
-我不喜欢 matplotlib
的循环方式。)
您可以直接使用熊猫进行绘图。
通过groupby 熊猫>
fig,ax = plt.subplots()
df.groupby( logic)。plot(x = n_index,y = value,marker = o,ax = ax)
ax.legend([ False, True])
这里的缺点是图例需要手动创建。
通过数据透视的熊猫
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)
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 color to distinguish logic
in a line plot, and mark points on value
s. Specifically, this is my desired output (plotted by R ggplot2):
ggplot(aes(x = n_index, y = value, color = logic), data = df) + geom_line() + geom_point()
I tried to do the same thing with seaborn.lineplot
, and I specified markers=True
but there was no marker:
import seaborn as sns
sns.set()
sns.lineplot(x="n_index", y="value", hue="logic", markers=True, data=df)
I then tried adding style="logic"
in the code, now the markers showed up:
sns.lineplot(x="n_index", y="value", hue="logic", style="logic", markers=True, data=df)
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)
It seems like that I have to specify style
before 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.
Is there any way I can have the lines and points all in the same style but in different colors with seaborn
or Python visualization? (seaborn
is preferred - I don't like the looping way ofmatplotlib
.)
You can directly use pandas for plotting.
pandas via groupby
fig, ax = plt.subplots()
df.groupby("logic").plot(x="n_index", y="value", marker="o", ax=ax)
ax.legend(["False","True"])
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")
seaborn lineplot
For seaborn lineplot it seems a single marker is enough to get the desired result.
sns.lineplot(x="n_index", y="value", hue="logic", data=df, marker="o")
这篇关于用不同的颜色绘制点标记和线条,但与Seaborn的样式相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!