0

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

1
  • What kind of plot are you looking for? Line, bar, scatter, something else? Commented Feb 2, 2016 at 20:58

2 Answers 2

0

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:

http://matplotlib.org/

The documentation is very thorough

You may find this SO answer very useful as well:

How to plot data with Python from text file

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.