user3855104
user3855104

Reputation:

Python - Plotting A Graph Using Txt Files Data

This is my Python code for plotting graph from txt file:

import matplotlib.pyplot as plt

x = []
y = []

fig = plt.figure()
rect = fig.patch
rect.set_facecolor('#31312e')

readFile = open('data.txt', 'r')
sepFile = readFile.read().split('\n')
readFile.close()

for plotPair in sepFile:
    xAndY = plotPair.Split(';')
    x.append(int(xAndY[0]))
    y.append(int(xAndY[1]))

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

plt.show()

This is how my data.txt look like:

[info]
Datum=100221
[messung]
Uhrzeit;Interval;AMB_TEMP;IRAD;W_D;W_S;Poly_M_TEMP;TF_M_TEMP
;s;DegC;W/m2;Deg;m/s;DegC;DegC
[Start]
00:15:00;900;26.1;55.8;5.5;1.0;;
00:30:00;900;26.1;55.8;6.1;1.0;;
00:45:00;900;26.1;55.9;5.7;0.9;;
01:00:00;900;26.1;55.9;5.8;0.7;;
01:15:00;900;26.1;55.8;6.4;0.8;;
01:30:00;900;26.1;55.8;6.1;0.8;;
01:45:00;900;26.1;55.8;5.7;1.0;;
02:00:00;900;26.0;55.8;5.8;1.1;;
02:15:00;900;25.9;55.9;5.4;1.1;;
02:30:00;900;25.8;55.9;5.9;0.9;;
02:45:00;900;25.8;55.9;8.0;1.0;;
03:00:00;900;25.8;55.8;7.2;0.9;;
03:15:00;900;25.7;55.8;11.1;0.7;;
03:30:00;900;25.6;55.9;8.5;1.0;;
03:45:00;900;25.7;55.8;6.8;1.1;;
04:00:00;900;25.7;55.8;6.8;0.9;;
04:15:00;900;25.7;55.8;7.0;0.9;;
04:30:00;900;25.6;55.8;6.6;0.6;;
04:45:00;900;25.7;55.8;6.3;0.5;;
05:00:00;900;25.6;55.8;6.1;0.5;;
05:15:00;900;25.5;55.8;5.6;0.8;;
05:30:00;900;25.5;55.8;5.0;0.6;;
05:45:00;900;25.5;55.8;5.2;0.7;;
06:00:00;900;25.5;55.8;5.1;0.7;;
06:15:00;900;25.4;55.8;5.5;0.6;;
06:30:00;900;25.4;55.8;6.1;0.6;;
06:45:00;900;25.4;55.8;5.9;0.6;;
07:00:00;900;25.4;55.8;6.1;0.7;;
07:15:00;900;25.3;55.8;6.2;0.9;;
07:30:00;900;25.4;55.8;5.8;0.9;;
07:45:00;900;25.5;57.4;6.1;0.8;;
08:00:00;900;25.7;68.7;5.9;0.8;;
08:15:00;900;26.0;85.5;6.1;0.8;;
08:30:00;900;26.2;95.5;5.6;0.9;;
08:45:00;900;26.4;110.5;5.5;1.0;;
09:00:00;900;26.8;137.7;5.7;1.2;;
09:15:00;900;27.4;175.7;5.6;1.3;;
09:30:00;900;28.1;223.1;6.0;1.6;;
09:45:00;900;28.7;275.1;5.9;1.9;;
10:00:00;900;29.5;317.7;6.1;2.5;;
10:15:00;900;31.3;633.4;6.3;2.8;;
10:30:00;900;31.4;601.3;6.0;3.0;;
10:45:00;900;32.6;719.6;6.4;3.1;;
11:00:00;900;32.6;695.0;6.5;2.9;;
11:15:00;900;32.8;656.7;6.7;2.5;;
11:30:00;900;33.3;755.1;6.6;2.7;;
11:45:00;900;33.5;773.4;6.4;2.7;;
12:00:00;900;34.0;912.4;6.1;3.0;;
12:15:00;900;34.0;842.2;5.9;3.2;;
12:30:00;900;34.1;594.6;6.5;2.3;;
12:45:00;900;33.7;755.2;7.2;2.6;;
13:00:00;900;34.2;560.3;6.1;2.5;;
13:15:00;900;33.4;437.0;6.9;2.2;;
13:30:00;900;32.7;411.4;6.2;2.7;;
13:45:00;900;32.9;296.0;7.1;1.8;;
14:00:00;900;32.1;289.3;6.9;2.5;;
14:15:00;900;33.2;441.0;6.2;2.1;;
14:30:00;900;31.8;275.0;5.9;2.6;;
14:45:00;900;31.1;206.9;6.7;2.6;;
15:00:00;900;31.0;294.3;6.1;2.1;;
15:15:00;900;33.7;750.2;6.2;2.8;;
15:30:00;900;35.0;729.4;6.6;2.6;;
15:45:00;900;33.4;480.6;6.1;3.2;;
16:00:00;900;33.5;502.6;6.8;3.0;;
16:15:00;900;33.1;391.8;6.6;2.3;;
16:30:00;900;33.3;490.9;6.7;2.8;;
16:45:00;900;33.2;419.9;6.6;2.7;;
17:00:00;900;31.2;168.5;6.2;2.7;;
17:15:00;900;30.5;147.6;6.5;2.8;;
17:30:00;900;30.0;96.0;7.3;2.0;;
17:45:00;900;28.0;58.0;14.4;2.1;;
18:00:00;900;25.2;57.0;20.3;3.1;;
18:15:00;900;23.7;58.0;19.7;2.6;;
18:30:00;900;23.5;55.9;19.6;1.9;;
18:45:00;900;23.8;55.8;23.4;1.2;;
19:00:00;900;24.1;56.5;18.6;0.5;;
19:15:00;900;24.4;57.6;17.7;0.3;;
19:30:00;900;24.8;56.8;9.7;0.3;;
19:45:00;900;25.1;55.8;5.4;0.4;;
20:00:00;900;25.0;55.8;7.8;0.3;;
20:15:00;900;25.2;55.8;6.7;0.5;;
20:30:00;900;25.2;55.8;5.9;0.8;;
20:45:00;900;25.2;55.8;5.6;0.8;;
21:00:00;900;25.0;55.8;5.6;1.0;;
21:15:00;900;24.9;55.8;5.7;1.3;;
21:30:00;900;24.9;55.8;5.8;1.2;;
21:45:00;900;24.9;55.8;5.7;1.0;;
22:00:00;900;25.0;55.8;6.0;0.8;;
22:15:00;900;25.0;55.8;6.0;0.9;;
22:30:00;900;25.0;55.8;5.9;1.0;;
22:45:00;900;25.0;55.7;6.1;0.6;;
23:00:00;900;25.0;55.8;5.2;0.4;;
23:15:00;900;25.2;55.8;5.7;0.5;;
23:30:00;900;25.3;55.8;6.2;0.5;;
23:45:00;900;25.4;55.8;5.8;0.4;;
24:00:00;900;25.3;55.8;4.5;0.4;;

