'Connecting markers on seaborn scatterplot based on the hue color
So I started to work on a visual using seaborn but I ran into a problem that I cannot wrap my head around. Basically I have 2 columns (start and end date) that I would like to link together based on my hue. The current graph is attached, the ideal final result would have the same hues linked via a line between them. This is what I wrote so far to make these plots:
sns.scatterplot(data=df, y=df.index, x='Start_Date', hue='Conf_Num', legend=False)
sns.scatterplot(data=df, y=df.index, x='End_Date', hue='Conf_Num', legend=False)
Sample Data | Conf_Num | Start_Date | End_Date | | :--------| :--------------: |-----------:| | 0 | 2/8/2021 | 3/1/2021 | | 1 | 2/15/2021 | 12/31/2021| | 2 | 3/1/2021 | 8/29/2021 |
Solution 1:[1]
Alternate method which recovers point hue data from plot (makes some possibly risky assumptions)
test data
df = pd.DataFrame(
{
"x":np.random.randint(10, size=10),
"y":np.random.randint(10, size=10),
"hue":[str(int(i/2)) for i in range(10)],
}
)
plot code
ax = sns.scatterplot(data = df, x="x", y="y", hue="hue", cmap="bright")
pc = ax.collections[0]
point_data = pd.DataFrame(pc.get_offsets().data, columns=["x", "y"])
point_data["hue"] = pd.Series([tuple(arr) for arr in ax.collections[0].get_facecolor()])
for hue, df_ in point_data.groupby("hue"):
ax.plot(df_["x"], df_["y"], color=hue)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Riley |

