Member Avatar for dumicom

Hi Guys, I'm using Python 2.7.3, How can I ask the user to enter "from datetime" & "to datetime" then plot the graph
For eg. when user
from datetime: 21/7/2014 0:00
to 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()

Recommended Answers

All 6 Replies

Install humanfriendly from pypi (pip install humanfriendly), then

from datetime import datetime
from humanfriendly import parse_date

s = raw_input('From datetime: ')
dt = datetime(*parse_date(s))
print(dt)
Member Avatar for dumicom

so since i want two timings from the user before plotting, is this how the codes will look? [the graph will be plotting just the timing between these two timings]:

    from datetime import datetime
    from humanfriendly import parse_date
    import matplotlib.pyplot as plt
    import matplotlib.dates as mdates


    s = raw_input('From datetime: ')
    s2 = raw_input('To datetime: ')
    dt = datetime(*parsedate(s))
    print(dt)


    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()

Yes, only you need to parse the two dates and may be write a loop to catch user errors, for example

from humanfriendly import parse_date, InvalidDate
from datetime import datetime

def ask_date(msg):
    while True:
        try:
            s = raw_input(msg)
            return datetime(*parse_date(s))
        except (InvalidDate, ValueError) as err:
            print(err)

dt1 = ask_date("From datetime: ")
dt2 = ask_date("To datetime: ")

You must also use dt1 and dt2 somewhere later in the code.

Edit: changed the except clause to catch ValueError.

Member Avatar for dumicom

hi Gribouillis, i'll try your suggestion and get back to you, thanks alot for the help!

Member Avatar for dumicom

how can i ask the user for the input on the graph window instead of on the python shell window ?

this is the code: but the program ask the user input on the python shell window before the graph window appear

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()

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()

I don't think you can do that with matplotlib. You'll have to use one of the gui toolkits to build a dialog widget.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.