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
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:Again, this triple quote syntax marks the end of the Gnuplot string, so what follows is expected to be Python code. Now,
setmeans something entirely different to Python (it’s a mathematical set, if you’re curious). Your Gnuplot syntax forsetdoes 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):Then interact with this file in Python like so:
The End Result
plot_scriptcontains exactly the same data, each file contains code unique to one language, and your code is much more readable.