Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
333 views
in Technique[技术] by (71.8m points)

python - How to plot files with numpy?

I have a .dat file that contains two columns of numbers so it looks something like this:

111    112
110.9  109
103    103

and so on.

I want to plot the two columns against one another. I have never dealt with a .dat file before so I am not sure where to start.

So far I figured out that numpy has something I can use to call.

data = numpy.loadtxt('data.DAT')

but I'm not sure where to go from here. Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Numpy doesn't support plotting by itself. You usually would use matplotlib for plotting numpy arrays.

If you just want to "look into the file", I think the easiest way would be to use plotfile.

import matplotlib.pyplot as plt 

plt.plotfile('data.dat', delimiter=' ', cols=(0, 1), 
             names=('col1', 'col2'), marker='o')
plt.show()

You can use this function almost like gnuplot from within ipython:

$ ipython --pylab
...
...
In [1]: plt.plotfile('data.dat', delimiter=' ', cols=(0, 1), 
...                  names=('col1', 'col2'), marker='o')

or put it in a shell script and pass the arguments to it to use it directly from your shell

plotfile_example


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...