[英]How do I plot a graph with x_lims between time h1:m1 and h2:m2
I'm working on a project with loads of temperature data and I'm currently processing and plotting all of my data. 我正在处理一个包含大量温度数据的项目,目前正在处理和绘制所有数据。 However, I keep falling foul when I try to set x_lims on my plots between a time1 (9:00) and time2 (21:00)
但是,当我尝试在时间1(9:00)和时间2(21:00)之间的情节上设置x_lims时,我总是犯规
Data background: 资料背景:
The sensor has collected data every second for two weeks and I've split the main data file into smaller daily files (eg dayX). 传感器每两秒钟收集一次数据,我将主数据文件分成了一些较小的每日文件(例如dayX)。 Each day contains a timestamp (column = 'timeStamp') and a mean temperature (column = 'meanT').
每天包含一个时间戳记(列='timeStamp')和平均温度(列='meanT')。
The data for each day has been presliced just slightly over the window I want to plot (ie dayX contains data from 8:55:00 - 21:05:00). 每天的数据仅略微超出了我要绘制的窗口的范围(即dayX包含8:55:00-21:05:00的数据)。 The dataset contains NaN values at some points as the sensors were not worn and data needed to be discarded.
数据集在某些点上包含NaN值,因为传感器未磨损且需要丢弃数据。
Goal: 目标:
What I want to do is to be able to plot the dayX data between a set time interval (x_lim = 9:00 - 21:00). 我想做的是能够在设定的时间间隔(x_lim = 9:00-21:00)之间绘制dayX数据。 As I have many days of data, I eventually want to plot each day using the same x axis (I want them as separate figures however, not subplots), but each day has different gaps in the main data set, so I want to set constant x lims.
由于我有很多天的数据,所以我最终希望每天使用相同的x轴作图(但是我希望将它们作为单独的图形,而不是子图),但是每一天在主数据集中都有不同的差距,因此我想设置恒定x lims。 As I have many different days of data, I'd rather not have to specify the date as well as the time.
由于我有许多不同的数据天,所以我宁愿不必指定日期和时间。
Example data: 示例数据:
dayX =
timeStamp meanT
2018-05-10 08:55:00 NaN
. .
. .
. .
2018-05-10 18:20:00 32.4
. .
. .
. .
2018-05-10 21:05:00 32.0
What I've tried: 我尝试过的
import pandas as pd
import numpy as np
import datetime as dt
import matplotlib.pyplot as plt
from matplotlib.dates import date2num, DateFormatter
dayX = pd.read_csv('path/to/file/dayX.csv)
dayX['timeStamp'] = pd.to_datetime(dayX['timeStamp'], format=%Y %m %d %H:%M:%S.%f')
fig, ax1 = plt.subplots(1,1)
ax1.plot(dayX['timeStamp'], dayX['meanT'])
ax1.xaxis.set_major_formatter(DateFormatter('%H:%M'))
ax1.set_xlim(pd.Timestamp('9:00'), pd.Timestamp('21:00'))
fig.autofmt_xdate()
plt.show()
Which gives: 这使:
If I remove the limit line however, the data plots okay, but the limits are automatically selected 但是,如果我删除了极限线,数据就可以了,但是会自动选择极限
# Get rid of this line:
ax1.set_xlim(pd.Timestamp('9:00'), pd.Timestamp('21:00'))
# Get this:
I'm really not sure why this is going wrong or what else I should be trying. 我真的不确定为什么会出错或我还要尝试什么。
Your timeStamp
is a datetime
object. 您的
timeStamp
是一个datetime
对象。 All you got to do is pass the datetime objects as the limits. 您要做的就是将datetime对象作为限制。
import pandas as pd
import numpy as np
import datetime as dt
import matplotlib.pyplot as plt
from matplotlib.dates import date2num, DateFormatter
dayX = df
dayX['timeStamp'] = pd.to_datetime(dayX['timeStamp'], format='%Y-%m-%d %H:%M:%S')
fig, ax1 = plt.subplots(1,1)
ax1.plot(dayX['timeStamp'], dayX['meanT'])
ax1.xaxis.set_major_formatter(DateFormatter('%H:%M'))
ax1.set_xlim(df['timeStamp'].min().replace(hour=9), df['timeStamp'].min().replace(hour=21))
fig.autofmt_xdate()
plt.show()
Output: 输出:
You probably need to construct a full timestamp object since it'll default to today's date, which has no data in your case. 您可能需要构造一个完整的时间戳对象,因为它将默认为今天的日期,该日期中没有数据。 the following snippet shoudl replace the
ax1.set_xlim
line in your code, and should also work for starting and ending multiday time ranges on specific hours of your choosing. 以下代码段替换代码中的
ax1.set_xlim
行,并且也应适用于您选择的特定时间段的开始和结束多日时间范围。
min_h = 9 # hours
max_h = 21 # hours
start = dayX['timeStamp'].min()
end = dayX['timeStamp'].max()
xmin = pd.Timestamp(year=start.year, month=start.month, day=start.day, hour=min_h)
xmax = pd.Timestamp(year=end.year, month=end.month, day=end.day, hour=max_h)
ax1.set_xlim(xmin, xmax)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.