0

I have a written a python code that generates the output in the form of text file. The numbers in the result are very small(of the order of 10^-120) and are written in the scientific notation in the form 1E-120 in the output file. I wish to plot the graph using the output in the text file. How do I plot the graph?

The output is in the form like 1 '\t' 2 stored in a text file. I wish to plot a simple x y plot between the two variables.

I am asking this here since I could not find anything related to this anywhere and I am new to python. Thanks.

3
  • What type of graph? Is it a simple (x,y) plot? How are the coordinate pairs formatted?
    – Maltysen
    Jun 19, 2015 at 22:46
  • Yes. That would help.
    – beginner
    Jun 19, 2015 at 22:48
  • I just need to know how to import data from a file and plot.
    – beginner
    Jun 19, 2015 at 22:49

1 Answer 1

0

Assuming that your values are a x -> y mapping (e.g. a 1D function), plotting these values is very simple. If you have your x values in the iterable (i.e. a list or an array or anything you can iterate over) x and your y values in the iterable y, the you need to import pyplot and do

import matplotlib.pyplot as plt
plt.plot(x, y)
plt.show()

The fact that your values are so small is not a problem. However, for the sake of the presentation you might want to rescale them by just multiplying the values by a number, say 1e120.

EDIT: I see right now that you need to import the data. The easiest way is numpy.loadtxt. Assuming the data is written in your file in matrix format (one column for x, one for y), you will get this data in the same format.

import numpy as np
import matplotlib.pyplot as plt
data = np.loadtxt('path/to/file')
x,y = data[:, 0], data[:,1]
plt.plot(x,y)
plt.show()
7
  • The output is in the form like 1 2. How do I import the data from a text file?
    – beginner
    Jun 19, 2015 at 22:52
  • I edited my answer. See if this solves your problem.
    – Spiros
    Jun 19, 2015 at 22:54
  • It gives an error saying couldn't convert string into float. It is because the numbers are too small as I said in the question details, that I had to print them addressing them as string in the output file. So is there any way to solve this?
    – beginner
    Jun 19, 2015 at 22:59
  • can you help me in solving this?
    – beginner
    Jun 19, 2015 at 23:16
  • I don't fully understand. Can you paste part of the file in your question? Even if the numbers are tiny, there should be no problem in loading them. Numpy converts the contents of the file, which are always strings, into floating points numbers.
    – Spiros
    Jun 20, 2015 at 7:12

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.