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

Recommended Answers

All 2 Replies

Use split, indexing and float.

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.

commented: Thank you for letting me know about numpy.txt(). It works! +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.