3

I have hourly timeseries data for over a year, and wish to plot average hourly measurements for each day of the week. I have run the following code and my data is in the following format (i previously created columns for days of week).

df = df.groupby([df['date_UTC'].dt.hour, df['w_day']]).pm2_5_ugm3.mean().reset_index().dropna()

date_UTC    w_day   pm2_5_ugm3
0   0   Friday  11.868802
1   0   Monday  12.741524
2   0   Saturday    13.346273
3   0   Sunday  13.093804
4   0   Thursday    14.525096
... ... ... ...
163 23  Saturday    12.836788
164 23  Sunday  12.712683
165 23  Thursday    12.046935
166 23  Tuesday 12.043133
167 23  Wednesday   14.164027

I ultimately want a plot that looks something like below, but I am not sure exactly how to plot it using seaborn/matplotlib.

enter image description here

UPDATE:

I used the suggestion of @Arnold Vialfont which basically gets me the results I am looking for, but facetplot doesnt seem to allow me to plot a legend when I introduce hue. I then used factorplot however it doesnt seem that this plot can produce a lineplot, which is what I need. Is there a way to use factorplot and have hue produce a legend?

col_order=["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

sns.set_style("white")
ax = sns.FacetGrid(df, col="w_day", col_wrap=7, height=6, col_order=col_order, hue ='Zone')
ax.map(sns.lineplot, "date_UTC", "pm2_5_ugm3")

Hue not producing legend

enter image description here

Here using factorplot i have a legend but cant produce a line plot as desired.

ax = sns.catplot(x="date_UTC", y="pm2_5_ugm3", col="w_day", hue = 'Zone',
                data=df, ci=None, aspect=.6)

enter image description here

ojp
  • 973
  • 1
  • 11
  • 26

2 Answers2

3

You can use seaborn in the following way, where col_wrap allows to choose the number of graphics per line:

g = sns.FacetGrid(df, col="w_day", col_wrap=7, height=4)
h.map(sns.lineplot, "date_UTC", "pm2_5_ugm3")

Note that as compared to what you say you ultimately want there will be a single curve.

  • Thanks, this mostly worked fine, although having issues with legend. I have edited my original question. – ojp Feb 18 '20 at 09:58
0

After doing some more research it seems the best way to achieve what I want is to use Seaborn's relplot, since this combines both lineplot and facetgrid.

ojp
  • 973
  • 1
  • 11
  • 26