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
.)
Line Plot Markers You can also plot markers on a Seaborn line plot. Markers are special symbols that appear at the places in a line plot where the values for x and y axes intersect. To plot markers, you have to pass a list of symbols in a list to the markers attribute. Each symbol corresponds to one line plot.
In Seaborn, we will plot multiple graphs in a single window in two ways. First with the help of Facetgrid() function and other by implicit with the help of matplotlib. data: Tidy dataframe where each column is a variable and each row is an observation.
In seaborn, the hue parameter represents which column in the data frame, you want to use for color encoding. The example shown here is from the official document for lmplot: import seaborn as sns; sns.set(color_codes=True) tips = sns.load_dataset("tips") g = sns.lmplot(x="total_bill", y="tip", data=tips)
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")
See the problem is that people are getting confused between 'markers' and 'marker'. To enable 'marker' set 'marker='o'' not markers.
sns.lineplot(x=range(1,100),y=err,marker='o')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With