如何使用Seaborn或Matplotlib制作相同的Crosstab线图?

发布于 2025-02-08 11:55:30 字数 1310 浏览 2 评论 0原文

我有三列。但是我有兴趣彼此绘制两列。列的数据类型是DateTime和对象。 DateTime列名是 start_at ,对象列名是 member_casual 。 我想要 start_at (每小时)在我的x轴上, emment_causal (计数)y轴上。我使用此代码

    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
   ...                ...                         ...
   ...                ...                         ...

此代码为我提供了计数成员_casual(y轴)的行图,相对于小时时间(x轴)。

pd.crosstab(df.started_at.dt.hour,df.member_casual).plot(kind='line', ax = ax)

谁能告诉我如何使用Seaborn或Matplotlib绘制相同的情节?

我,M Begineer,这是我能做的最好的。

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)

of course of两行代码都给了我一个错误。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傲世九天 2025-02-15 11:55:30

您首先需要计算每小时发生的情况,例如与您已经相同的方式:

ct = pd.crosstab(df.started_at.dt.hour, df.member_casual)
Matplotlib逐步:
plt.plot(ct.index, ct.casual, label='casual')
plt.plot(ct.index, ct.member, label='member')
plt.legend()
Matplotlib短形式:
plt.plot(ct)
plt.legend(ct.columns)
Seaborn:Seaborn:
sns.lineplot(data=ct)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文