0

I'm currently stuck trying to plot multiple lines from a text file using gnuplot py. I can get both lines plotting individually but when I try to plot both of them on the same graph, it only plots one line.

This is my code:

#!/usr/bin/env python   

import Gnuplot  

g = Gnuplot.Gnuplot()   


g('set terminal png') # Output of graph will be .png    
g('set output "' + "python_test.png" + '"') # Set the name of the output file    
g('set term png size 1200, 800')    
g('set lmargin 8')    
g('set rmargin 4')    
g('set tmargin 3')    
g('set bmargin 3')    
g('set xdata time')    
g('set timefmt "%H:%M:%S"')    
g('set format x "%H:%M:%S"')    
title = "Python Test graph "    
g('set title "' + title + '"')    
g('set xlabel "Time (HH:MM:SS)"')    
g('set ylabel "' + "quantity" + '"')
#g('set xrange [*:*]')

plot_cmd = "< head -n -1 "
datFile = "data.dat"

g('plot "' + plot_cmd + datFile + '" using 1:3' + ' title "' + "Line 1" +'" with lines')
g('plot "' + plot_cmd + datFile + '" using 1:5' + ' title "' + "Line 2" +'" with lines')

I've managed to get multiple lines plotted just using gnuplot plot but I can't seem to get it to work when I'm using gnuplot py which is what I need to use as I'm wanting to produce a gnuplot graph using my python script.

If needed, here is a link to my datFile: link

5
  • I'm not familiar with the gnuplot module but this appears to be creating two plots. For a single plot with two lines you issue just one "plot" command. Commented Sep 11, 2014 at 15:36
  • @george I've tried variations of that, for example, I tried g('"'datFile + '" using 1:5' + ' title "' + "Line 2" +'" with lines') but it says "invalid syntax" when I tried that. Commented Sep 11, 2014 at 15:40
  • 2
    try something like this : g('plot "' + plot_cmd + datFile + '" using 1:3 , "' + plot_cmd + datFile + '" using 1:5') (note the comma separating the two lines ) Commented Sep 11, 2014 at 15:54
  • @george it kind of worked but there was no lines, I had to add this to get it to work correctly: g('plot "' + plot_cmd + datFile + '" using 1:3 with lines, "' + plot_cmd + datFile + '" using 1:5 with lines') . Do you want to add your answer and then I can accept it as the answer? Also do you think it's possible to get it to work on separate lines like I had in my original code or will I have to keep it the way it is? Commented Sep 11, 2014 at 16:12
  • glad it works, feel free to write up an answer and accept - I don't want to post an answer since it was basically a guess and I cant test it myself. Commented Sep 11, 2014 at 17:13

1 Answer 1

1

In order to get it working, I had to merge these two lines of code into one line of code.

g('plot "' + plot_cmd + datFile + '" using 1:3' + ' title "' + "Line 1" +'" with lines')
g('plot "' + plot_cmd + datFile + '" using 1:5' + ' title "' + "Line 2" +'" with lines')

This is the code which allowed me to plot multiple lines:

g('plot "' + plot_cmd + datFile + '" using 1:3 with lines, "' + plot_cmd + datFile + '" using 1:5 with lines')

Big thanks to george who helped me find a solution to my problem!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.