【问题标题】:python gnuplot read from filepython gnuplot 从文件中读取
【发布时间】:2012-11-03 00:17:57
【问题描述】:

我有一个包含以下数据的文件,第一个字段是 UNIXTimestamp,

1351734672.095 25
1351734674.449 52
1351734676.638 612
1351734680.669 44
1351734681.121 16
1351734684.182 15
1351734684.386 17
1351734686.823 16
1351734689.807 22
1351734689.807 28

如何将 x,y 从这个文件加载到 python?

#!/usr/bin/env python

from numpy import *
import Gnuplot

g = Gnuplot.Gnuplot()
g.title('My Systems Plot')
g.xlabel('Date')
g.ylabel('Value')
g('set term png')
g('set out "output.png"')
proc = open("response","r")
databuff = Gnuplot.Data(proc.read(), title="test")
g.plot(databuff)

目前这不起作用...有什么想法吗?

更新:

#!/usr/bin/env python
from numpy import *
import Gnuplot

g = Gnuplot.Gnuplot()
g.title('My Systems Plot')
g.xlabel('Date')
g.ylabel('Response')
g('set auto x')
g('set term png')
g('set out "output.png"')
g('set timefmt "%s"')
g('set xdata time')
g('set xtic rotate by 45 scale 1 font ",2"')
g('set key noenhanced')
g('set format x "%H:%M:%S"')
g('set grid')


databuff = Gnuplot.File("repo", using='1:2',title="test")
g.plot(databuff)

xaxis 上的时间值与图表重叠。有什么想法吗??

【问题讨论】:

    标签: python linux gnuplot


    【解决方案1】:

    使用Gnuplot.File代替Gnuplot.Data,可以得到如下图:

    #!/usr/bin/env python
    
    #from numpy import *
    import Gnuplot
    
    g = Gnuplot.Gnuplot()
    g.title('My Systems Plot')
    g.xlabel('Date')
    g.ylabel('Value')
    g('set term png')
    g('set out "output.png"')
    #proc = open("response","r")
    #databuff = Gnuplot.Data(proc.read(), title="test")
    
    databuff = Gnuplot.File("response", using='1:2',with_='line', title="test")
    g.plot(databuff)
    


    更新:

    对于重叠,您可以将45 更改为-45

    #g('set xtic rotate by 45 scale 1 font ",2"')
    g('set xtic rotate by -45 scale 1 font ",2"')
    

    【讨论】:

    • 这里不需要导入numpy。否则+1,比我的答案更好。
    • 我评论了numpy import
    猜你喜欢
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-23
    • 2015-06-25
    • 2018-04-02
    • 1970-01-01
    • 2014-11-28
    相关资源
    最近更新 更多