Calling gnuplot from python

前端 未结 8 629
清酒与你
清酒与你 2020-12-04 18:34

I\'ve a python script that after some computing will generate two data files formatted as gnuplot input.

How do I \'call\' gnuplot from python ?

I want to se

8条回答
  •  Happy的楠姐
    2020-12-04 19:00

    Subprocess is explained very clearly on Doug Hellemann's Python Module of the Week

    This works well:

    import subprocess
    proc = subprocess.Popen(['gnuplot','-p'], 
                            shell=True,
                            stdin=subprocess.PIPE,
                            )
    proc.stdin.write('set xrange [0:10]; set yrange [-2:2]\n')
    proc.stdin.write('plot sin(x)\n')
    proc.stdin.write('quit\n') #close the gnuplot window
    

    One could also use 'communicate' but the plot window closes immediately unless a gnuplot pause command is used

    proc.communicate("""
    set xrange [0:10]; set yrange [-2:2]
    plot sin(x)
    pause 4
    """)
    

提交回复
热议问题