When i run module in my python: it say error: ValueError: invalid literal for int() with base 10: '[info]r'

In my txt file i got 6 columns, how can choose which column to be plotted as a graph?

Upvotes: 1

Views: 1247

Answers (2)

zinjaai
zinjaai

Reputation: 2385

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 > 5:
        xAndY = plotPair.split(';')
        time_string = xAndY[0]
        time_string = time_string.replace(' ', '')  # remove blanks
        datetime_obj = datetime.strptime(time_string, '%H:%M:%S')
        t.append(datetime_obj)
        x.append(float(xAndY[2]))
        y.append(float(xAndY[3]))

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

plt.show()

You have to ignore the first 6 header rows. I'm using if idx > 5: for this purpose.

I changed the columns that will be printed to [2] and [3]. If you want to plot the first column you have to handle the : in the strings.

Changed int() into float() for casting the string into a number. For more information on that aspect see: Parse String to Float or Int

now with datetime.

However: 24:00:00 should be written in the data file as 00:00:00

Upvotes: 2

jrjc
jrjc

Reputation: 21873

With Pandas, 3 lines:

import pandas as pd
df = pd.read_table("file",skiprows=6, sep=";", index_col=0,
                  parse_dates=True, header=None, 
                  names=["Interval","AMB_TEMP","IRAD","W_D","W_S","Poly_M_TEMP","TF_M_TEMP"]
df.AMB_TEMP.plot()
  • skiprows allows to skip the 6 first lines
  • index_col and parse_date, make the first column being the index and parsed as date
  • names : names of the column since there is no header.

Then plot of column AMB_TEMP, it could have been :

df.ix[:,1:5].plot() 

to plot columns from AMB_TEMP to W_S.

With df being a dataframe:

df.head()

                     Interval  AMB_TEMP  IRAD  W_D  W_S  Poly_M_TEMP  \
2014-07-22 00:15:00       900      26.1  55.8  5.5  1.0          NaN   
2014-07-22 00:30:00       900      26.1  55.8  6.1  1.0          NaN   
2014-07-22 00:45:00       900      26.1  55.9  5.7  0.9          NaN   
2014-07-22 01:00:00       900      26.1  55.9  5.8  0.7          NaN   
2014-07-22 01:15:00       900      26.1  55.8  6.4  0.8          NaN   

                     TF_M_TEMP  
2014-07-22 00:15:00        NaN  
2014-07-22 00:30:00        NaN  
2014-07-22 00:45:00        NaN  
2014-07-22 01:00:00        NaN  
2014-07-22 01:15:00        NaN  

Upvotes: 2

Related Questions