Hi guys, I would like to plot with this code a data from text file.:
from datetime import datetime import matplotlib.pyplot as plt import matplotlib.dates as mdates x = [] y = [] t = [] fig = plt.figure()
readFile = open('20.txt', 'r') sepFile = readFile.read().split('\n') readFile.close() for idx, plotPair in enumerate(sepFile): if plotPair in '. ':
continue if idx > 1: xAndY = plotPair.split('\t') time_string = xAndY[0]+" "+xAndY[1] time_string1 = datetime.strptime(time_string, '%d/%m/%Y %H:%M') t.append(time_string1) y.append(float(xAndY[2]))
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, 'r-', linewidth=3.3) plt.title('Subordinate prediction') plt.xlabel('DATA/TIME') plt.ylabel('Height (Meters relative to MLLW)') fig.autofmt_xdate(rotation=45) fig.tight_layout() fig.show()
but I have this error:
ella\fo\dataTime.py", line 19, in <module> time_string = xAndY[0]+" "+xAndY[1] NameError: name 'xAndY' is not defined
what is wrong? How can I figure out? Thanks so much, for your help!!
the text file is like this below
01/12/2014 4:36 1.6 01/12/2014 10:20 0.45 01/12/2014 16:50 2 01/12/2014 23:28 0.21 02/12/2014 5:52 1.54 02/12/2014 11:11 0.48 02/12/2014 17:40 2.14 03/12/2014 0:28 0.04 03/12/2014 6:54 1.51 03/12/2014 11:58 0.48 03/12/2014 18:26 2.25 04/12/2014 1:20 -0.09 04/12/2014 7:46 1.48 04/12/2014 12:43 0.46 04/12/2014 19:09 2.32 05/12/2014 2:08 -0.17 05/12/2014 8:32 1.44 05/12/2014 13:27 0.44
thanks so much,
Cristina
Log in to post a comment.
Hi guys,
I would like to plot with this code a data from text file.:
from datetime import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
x = []
y = []
t = []
fig = plt.figure()
readFile = open('20.txt', 'r')
sepFile = readFile.read().split('\n')
readFile.close()
for idx, plotPair in enumerate(sepFile):
if plotPair in '. ':
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, 'r-', linewidth=3.3)
plt.title('Subordinate prediction')
plt.xlabel('DATA/TIME')
plt.ylabel('Height (Meters relative to MLLW)')
fig.autofmt_xdate(rotation=45)
fig.tight_layout()
fig.show()
but I have this error:
ella\fo\dataTime.py", line 19, in <module>
time_string = xAndY[0]+" "+xAndY[1]
NameError: name 'xAndY' is not defined
what is wrong? How can I figure out?
Thanks so much, for your help!!
the text file is like this below
01/12/2014 4:36 1.6
01/12/2014 10:20 0.45
01/12/2014 16:50 2
01/12/2014 23:28 0.21
02/12/2014 5:52 1.54
02/12/2014 11:11 0.48
02/12/2014 17:40 2.14
03/12/2014 0:28 0.04
03/12/2014 6:54 1.51
03/12/2014 11:58 0.48
03/12/2014 18:26 2.25
04/12/2014 1:20 -0.09
04/12/2014 7:46 1.48
04/12/2014 12:43 0.46
04/12/2014 19:09 2.32
05/12/2014 2:08 -0.17
05/12/2014 8:32 1.44
05/12/2014 13:27 0.44
thanks so much,
Cristina