I have three columns. But I'm interested in plotting two columns with respect to each other. The datatype of the columns are datetime and object. The datetime column name is started_at and object column name is member_casual. I want started_at(hourly) on my x-axis and member_causal(count) on y-axis. I use this code
my DaraFrame(df) looks like this
rideable_type started_at member_casual
electric_bike 2021-06-13 14:31:28 casual
classic_bike 2021-06-04 11:18:02 member
classic_bike 2021-06-04 09:49:35 casual
calssic_bike 2021-06-04 09:55:34 casual
electric_bike 2021-06-04 14:05:51 casual
classic_bike 2021-06-04 14:09:59 member
electric_bike 2021-06-03 19:32:01 casual
electric_bike 2021-06-10 16:30:10 member
... ... ...
... ... ...
this code gives me line plot of counting member_casual(y-axis) with respect to hourly time(x-axis).
pd.crosstab(df.started_at.dt.hour,df.member_casual).plot(kind='line', ax = ax)
Can anyone tell me how to draw the same plot using seaborn or matplotlib ?
I,m begineer so this is the best I can do.
try_1
plt.plot(df.started_at.dt.hour, df.member_casual)
try_2
sns.lineplot(df.started_at.dt.hour, hue = df.member_casual)
ofcourse both lines of code gives me an error.
CodePudding user response:
You first need to count the hourly occurrences, for instance the same way as you already did:
ct = pd.crosstab(df.started_at.dt.hour, df.member_casual)
matplotlib step by step:
plt.plot(ct.index, ct.casual, label='casual')
plt.plot(ct.index, ct.member, label='member')
plt.legend()
matplotlib short form:
plt.plot(ct)
plt.legend(ct.columns)
seaborn:
sns.lineplot(data=ct)