Member Avatar for dumicom

hi guys below is my attempted code, how can i plot date and time(x-axis) versus a value (y-axis)

import matplotlib.pyplot as plt
from datetime import time, datetime

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 idx > 1:  # to skip the first line
        xAndY = plotPair.split(',')
        time_string = xAndY[0]
        datetime_obj = datetime.strptime(time_string, '%d/%m%Y %H:%M:%S')
        t.append(datetime_obj)
        y.append(float(xAndY[1]))

ax1 = fig.add_subplot(1, 1, 1, axisbg='blue')
ax1.plot(t, y, 'c', linewidth=3.3)

plt.title('IRRADIANCE')
plt.xlabel('TIME')

plt.show()

this is my txt file:

TimeStamp,Irradiance
21/7/2014 0:00,0.66
21/7/2014 0:00,0.71
21/7/2014 0:00,0.65
21/7/2014 0:00,0.67
21/7/2014 0:01,0.58
21/7/2014 0:01,0.54
21/7/2014 0:01,0.63
21/7/2014 0:01,0.65
21/7/2014 0:02,0.64
21/7/2014 0:02,0.63
21/7/2014 0:02,0.63
21/7/2014 0:02,0.64
.
.
. 
.
22/7/2014 23:57,0.53
22/7/2014 23:58,0.69
22/7/2014 23:58,0.61
22/7/2014 23:58,0.65
22/7/2014 23:58,0.59
22/7/2014 23:59,0.63
22/7/2014 23:59,0.67
22/7/2014 23:59,0.68
22/7/2014 23:59,0.58

but it doesn't seem to work (my code): ValueError: time data '' does not match format '%d/%m/%Y %H:%M'

Use

    time_format = '%d/%m/%Y %H:%M'
    try:
        datetime_obj = datetime.strptime(time_string, time_format)
    except ValueError:
        print("STRING: ", repr(time_string), "FORMAT: ", repr(time_format))
        raise

and post the output.

Member Avatar for dumicom

hi where do i place this in my code?

Replace line 20 by the whole block of code.

Member Avatar for dumicom

Gribouillis, i had done it but with another method, this is my full code:

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()
fig.show()

it works but the problem i'm having is when i maximise the graph's window , the program will hang and at first i thought the problems is because my txt file data is too large(ard ten thousands lines) so i try reducing the data down to 5 lines, but it still hang when i maximise the window. (i need to maximise the window in order to see the entire data.

I tried with the 22 data rows above. I had to replace line 36 with plt.show() and comment line 11. It seems to work and I don't know what you mean by the program will hang. I'm on linux x86_64 with python 2.7.

Member Avatar for dumicom

hi Gribouillis, thanks the fault is at fig.show() when i change it to plt.show() it works.

how can i ask the user to enter "from time" & "to time" & plotting these selected points?

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.