如何用numpy绘制文件? [英] How to plot files with numpy?
问题描述
我有一个.dat文件,其中包含两列数字,因此看起来像这样:
I have a .dat file that contains two columns of numbers so it looks something like this:
111 112
110.9 109
103 103
以此类推.
我想将两列相互绘制.我以前从未处理过.dat文件,所以我不确定从哪里开始.
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.
到目前为止,我发现numpy可以用来调用.
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?
推荐答案
Numpy本身不支持绘图.通常,您可以使用 matplotlib
绘制numpy数组.
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()
您可以在ipython中像gnuplot
一样使用此功能:
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')
或将其放在shell脚本中,并将参数传递给它,以直接从您的shell中使用它
or put it in a shell script and pass the arguments to it to use it directly from your shell
这篇关于如何用numpy绘制文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!