Silviu Tofan
Silviu Tofan

Reputation: 489

Plot dataframe grouped by one column, and hue by another column

I have the following DataFrame and would like to create separate line graphs (1 for each "Cluster"), where x-axis is "Week", y-axis is "Slot Request" and hue is "Group".

To get the data that I want to plot, I use

summed = full_df.groupby(["Group", "Cluster", "Week"])["Slot Request"].sum()

The snippet above returns a "Slot Request", dtype = int64. From here onwards, I'm kind-of stuck.

Since I had no success in plotting the result from above, I tried exporting it as a .csv and then re-importing (to bring it back to a dataframe, as I didn't know how else to do it, sorry for the blasphemy).

The only working code I could come up with is below, but that's not exactly what I need to get. No luck using FacetGrid either.

for i, group in summed.groupby("Cluster"):
    plt.figure()
    sns.pointplot(data = summed, x="Week", y="Slot Request", hue="Group", scale=0.2)

Upvotes: 1

Views: 2720

Answers (2)

Silviu Tofan
Silviu Tofan

Reputation: 489

Thank you very much @Sam for the idea. I would've posted in comments, but code formatting was not clear.

Using the following:

summed = full_df.groupby(["Group", "Cluster", "Week"])["Slot Request"].sum().reset_index()
g = sns.FacetGrid(summed, row ="Cluster", hue = "Cluster", sharex = False)
g.map(sns.pointplot, "Week", "Slot Request", "Group", scale=0.2, palette = sns.color_palette("muted"))
g.set(xticks = np.arange(1, number_of_weeks+2,week_span))
g.set_xticklabels([w for w in range(1, number_of_weeks+2, week_span)])

I managed to obtain: enter image description here

However, I'm not sure hot to set x_ticks and x_tick_labels for all the 3 plots, nor how to add a legend for the colours (where each colour is a different "Group"). Any suggestions?

Upvotes: 0

Sam
Sam

Reputation: 4090

Try something like this:

summed = full_df.groupby(["Group", "Cluster", "Week"])["Slot Request"].sum().reset_index() #reset_index turns this back into a normal dataframe
g = sns.FacetGrid(summed, col="Group") #create a new grid for each "Group"
g.map(sns.pointplot, 'Week', 'Slot Request') #map a pointplot to each group where X is Week and Y is slot request

Upvotes: 3

Related Questions