![](https://secure.gravatar.com/avatar/9181039038388fe4e542aaa6354bec62.jpg?s=120&d=mm&r=g)
Hi Need some basic help with Gnuplot.py - it must a simple thing I'm not getting. I can't get xrange to work as expected, the axis shifts, but the data doesn't get plotted accordingly. Best explained with an example. If you enter the following in gnuplot directly/interactively: ---------------------- gnuplot> set xrange[-5:5] gnuplot> plot x*x ---------------------- This is what I want to be able to do via a python script where I pass in the data for x and y in list. I have the following code: ------------------- #!/usr/bin/env python import Gnuplot g = Gnuplot.Gnuplot(debug=1) x = range(-5,5) y = [] for i in x: y.append(i*i) g('set style data linespoint') #g('set xrange[-5:5]') g.plot(y) ------------------- the plot is centered around 5 (tick marks go from 0 to 10). If I use g('set xrange[-5:5]') to center it around 0, I only get half of the graph (tick marks go from -5 to 5). I also tried g.plot(x, y) w/o luck Is there a way to get this to work as the interactive gnuplot example above does? I think I may be missing something obvious ... Thanks!
![](https://secure.gravatar.com/avatar/9181039038388fe4e542aaa6354bec62.jpg?s=120&d=mm&r=g)
Well .. technically, this x = range(-5,5) should really be x = range(-5,6) but still doesn't solve the problem ...
![](https://secure.gravatar.com/avatar/5bc3911d30ec66d8a65a05846502f0c4.jpg?s=120&d=mm&r=g)
This works for me. Mind that data is a list of lists, while your y is just a list. Hence gnuplot plotted y as a function of its index, not of x. #!/usr/bin/env python import Gnuplot g = Gnuplot.Gnuplot() x = range(-5,6) data = [] for i in x: y.append(i*i) data.append([i,i*i]) g('set style data linespoint') #g('set xrange[-5:5]') g.plot(data) g.hardcopy("elephant.ps") For some reason Gnuplot.Gnuplot(persist = 1) does not work for me. However, I view my results with gv. For this set chuck~>cat .gv GV.antialias: True GV.watchFile: True GV.watchFileFrequency: 500 GV.version: gv 3.6.5.91 chuck~> Hope this helps Nicky 2009/8/1 Esmail <ebonak@hotmail.com>:
Well .. technically, this
x = range(-5,5)
should really be
x = range(-5,6)
but still doesn't solve the problem ...
_______________________________________________ SciPy-User mailing list SciPy-User@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-user
![](https://secure.gravatar.com/avatar/9181039038388fe4e542aaa6354bec62.jpg?s=120&d=mm&r=g)
Hi Nicky, nicky van foreest wrote:
This works for me. Mind that data is a list of lists, while your y is just a list. Hence gnuplot plotted y as a function of its index, not of x.
ah .. got it!
Hope this helps
It sure did .. it works great. I have a quick follow up though if you don't mind. Here is a 3D version. ------------------ #!/usr/bin/env python import Gnuplot g = Gnuplot.Gnuplot(debug=1) x = range(-3,4) data = [] for i in x: for j in x: data.append([i, j, i*i]) for i in data: print i[0], i[1], i[2] g('set hidden3d') g('set style data linespoint') g.splot(data) ------------------ the g('set style data linespoint') leads to lines connecting that ought not to. To see what I mean run the above code, and then compare this with plotting the data below straight from gnuplot. It is the same as the program generates during the run, all I have done is inserted blank lines for each block and then saved the file under the name t2data ------------------ gnuplot> set style data linespoint gnuplot> splot './t2data' ------------------ Is there a way to mimic this with the data list? I tried appending '\n' to the data list, but that didn't work. If there is no easy work around I may just write the data to a file (with the blank lines) and then read it in again - probably not the most efficient way to do this, but it'll work :-) ... plus that seems to be going on in the background already anyway. Thanks, Esmail --- t2data file ---- -3 -3 9 -3 -2 9 -3 -1 9 -3 0 9 -3 1 9 -3 2 9 -3 3 9 -2 -3 4 -2 -2 4 -2 -1 4 -2 0 4 -2 1 4 -2 2 4 -2 3 4 -1 -3 1 -1 -2 1 -1 -1 1 -1 0 1 -1 1 1 -1 2 1 -1 3 1 0 -3 0 0 -2 0 0 -1 0 0 0 0 0 1 0 0 2 0 0 3 0 1 -3 1 1 -2 1 1 -1 1 1 0 1 1 1 1 1 2 1 1 3 1 2 -3 4 2 -2 4 2 -1 4 2 0 4 2 1 4 2 2 4 2 3 4 3 -3 9 3 -2 9 3 -1 9 3 0 9 3 1 9 3 2 9 3 3 9
![](https://secure.gravatar.com/avatar/5bc3911d30ec66d8a65a05846502f0c4.jpg?s=120&d=mm&r=g)
Hi,
Here is a 3D version. ------------------
#!/usr/bin/env python
import Gnuplot
g = Gnuplot.Gnuplot(debug=1) x = range(-3,4) data = []
for i in x: for j in x: data.append([i, j, i*i])
I cannot help you right away this time. This is what I know: I checked the demo.py file that comes along with python-gnuplot. There the plotting starts with the z-values, then the x, and then the y values: This is not what you seem to be doing. See the line: g.splot(Gnuplot.GridData(m,x,y, binary=0)) Besides this, I recall from the not the frequently asked questions site of gnuplot that interlacing spaces has some effect on the plotting of data, which effect I forgot. This may also affect your plot. Sorry that I cannot be of further help this time.
for i in data: print i[0], i[1], i[2]
g('set hidden3d') g('set style data linespoint') g.splot(data)
------------------
the
g('set style data linespoint')
leads to lines connecting that ought not to. To see what I mean run the above code, and then compare this with plotting the data below straight from gnuplot. It is the same as the program generates during the run, all I have done is inserted blank lines for each block and then saved the file under the name t2data
------------------
gnuplot> set style data linespoint gnuplot> splot './t2data'
------------------
Is there a way to mimic this with the data list? I tried appending '\n' to the data list, but that didn't work. If there is no easy work around I may just write the data to a file (with the blank lines) and then read it in again - probably not the most efficient way to do this, but it'll work :-) ... plus that seems to be going on in the background already anyway.
Thanks, Esmail
--- t2data file ---- -3 -3 9 -3 -2 9 -3 -1 9 -3 0 9 -3 1 9 -3 2 9 -3 3 9
-2 -3 4 -2 -2 4 -2 -1 4 -2 0 4 -2 1 4 -2 2 4 -2 3 4
-1 -3 1 -1 -2 1 -1 -1 1 -1 0 1 -1 1 1 -1 2 1 -1 3 1
0 -3 0 0 -2 0 0 -1 0 0 0 0 0 1 0 0 2 0 0 3 0
1 -3 1 1 -2 1 1 -1 1 1 0 1 1 1 1 1 2 1 1 3 1
2 -3 4 2 -2 4 2 -1 4 2 0 4 2 1 4 2 2 4 2 3 4
3 -3 9 3 -2 9 3 -1 9 3 0 9 3 1 9 3 2 9 3 3 9
_______________________________________________ SciPy-User mailing list SciPy-User@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-user
![](https://secure.gravatar.com/avatar/9181039038388fe4e542aaa6354bec62.jpg?s=120&d=mm&r=g)
nicky van foreest wrote:
I cannot help you right away this time. This is what I know:
I checked the demo.py file that comes along with python-gnuplot. There the plotting starts with the z-values, then the x, and then the y values: This is not what you seem to be doing. See the line: g.splot(Gnuplot.GridData(m,x,y, binary=0))
Besides this, I recall from the not the frequently asked questions site of gnuplot that interlacing spaces has some effect on the plotting of data, which effect I forgot. This may also affect your plot.
Sorry that I cannot be of further help this time.
Not at all, no reason to be sorry, thanks for the help and the pointers to the demo file. I will take a closer look at that file and the test.py file too. Thanks again! Esmail
participants (2)
-
Esmail
-
nicky van foreest