How to plot files with numpy?

前端 未结 2 882
闹比i
闹比i 2020-12-17 17:44

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.

<
相关标签:
2条回答
  • 2020-12-17 18:01
    import numpy as np
    import matplotlib.pyplot as plot
    #data = np.loadtxt("plot_me.dat")
    #x,y=np.loadtxt("plot_me.dat",unpack=True) #thanks warren!
    #x,y =  zip(*data)
    #plot.plot(x, y, linewidth=2.0)
    plot.plot(*np.loadtxt("plot_me.dat",unpack=True), linewidth=2.0)
    plot.show()
    

    [Edit]Thanks for the tip i think its as compact as possible now :P

    plot 1

    If you want it to be log10 just call log10 on the nparray)

    plot.plot(*np.log10(np.loadtxt("plot_me.dat",unpack=True)), linewidth=2.0)
    

    log10

    0 讨论(0)
  • 2020-12-17 18:15

    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

    0 讨论(0)
提交回复
热议问题