"Reading and writing files" is available in the 2014 version of the lecture here: http://python-astro.blogspot.mx/2014/09/interacting-with-files-reading-writing.html
In this second post, we will read the file we wrote in the previous one (datas.dat), and use the data to draw a plot.
We will now have to import the plotting package, which is part of matplotlib (we will also need numpy):
import matplotlib.pyplot as plt
import numpy as np
There is a lot of different ways to read a file, depending if it is ASCII or fits or Binary, if we want to extract only some columns, if we know the format of the data, etc... Even for a given data file, for example the one we did last post, I'll present various ways, to illustrate some functions of python.
We now open the file with the 'r' parameter, to specify that we will read it:
f2 = open('datas.dat', 'r')
'r' is optional.
We read the whole file in a single variable, we will deal with it latter. If the file is really big, and/or we only want a small part of it (we are looking for some line, or only one column is of interest) we may have to proceed differently:
lines = f2.readlines()
Now that all the datas are in the lines variable, we can close the file and forget it:
f2.close()
lines contains the 200 values from the 2 vectors x and y, but we will have to extract this information, as it is now in an unusable form.
First we can ask for the type of the variable:
type(lines)
list A list is... a list of objects. It can handle objects of different types, even other lists.
We can print the length of the list, it is the number of elements it contains:
len(lines)
100We can print the first element:
print(lines[0])
0.000000 0.000000 It seems it is the first line of our file, nice!
But it is a single string...:
print(type(lines[0]))
<type 'str'>
We will have to use a method of the object string to split the line into the 2 values:
print(lines[0].split())
['0.000000', '0.000000']
The result is another list, with 2 elements, each one being a string with the value of x and y corresponding to the line.
We can now access to the value of x, and transform it into a floating point value:
print(float(lines[0].split()[0]))
0.0
We will now loop on all the lines of the file (they are stored in the lines variable) and do the same.
We want to store the result in 2 lists. We first then need to define these lists, that will later be filled by the values of x and y.
x1 = []
y1 = []The two empty lists are now ready. We can loop on files without to know the number of elements, python take care of this:
for line in lines:
p = line.split()x1.append(float(p[0]))
y1.append(float(p[1]))
We use p as an intermediate variable, to store the list of 2 strings (like ['0.000000', '0.000000'] for the first line).
append is a method applied to the lists x1 and y1, to insert a new element to the end of the list.
We now have the x and y vectors in the 2 variables x1 and y1.
We can transform the two lists into numpy vectors, to have more flexibility on them:
xv = np.array(x1)
yv = np.array(y1)
We can plot them:
plt.plot(xv, yv)
To see the plot, one last command must be sent:
plt.show()
A window should pop-up with a nice sin(x) plot!
All the previous commands can be stored in a file, called for example Ex2.py and execute by python.
From the shell:
LINUX> python Ex2.py
or from with python
import Ex2
or from ipython
%run Ex2
Here is the script of Ex2.py:
# Need to import the plotting package:
import matplotlib.pyplot as plt
# Read the file.
f2 = open('datas.dat', 'r')
# read the whole file into a single variable, which is a list of every row of the file.
lines = f2.readlines()
f2.close()
# initialize some variable to be lists:
x1 = []
y1 = []
# scan the rows of the file stored in lines, and put the values into some variables:
for line in lines:
p = line.split()
x1.append(float(p[0]))
y1.append(float(p[1]))
xv = np.array(x1)
yv = np.array(y1)
# now, plot the data:
plt.plot(xv, yv)
plt.show()
import matplotlib.pyplot as plt
# Read the file.
f2 = open('datas.dat', 'r')
# read the whole file into a single variable, which is a list of every row of the file.
lines = f2.readlines()
f2.close()
# initialize some variable to be lists:
x1 = []
y1 = []
# scan the rows of the file stored in lines, and put the values into some variables:
for line in lines:
p = line.split()
x1.append(float(p[0]))
y1.append(float(p[1]))
xv = np.array(x1)
yv = np.array(y1)
# now, plot the data:
plt.plot(xv, yv)
plt.show()
To learn more on lists:
- http://docs.python.org/tutorial/introduction.html#lists
- http://docs.python.org/tutorial/datastructures.html
- http://www.tutorialspoint.com/python/python_lists.htm
To learn more on plot (but we'll coma back to this latter):
Thank you very much, very helpful and detailed explanantions.
ReplyDeleteVery helpful to understand the process!!! Thank you!
ReplyDeletethank you!!! very understandable!
ReplyDeleteawesome
ReplyDeleteVery understandable!
ReplyDeleteps> As you said before you need to add in the file `Ex2.py` the line
import numpy as np
Well done, extremely clear explanations! Many thanks
ReplyDeleteThank you for the tutorial! Very clear!
ReplyDeletethe best explanation .perfect.thx
ReplyDeleteIt's really helpful! Thank you! :)
ReplyDeleteThank You. It was of such a great help.
ReplyDeleteThank You. It was of such a great help.
ReplyDeleteHow to write this file in proper column format ? My data set is coming in this order.
ReplyDeleteW h=200/real
----------------------------------------------------------------------
200 0.99132727
220 0.99296962
240 0.99445066
260 0.99563624
280 0.99634305
300 0.99601406
320 0.99420797
340 0.99084941
360 0.98501062
380 0.97673276
400 0.96430636
420 0.94842594
440 0.92739405
460 0.8972507
480 0.84528364
500 0.71146541
520 0.059702639
540 0.86861456
W h=210/real
----------------------------------------------------------------------
200 0.9917491
220 0.9933769
240 0.99498827
260 0.99624275
280 0.99701121
300 0.99700354
320 0.99560914
340 0.99291131
360 0.98785958
380 0.98048542
400 0.9691417
420 0.95409132
440 0.93358553
460 0.90301787
480 0.84781975
500 0.67844863
520 0.27273553
540 0.75944457
W h=220/real
----------------------------------------------------------------------
200 0.9920283
220 0.99374654
240 0.99527633
260 0.9967577
280 0.99765434
300 0.99768789
320 0.99680027
340 0.99467139
360 0.99002753
380 0.98275479
400 0.97067326
420 0.95275926
440 0.9262277
460 0.88752239
480 0.83214671
500 0.67636413
520 0.18564648
540 0.42878402
Thank you! Your post is very helpful! This is the first time I can plot a graph from a data file by using python.
ReplyDeleteMore on...Python List
ReplyDeleteHey there outstanding website! Does running a blog similar to this take a large amount of work? I've absolutely no expertise in coding however I was hoping to start my own blog in the near future. Anyways, if you have any ideas or techniques for new blog owners please share. I understand this is off subject nevertheless I just needed to ask. Many thanks!
ReplyDeleteI do not even know how I ended up here, but I thought this post was great. I don't know who you are but definitely you are going to a famous blogger if you aren't already ;) Cheers!
ReplyDeleteWe are also noticing that traditional medical doctors are also recognizing the increased interest in holistic healing and are finally starting to incorporate these healing methods into their practice to keep up with the current trends. There are many ways to obtain a construction job interview, but some are more effective than others. https://python.engineering/16150819-common-xlabel_ylabel-for-matplotlib-subplots/
ReplyDelete