0

I'm a newbie in Python and I would like to ask you, how can I get images (a lot of images) made by Gnuplot.py with variable in name? I have this function, which creates single image:

def printimage(conf, i):
   filename = str(i) + "out.postscript"
   g = Gnuplot.Gnuplot()
   g.title('My Systems Plot')
   g.xlabel('Date')
   g.ylabel('Value')
   g('set term postscript')
   g('set out filename')
   databuff = Gnuplot.File(conf, using='1:2',with_='line', title="test")
   g.plot(databuff)

And this function is used in for loop:

i = 0
for row in data:
   config_soubor.write(str(i) + " " + row[22:])
   printimage("config_soubor.conf", i)
   i = i + 1

I still can't get rid of error "undefined variable: filename".

Thanks, Majzlik

3 Answers 3

1

Right now, your python script is passing

set out filename

to gnuplot. There 'filename' is part of the command string; the variable filename you set in your script is not being passed to gnuplot. You could try replacing

g('set out filename')

with

g('set out "'+filename+'"')
Sign up to request clarification or add additional context in comments.

2 Comments

Still the same error, undefined variable: out203... Could the fact, that python said "out203" instead of "out203.postscript", be the point of that problem? Is it possible, the name of that file is interpreted as another variable? btw. I edited line with variable to: filename = "out" + str(i) + ".postscript"
My mistake, the filename needs to be in quotes. See my edit. Gnuplot will create a postscript file out203 properly, but you may want to add the extension so other programs know it is a postscript file.
1

Perhaps you can use the hardcopy method?

Documentation

hardcopy (
        self,
        filename=None,
        terminal='postscript',
        **keyw,
        )

Create a hardcopy of the current plot.

Create a postscript hardcopy of the current plot to the default printer (if configured) or to the specified filename.

Note that gnuplot remembers the postscript suboptions across terminal changes. Therefore if you set, for example, color=1 for one hardcopy then the next hardcopy will also be color unless you explicitly choose color=0. Alternately you can force all of the options to their defaults by setting mode=default. I consider this to be a bug in gnuplot.

Example

See example call:

g.hardcopy('gp_test.ps', enhanced=1, color=1)

Comments

0

Gnuplot expects a line of the form set output "filename". Notice that the filename must be a string. So, for your example, it would be something like:

g('set out "%s"'%filename)

or using newer style string formatting:

g('set out "{0}"'.format(filename))

There are a few other things which could be done better. In general:

i = 0
for x in whatever:
    ...
    i = i + 1

is better written as:

for i,x in enumerate(whatever):
    ...

Also, again using string formatting:

str(i) + " " + row[22:]

can be transformed to:

'%d %s'%(i,row[22:])

or:

'{0} {1}'.format(i,row[22:])

These are minor things and in this script it probably doesn't make a huge difference either way, but they'll be good to keep in mind for the future.

1 Comment

Thank you, I'm glad for every advice. :-)

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.