在绘制选定点之前询问用户的日期时间

时间:2014-07-25 04:53:59

标签: python datetime graph

我正在使用Python 2.7.3,如何让用户输入“from datetime”&在将所选标准绘制到图表上之前,“到日期时间”    例如。当用户    从datetime:21/7/2014 0:00    到datetime:22/7/2014 23:57

from datetime import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
x = []
y = []
t = []
fig = plt.figure()
rect = fig.patch
rect.set_facecolor('#31312e')
readFile = open('data.txt', 'r')
sepFile = readFile.read().split('\n')
readFile.close()
for idx, plotPair in enumerate(sepFile):
    if plotPair in '. ':
        # skip. or space
        continue
    if idx > 1:  # to skip the first line
        xAndY = plotPair.split(',')
        time_string = xAndY[0]
        time_string1 = datetime.strptime(time_string, '%d/%m/%Y %H:%M')
        t.append(time_string1)
        y.append(float(xAndY[1]))
ax1 = fig.add_subplot(1, 1, 1, axisbg='white')
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%Y %H:%M'))
ax1.plot(t, y, 'c', linewidth=3.3)
plt.title('IRRADIANCE')
plt.xlabel('TIME')
fig.autofmt_xdate(rotation=45)
fig.tight_layout()
plt.show()

1 个答案:

答案 0 :(得分:0)

试试这个。

#!/usr/bin/env python
#-*- coding:utf-8 -*-

from datetime import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
x = []
y = []
t = []
fig = plt.figure()
rect = fig.patch
rect.set_facecolor('#31312e')
readFile = open('C:\\Temp\\001.txt', 'r')
sepFile = readFile.read().split('\n')
readFile.close()

startTime = raw_input('please enter start time in format like 21/7/2014 0:00 :')
endTime   = raw_input('please enter end time in format like 22/7/2014 23:57 :') 

# startTime = '21/7/2014 0:02'
# endTime = '22/7/2014 23:58'
startTime = datetime.strptime(startTime, '%d/%m/%Y %H:%M')
endTime = datetime.strptime(endTime, '%d/%m/%Y %H:%M')


for idx, plotPair in enumerate(sepFile):
    if plotPair in '. ':
        # skip. or space
        continue
    if idx > 1:  # to skip the first line
        xAndY = plotPair.split(',')
        time_string = xAndY[0]
        time_string1 = datetime.strptime(time_string, '%d/%m/%Y %H:%M')
        if startTime<=time_string1 <=endTime:
            t.append(time_string1)
            y.append(float(xAndY[1]))
ax1 = fig.add_subplot(1, 1, 1, axisbg='white')
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%Y %H:%M'))
ax1.plot(t, y, 'c', linewidth=3.3)
plt.title('IRRADIANCE')
plt.xlabel('TIME')
fig.autofmt_xdate(rotation=45)
fig.tight_layout()
plt.show()