Python Forum
[Solved] Plotting data from txt file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Solved] Plotting data from txt file
#1
Hey! I'm trying to plot a text file that looks like this:

#0      0.4000 0.1485 2.1926  Fixed  0.0195  0.0299  64.9854 11.2706 23.7440 1.4814  1.2203  0.3871  42.1810   0.0664                 
#1      0.4000 0.1457 2.1253  Fixed  0.0185  0.0288  63.7908 11.7439 24.4653 1.4802  1.2043  0.4036  42.2980   0.0670                 
#2      0.4000 0.1429 2.1903  Fixed  0.0189  0.0299  65.3086 11.0723 23.6191 1.4004  1.1423  0.3846  42.2350   0.0677                 
#3      0.4000 0.1369 2.1734  Fixed  0.0179  0.0293  64.8133 11.0524 24.1343 1.3254  1.0651  0.3870  42.2444   0.0694                 
#4      0.4000 0.1393 2.2184  Fixed  0.0216  0.0304  67.2157  9.3551 23.4292 1.3416  1.0907  0.3780  42.1653   0.0704                 
#5      0.4000 0.1494 2.1767  Fixed  0.0217  0.0299  65.9704 10.3180 23.7115 1.5089  1.2435  0.3917  42.1547   0.0674                 
#6      0.4000 0.1499 2.1952  Fixed  0.0213  0.0304  66.1049 10.5957 23.2994 1.5131  1.2522  0.3876  42.2199   0.0672                 
#7      0.4000 0.1238 2.1880  Fixed  0.0167  0.0296  66.2869 10.5109 23.2023 1.1445  0.9037  0.3707  42.2324   0.0725                 
#8      0.4000 0.1457 2.1814  Fixed  0.0222  0.0302  66.9152  9.7170 23.3678 1.4467  1.1857  0.3872  42.1238   0.0686                 
#9      0.4000 0.1309 2.1627  Fixed  0.0169  0.0296  65.1505 11.1807 23.6688 1.2464  0.9946  0.3815  42.3048   0.0711                 
#10     0.4000 0.1226 2.2090  Fixed  0.0169  0.0304  66.6621 10.3485 22.9894 1.1272  0.8923  0.3663  42.2965   0.0740                 
#11     0.4000 0.1382 2.1704  Fixed  0.0188  0.0298  66.2671 10.7010 23.0319 1.3362  1.0828  0.3812  42.2499   0.0690                 
#12     0.4000 0.1878 2.1110  Fixed  0.0240  0.0303  62.1355 13.6391 24.2254 2.4126  2.0972  0.4406  42.1389   0.0598                 
#13     0.4000 0.1619 2.1212  Fixed  0.0213  0.0301  64.2333 11.9293 23.8374 1.7699  1.4809  0.4146  42.2166   0.0641                 
#14     0.4000 0.1529 2.1428  Fixed  0.0190  0.0299  63.9508 12.1658 23.8833 1.5838  1.3072  0.4025  42.2055   0.0651                 
#15     0.4000 0.1456 2.1313  Fixed  0.0212  0.0296  66.0905 10.2328 23.6767 1.4652  1.1931  0.3987  42.1202   0.0685                 
#16     0.4000 0.1260 2.1505  Fixed  0.0190  0.0286  66.9064  9.4892 23.6044 1.1800  0.9331  0.3777  42.1801   0.0739                 
#17     0.4000 0.1515 2.0944  Fixed  0.0212  0.0289  65.4946 10.8854 23.6201 1.5739  1.2954  0.4055  42.1221   0.0668                 
#18     0.4000 0.1725 2.1762  Fixed  0.0234  0.0320  65.5772 11.9181 22.5047 1.9533  1.6754  0.4023  42.1257   0.0619                 
#19     0.4000 0.1285 2.1495  Fixed  0.0185  0.0301  67.2668  9.9654 22.7679 1.2102  0.9621  0.3777  42.2216   0.0724                 
#20     0.4000 0.1326 2.1352  Fixed  0.0188  0.0296  66.5970 10.2232 23.1798 1.2774  1.0217  0.3851  42.2026   0.0712                 
#21     0.4000 0.1691 2.0464  Fixed  0.0229  0.0301  64.5941 11.9354 23.4705 1.9505  1.6390  0.4356  42.1784   0.0626                 
#22     0.4000 0.1354 2.0646  Fixed  0.0179  0.0294  65.9501 11.0197 23.0302 1.3295  1.0600  0.3978  42.2522   0.0695                 
#23     0.4000 0.1483 2.1130  Fixed  0.0192  0.0307  65.6120 11.5982 22.7898 1.5075  1.2344  0.3991  42.2299   0.0666                 
#24     0.4000 0.1406 2.1284  Fixed  0.0194  0.0305  66.7009 10.6242 22.6749 1.3818  1.1187  0.3895  42.2534   0.0685                 
#25     0.4000 0.1729 2.1016  Fixed  0.0218  0.0303  63.6588 12.9516 23.3896 2.0011  1.7045  0.4209  42.1136   0.0614                 
#26     0.4000 0.1447 2.1227  Fixed  0.0199  0.0300  66.5484 10.7446 22.7070 1.4448  1.1773  0.3928  42.2082   0.0671                 
#27     0.4000 0.1366 2.1660  Fixed  0.0221  0.0305  68.5456  8.9745 22.4799 1.3101  1.0596  0.3790  42.1276   0.0709                 
#28     0.4000 0.1419 2.0774  Fixed  0.0190  0.0295  65.3285 11.0722 23.5993 1.4329  1.1526  0.4071  42.2543   0.0680                 
#29     0.4000 0.1461 2.0880  Fixed  0.0223  0.0293  66.8589  9.7719 23.3692 1.4844  1.2068  0.4041  42.1765   0.0684                 
#30     0.4000 0.1644 2.1231  Fixed  0.0223  0.0299  65.3076 11.5743 23.1181 1.7942  1.5126  0.4064  42.1303   0.0633                 
#31     0.4000 0.1411 2.1044  Fixed  0.0199  0.0300  67.0179 10.4432 22.5389 1.3901  1.1253  0.3918  42.1674   0.0688                 
#32     0.4000 0.1646 2.1427  Fixed  0.0251  0.0306  66.3990 10.3496 23.2514 1.8036  1.5215  0.4077  42.0829   0.0648                 
#33     0.4000 0.1589 2.1114  Fixed  0.0223  0.0304  66.1828 11.0755 22.7417 1.7011  1.4206  0.4060  42.1118   0.0649                 
#34     0.4000 0.1523 2.0527  Fixed  0.0218  0.0291  65.6313 10.7024 23.6663 1.6114  1.3211  0.4180  42.1892   0.0671                 
#35     0.4000 0.1522 2.1521  Fixed  0.0198  0.0305  65.4273 11.5670 23.0057 1.5576  1.2895  0.3936  42.2862   0.0650                 
#36     0.4000 0.1301 2.0665  Fixed  0.0188  0.0295  66.7894 10.0438 23.1668 1.2761  1.0041  0.3999  42.1965   0.0724                 
#37     0.4000 0.1285 2.1273  Fixed  0.0180  0.0299  67.1615 10.2937 22.5448 1.2168  0.9663  0.3806  42.2621   0.0726                 
#38     0.4000 0.1367 2.1016  Fixed  0.0195  0.0294  66.7871 10.2103 23.0025 1.3310  1.0664  0.3923  42.1630   0.0698                 
#39     0.4000 0.1958 2.0671  Fixed  0.0277  0.0317  63.9121 12.8755 23.2124 2.6789  2.3509  0.4535  42.0563   0.0588                 
#40     0.4000 0.1315 2.0868  Fixed  0.0186  0.0288  66.4101 10.2574 23.3325 1.2705  1.0084  0.3921  42.2757   0.0715                 
#41     0.4000 0.1653 2.0615  Fixed  0.0225  0.0304  65.4340 11.6680 22.8980 1.8472  1.5490  0.4227  42.1883   0.0635                 
#42     0.4000 0.1709 2.0562  Fixed  0.0215  0.0298  63.7508 12.9209 23.3284 1.9813  1.6762  0.4299  42.1492   0.0620                 
#43     0.4000 0.1347 2.1474  Fixed  0.0180  0.0300  66.1287 10.9189 22.9524 1.3008  1.0446  0.3841  42.2772   0.0700                 
#44     0.4000 0.1352 2.0543  Fixed  0.0178  0.0294  65.6570 11.1316 23.2114 1.3374  1.0626  0.4034  42.2764   0.0696                 
#45     0.4000 0.1642 2.0722  Fixed  0.0233  0.0307  66.0989 11.1780 22.7231 1.8240  1.5301  0.4190  42.1244   0.0640                 
#46     0.4000 0.1650 2.0688  Fixed  0.0235  0.0300  65.4304 11.1399 23.4297 1.8424  1.5452  0.4230  42.1477   0.0638                 
#47     0.4000 0.1549 2.0342  Fixed  0.0202  0.0292  64.6196 11.8704 23.5100 1.6630  1.3677  0.4222  42.2095   0.0653                 
#48     0.4000 0.1770 2.0805  Fixed  0.0253  0.0308  65.1282 11.6274 23.2445 2.1167  1.8093  0.4329  42.1227   0.0617                 
#49     0.4000 0.1734 2.0832  Fixed  0.0266  0.0307  65.9513 10.7070 23.3416 2.0332  1.7295  0.4289  42.0954   0.0627                 
#50     0.4000 0.1077 2.1400  Fixed  0.0162  0.0291  67.6514  9.4889 22.8597 0.9908  0.7600  0.3672  42.3381   0.0804                 
#51     0.4000 0.1151 2.1446  Fixed  0.0181  0.0299  68.1800  9.0882 22.7318 1.0691  0.8307  0.3735  42.2072   0.0784                 
#52     0.4000 0.1449 2.1191  Fixed  0.0202  0.0295  65.8096 10.6812 23.5092 1.4621  1.1885  0.3994  42.1852   0.0677                 
#53     0.4000 0.1452 2.1133  Fixed  0.0229  0.0298  67.6496  9.4322 22.9182 1.4607  1.1900  0.3969  42.1365   0.0689                 
#54     0.4000 0.1404 2.0963  Fixed  0.0201  0.0294  65.9752 10.3499 23.6749 1.4074  1.1345  0.4012  42.2226   0.0698                 
#55     0.4000 0.1847 2.0944  Fixed  0.0254  0.0305  63.6377 12.5124 23.8499 2.3246  2.0121  0.4377  42.1622   0.0607                 
#56     0.4000 0.1424 2.1103  Fixed  0.0206  0.0292  66.2297 10.2403 23.5300 1.4232  1.1512  0.3989  42.2029   0.0692                 
#57     0.4000 0.1797 2.1408  Fixed  0.0234  0.0313  63.8664 12.8094 23.3242 2.1499  1.8519  0.4214  42.1289   0.0606                 
#58     0.4000 0.1312 2.1708  Fixed  0.0237  0.0297  68.5779  7.9633 23.4588 1.2490  0.9971  0.3817  42.1462   0.0739                 
#59     0.4000 0.1516 2.0907  Fixed  0.0200  0.0295  64.7978 11.5618 23.6403 1.5822  1.2986  0.4099  42.1454   0.0664                 
I want to be able to plot all columns (except for the numbering and 'Fixed' ones), but I suppose the problem is that the indices aren't consistent in the file. I want to plot stuff from the file in the y-axis, leaving x as a defined range. I tried creating a dataframe:

