In the previous blog post we showed how to import and plot data using pure python and using numpy package. In those we had only two data column, representing x and y values. Consider the case you have the x data in the first column and you then have 2nd and 3rd or more data column representing other data values. How can you plot these multiple column data?

It is easy to extend the idea of the program in the post Import and plot data using Numpy.

The python code is as follows:

import numpy as np

import matplotlib.pyplot as plt

fobj = np.loadtxt('f.txt')

x = fobj[:,0]

y1 = fobj[:,1]

y2 = fobj[:,2]

plt.plot(x,y1)

plt.plot(x,y2)

plt.show()

What we did in the program is that we imported using the numpy method loadtxt( ). This function imports data as array.

[In] fobj

[Out]

array([[ 0., 10., 30.],

[ 1., 8., 12.],

[ 2., 4., 20.],

[ 4., 39., 5.],

[ 5., 25., 9.],

[ 6., 20., 12.],

[ 7., 10., 21.],

[ 8., 10., 0.],

[ 9., 42., 11.],

[ 10., 11., 37.]])

Then we assign each column to x, y1 and y2.

Finally we plot y1 and y2 against x using the plt.plt( ) function of matplotlib library.

It is easy to extend the idea of the program in the post Import and plot data using Numpy.

The python code is as follows:

import numpy as np

import matplotlib.pyplot as plt

fobj = np.loadtxt('f.txt')

x = fobj[:,0]

y1 = fobj[:,1]

y2 = fobj[:,2]

plt.plot(x,y1)

plt.plot(x,y2)

plt.show()

What we did in the program is that we imported using the numpy method loadtxt( ). This function imports data as array.

[In] fobj

[Out]

array([[ 0., 10., 30.],

[ 1., 8., 12.],

[ 2., 4., 20.],

[ 4., 39., 5.],

[ 5., 25., 9.],

[ 6., 20., 12.],

[ 7., 10., 21.],

[ 8., 10., 0.],

[ 9., 42., 11.],

[ 10., 11., 37.]])

Then we assign each column to x, y1 and y2.

Finally we plot y1 and y2 against x using the plt.plt( ) function of matplotlib library.

## 0 comments:

## Post a Comment