How to plot files with numpy?

发布于 2021-01-29 14:09:51

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?

关注者
0
被浏览
143
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    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



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看