df = pd.read_csv(out)
print(df)
x = range(0, 80)
y = df[:,1]
df.plot(x, y)
with this error:

Traceback (most recent call last):

  File "<ipython-input-7-f8159069ae40>", line 1, in <module>
    runfile('C:/.../Python scripts/palsfit_output.py', wdir='C:/.../Python scripts')

  File "C:\...\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 710, in runfile
    execfile(filename, namespace)

  File "C:\...\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 101, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/.../Python scripts/palsfit_output.py", line 156, in <module>
    y = df[:,1]

  File "C:\...\Anaconda3\lib\site-packages\pandas\core\frame.py", line 1964, in __getitem__
    return self._getitem_column(key)

  File "C:\...\Anaconda3\lib\site-packages\pandas\core\frame.py", line 1971, in _getitem_column
    return self._get_item_cache(key)

  File "C:\...\Anaconda3\lib\site-packages\pandas\core\generic.py", line 1643, in _get_item_cache
    res = cache.get(item)

TypeError: unhashable type: 'slice'
And using plt.plot:

with open(out) as file:
    data = file.read()
    print(data)

x = range(0,80)
print(x)
y = data[:,5]
print(y)
plt.plot(x,y, c='r', label='data')
plt.legend()
plt.show()
with an error:

Traceback (most recent call last):

  File "<ipython-input-10-f8159069ae40>", line 1, in <module>
    runfile('C:/.../Python scripts/palsfit_output.py', wdir='C:/.../Python scripts')

  File "C:\...\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 710, in runfile
    execfile(filename, namespace)

  File "C:\...\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 101, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/.../Python scripts/palsfit_output.py", line 168, in <module>
    y = data[:,5]

