1

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
5
  • 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? Commented May 4, 2012 at 18:18
  • Also, giving us a snippet of the error would be helpful. Commented May 4, 2012 at 18:22
  • the syntax error is this: set style line 1 lt 1 lc 1 lw 3 ^ SyntaxError: invalid syntax Commented May 4, 2012 at 18:48
  • 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. Commented May 4, 2012 at 20:22
  • Also, I just edited the syntax on your question. I specified your code sample as Python. The syntax highlighting should show you your error. Commented May 4, 2012 at 20:37

1 Answer 1

1

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.

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

2 Comments

---wow I can't believe I missed those triple quotes! I even commented out that line with plans to move it to the end of the string. I'm not real good with Python, and know very little about gnuplot. So your help is greatly appreciated! Now I have to figure out how to get the plot lines to print in the specified colors.
I'm glad I could help. If this answer solves your problem, I suggest you mark it as accepted.

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.