How can I plot a graph in python taking the values from a file that it just has values? I mean that there are not two columns representing X and Y axis, just values. For example the file contains:
1.5 6 3 0.2 -1.2 4 9 2.34 0.75 1.2....
Thank you
How can I plot a graph in python taking the values from a file that it just has values? I mean that there are not two columns representing X and Y axis, just values. For example the file contains:
1.5 6 3 0.2 -1.2 4 9 2.34 0.75 1.2....
Thank you
So obviously you will need to know the structure of the data within the file. You can read data from a file very easily:
with open(filename,"r") as f:
data = f.read() # or you can use f.readline()
.....
#Format the data
The matplotlib library is what you want for graphing the data:
The documentation is very thorough
You may find this SO answer very useful as well:
Assuming that the data is formatted as you say in a file with each value separated by a space.
1.5 6 3 0.2 -1.2 4 9 2.34 0.75 1.2
And assuming that it is representing something like time-series data, then you can import and plot a line graph of that data using something like.
import matplotlib.pyplot as plt
filename = "path/to/my_data_file"
# Import data as a list of numbers
with open(filename) as textFile:
data = textFile.read().split() # split based on spaces
data = [float(point) for point in data] # convert strings to floats
# Plot as a time series plot
plt.plot(data)
plt.show()
Ronny