TypeError: string indices must be integers
I assume the error is in that the file is not spaced consistently, and thus reading columns doesn't work. How can this be fixed?
Reply
#2
I've now changed the composition of the values so that I have everything in a list. Now what I'd have to do is stack every 8th value into a column somehow, if that's possible, or create a loop to calculate the averages of every 8th value etc. The output is now this:

[['#0'], ['0.4000'], ['0.1500'], ['1.5187'], ['Fixed'], ['Fixed'], ['0.0473'], ['76.2093'], ['8.8510'], ['14.9398'], ['1.3789'], ['0.9853'], ['0.5720'], ['40.4648'], ['0.0551'], ['#1'], ['0.4000'], ['0.1500'], ['1.5094'], ['Fixed'], ['Fixed'], ['0.0450'], ['74.3954'], ['9.9777'], ['15.6269'], ['1.3704'], ['0.9724'], ['0.5760'], ['40.5128'], ['0.0545'], ['#2'], ['0.4000'], ['0.1500'], ['1.5491'], ['Fixed'], ['Fixed'], ['0.0468'], ['74.7040'], ... ['Fixed'], ['Fixed'], ['0.0536'], ['77.1376'], ['8.7556'], ['14.1069'], ['1.2754'], ['0.9602'], ['0.4889'], ['40.5042'], ['0.0548']]
(with this code):

