简体   繁体   English

使用 pandas 和 seaborn 的分钟和小时数据的 Plot 箱线图

[英]Plot boxplots for minute - and hourly data using pandas and seaborn

I've got the following dataframe:我有以下 dataframe:

dfB1

Date_and_time                   MP  
2020-08-28 19:05:00.066663676   75.0
2020-08-28 19:05:00.133330342   70.0
2020-08-28 19:05:00.199997008   76.0
2020-08-28 19:05:00.266663674   85.0
2020-08-28 19:05:00.333330340   73.0
... ...
2020-08-29 01:59:50.666414770   1454.0
2020-08-29 01:59:50.733081436   1359.0
2020-08-29 01:59:50.799748102   1320.0
2020-08-29 01:59:50.866414768   1217.0
2020-08-29 01:59:50.933081434   1246.0

373364 rows × 1 columns

My goal is to create a plot which displays boxplots for every 1 or 5 or 30 minutes, or even every 1 hour.我的目标是创建一个 plot 每 1 或 5 或 30 分钟甚至每 1 小时显示一次箱线图。 The datetimeindex is in the correct format (data was collected at 15 Hz, which means every datapoint is 66666666 nanaseconds) in order to index to 'hours'. datetimeindex 格式正确(以 15 Hz 的频率收集数据,这意味着每个数据点为 66666666 纳秒)以便索引到“小时”。

dfB1.index

DatetimeIndex(['2020-08-28 19:05:00.066663676',
               '2020-08-28 19:05:00.133330342',
               '2020-08-28 19:05:00.199997008',
               '2020-08-28 19:05:00.266663674',
               '2020-08-28 19:05:00.333330340',
               ...
               '2020-08-29 01:59:50.666414770',
               '2020-08-29 01:59:50.733081436',
               '2020-08-29 01:59:50.799748102',
               '2020-08-29 01:59:50.866414768',
               '2020-08-29 01:59:50.933081434'],
              dtype='datetime64[ns]', name='Date_and_time', length=373364, freq='66666666N')

I've tried plotting using seaborn, and I get a result.我尝试使用 seaborn 进行绘图,我得到了结果。 But I can't interact with the plot and it is also plotted very poorly.但我无法与 plot 交互,而且它的绘制也很差。 I am familiar with plotly, but I can't seem to find a way to integrate plotly.我熟悉plotly,但我似乎找不到集成plotly的方法。 Also, the minute plot is completely wrong.此外,分钟 plot 是完全错误的。 I only get 59 points on the x-axis.我在 x 轴上只得到 59 分。 What should I do to interact with the plots and to get boxplots every minute (or every 5 minutes)?我应该怎么做才能与图交互并每分钟(或每 5 分钟)获取箱线图? I've also read and tried functions described here: Box plot of hourly data in Time Series Python我还阅读并尝试了此处描述的功能: Box plot of hourly data in Time Series Python

import seaborn as sns
fig, ax = plt.subplots(figsize=(15,5))
sns.boxplot(x=dfB1.index.hour, y=dfB1['MP'], ax=ax)

hour gives only the hours, ie both 2020-01-01 00:00 and 2020-01-10 00:00 will give 0 . hour仅给出小时数,即2020-01-01 00:002020-01-10 00:00都会给出0 I think you want .floor :我想你想要.floor

sns.boxplot(x=dfB1.index.floor('H'), y=dfB1['MP'], ax=ax)

and also:并且:

sns.boxplot(x=dfB1.index.floor('5Min'), y=dfB1['MP'], ax=ax)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM