Reputation: 25
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 |
Any help or guidance is appreciated.
Upvotes: 0
Views: 1344
Reputation: 2271
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)
Upvotes: 1
Reputation: 2271
Might have to appeal to matplotlib instead of seaborn. Here is an example:
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 = plt.subplots()
# choose a palette you like https://seaborn.pydata.org/tutorial/color_palettes.html
cmap = sns.color_palette("flare", n_colors=df["hue"].nunique())
for hue, color in zip(df["hue"].unique(), cmap):
temp_df = df[df["hue"]==hue]
ax.plot(temp_df["x"], temp_df["y"], marker="o", color=color)
Upvotes: 1