1
votes

I have been searching all over the internet for an answer to this.

It seems there are many versions of how to plot color lines in gnuplot. I have a data file that has 4 columns in it. I want to plot each column with a different color. This is the snippet of code I am using per the gnuplot help file, but I get a syntax error when I use these.

set style line 1 lt 1 lc 1 lw 3 # red
set style line 2 lt 1 lc 2 lw 3 #green
set style line 3 lt 1 lc 3 lw 3 #blue
set style line 4 lt 1 lc 4 lw 3 #magenta

I have terminal set to postscript.

I have tried all combinations of this line type style including linestyle, and lc rgb 'red', for example, and none of them work!

Can anyone tell me what is wrong?

Let me clarify, this is a gnuplot script in a python script. code looks like this:

plot = open('plot.pg','w')
plot_script = """#!/usr/bin/gnuplot
reset
set terminal postscript 
#cd publienhanced color
set output "roamingresult.ps"
set xlabel "time (seconds)"
set xrange [0:900]
set xtics (0, 60, 120, 180, 240, 300, 360, 420, 480, 540, 600, 660, 720, 780, 840, 900)
set ylabel "AP1         AP2         AP3       AP4"
set yrange [0:5]
set nokey
set grid
set noclip one
set ytics 1
#set style data boxes"""
set style line 1 lt 1 lc 1 lw 3
set style line 2 lt 1 lc 2 lw 3
set style line 3 lt 1 lc 3 lw 3
set style line 4 lt 1 lc 4 lw 3
1
For starters, the snippet you are using is not Python. I don't know much about Gnuplot, but it looks like the script you're using is just a standard Gnuplot script. Might you be trying to invoke it as a Python script?David Cain
Also, giving us a snippet of the error would be helpful.David Cain
the syntax error is this: set style line 1 lt 1 lc 1 lw 3 ^ SyntaxError: invalid syntaxDavidScott612
Ah, very good. For future reference, you should almost always post your error/stack trace, as well as the relevant code sample. It can be very difficult to pinpoint your problem without it. See my answer for a solution to your problem.David Cain
Also, I just edited the syntax on your question. I specified your code sample as Python. The syntax highlighting should show you your error.David Cain

1 Answers

1
votes

Okay, from the code you've just updated, your problem is clear.

What's wrong (quick answer)

You're including your Gnuplot script into the Python source code as a string. The """ token signifies the begininng of a string, as well as its end. The problem is that you terminate the string with this line:

#set style data boxes"""

Again, this triple quote syntax marks the end of the Gnuplot string, so what follows is expected to be Python code. Now, set means something entirely different to Python (it's a mathematical set, if you're curious). Your Gnuplot syntax for set does not match the meaning in Python, so this is why it's giving you a syntax error.

Moving the triple quotes to the end of your Gnuplot script would fix the problem. However, there is a much easier solution.

A better way

You should not be in-lining Gnuplot code directly into a Python script. Instead, you should read in the script from another file (where the file is entirely Gnuplot code), and deal with it that way.

So, keep a file with just your Gnuplot code (e.g. plot.script):

#!/usr/bin/gnuplot
reset
set terminal postscript 
#cd publienhanced color
set output "roamingresult.ps"
set xlabel "time (seconds)"
set xrange [0:900]
set xtics (0, 60, 120, 180, 240, 300, 360, 420, 480, 540, 600, 660, 720, 780, 840, 900)
set ylabel "AP1         AP2         AP3       AP4"
set yrange [0:5]
set nokey
set grid
set noclip one
set ytics 1
#set style data boxes
set style line 1 lt 1 lc 1 lw 3
set style line 2 lt 1 lc 2 lw 3
set style line 3 lt 1 lc 3 lw 3
set style line 4 lt 1 lc 4 lw 3

Then interact with this file in Python like so:

plot_script = open("plot.script", "r").read()

The End Result

plot_script contains exactly the same data, each file contains code unique to one language, and your code is much more readable.