0

Hi,

I'm a beginner at python and I'm trying to extract specific columns from a text file, and then use those columns to make a line plot.

The files contains the following few lines of data:

# Raw SIFs at Crack Propagation Step: 0
# Vertex, X,              Y,              Z,              K_I,            K_II,           K_III,           J
  0      , 2.100000e+00   , 2.000000e+00   , -1.000000e-04  , 0.000000e+00   , 0.000000e+00   , 0.000000e+00   , 0.000000e+00   , 
  1      , 2.100000e+00   , 2.000000e+00   , 1.699733e-01   , 8.727065e+00   , -8.696262e-04  , -1.800691e-04  , 3.465355e-04   , 
  2      , 2.100000e+00   , 2.000000e+00   , 3.367067e-01   , 8.907810e+00   , -2.548819e-04  , -2.789738e-04  , 3.610383e-04   , 
  3      , 2.100000e+00   , 2.000000e+00   , 5.000400e-01   , 8.928808e+00   , -4.642099e-04  , -1.658399e-04  , 3.627425e-04   , 

I would like to plot:
vertex (1st column) versus K_I (5th column)
vertex (1st column) versus K_II (6th column)
vertex (1st column) versus K_III (7th column)

How do I extract the specific columns from this file and then plot a line graph from the columns?

Thanks

3
Contributors
2
Replies
7
Views
4 Years
Discussion Span
Last Post by Gribouillis
1

There is also numpy.loadtxt() to read the file

    import numpy
    with open("numfile.txt") as ifh:
        arr = numpy.loadtxt(ifh, usecols = (2, 3), dtype=float, delimiter=" , ", skiprows = 2)
    print arr

""" my output -->
[[  2.00000000e+00  -1.00000000e-04]
 [  2.00000000e+00   1.69973300e-01]
 [  2.00000000e+00   3.36706700e-01]
 [  2.00000000e+00   5.00040000e-01]]
"""

Use matplotlib to plot the curves.

Edited by Gribouillis

Votes + Comments
Thank you for letting me know about numpy.txt(). It works!
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.