my_file = open(out, "r")
content = my_file.read()

content_list = content.split()
my_file.close()

myList = content_list
myList = [i.split('\n') for i in myList] 
del myList[0:10]
print(myList)
So now I'd have to calculate averages of every 8th value, as well as plot them. I suppose it's easier this way: is there a method to stack the values into columns somehow, is the only option loop over the values? Any help is greatly appreciated!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] Correct way to convert file from cp-1252 to utf-8? Winfried 8 875 Feb-29-2024, 12:30 AM
Last Post: Winfried
  Loop through json file and reset values [SOLVED] AlphaInc 2 2,143 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  [Solved by deanhystad] Create a zip file using zipfile library DZ_Galaxy 2 1,166 Aug-17-2022, 04:57 PM
Last Post: DZ_Galaxy
  [SOLVED] Concat data from dictionary? Winfried 4 1,734 Mar-30-2022, 02:55 PM
Last Post: Winfried
  Updating a config file [solved] ebolisa 8 2,597 Nov-04-2021, 10:20 AM
Last Post: Gribouillis
  |SOLVED] Glob JPGs, read EXIF, update file timestamp? Winfried 5 2,495 Oct-21-2021, 03:29 AM
Last Post: buran
  [SOLVED] Read text file from some point till EOF? Winfried 1 1,963 Oct-10-2021, 10:29 PM
Last Post: Winfried
  [SOLVED] Input parameter: Single file or glob? Winfried 0 1,588 Sep-10-2021, 11:54 AM
Last Post: Winfried
Thumbs Up [SOLVED] Find last occurence of pattern in text file? Winfried 4 4,411 Aug-13-2021, 08:21 PM
Last Post: Winfried
Thumbs Up Parsing a YAML file without changing the string content..?, Flask - solved. SpongeB0B 2 2,280 Aug-05-2021, 08:02 AM
Last Post: SpongeB0B

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020