Mark O'Sullivan
Mark O'Sullivan

Reputation: 10778

Gnuplot.py plotting multiple lines on the one graph

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

Upvotes: 0

Views: 1367

Answers (1)

Mark O&#39;Sullivan
Mark O&#39;Sullivan

Reputation: 10778

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!

Upvotes: 1

Related Questions