using gnuplot in Windows

General FreeBASIC programming questions.
srvaldez
Posts: 2134
Joined: Sep 25, 2005 21:54

using gnuplot in Windows

Postby srvaldez » May 16, 2019 14:14

badidea's post viewtopic.php?f=8&t=27609 and Carlos Herrera's response prompted me to experiment
you could do a plot with a simple command like

Code: Select all

shell("gnuplot -p -e " + """plot sin(x)/x with lines""")

or with more control

Code: Select all

dim as string plot_command
plot_command=chr(34)+"set terminal windows"
plot_command=plot_command+" color" '" monochrome"
plot_command=plot_command+" solid" '" dashed"
plot_command=plot_command+" enhanced" '" noenhanced"
plot_command=plot_command+" font 'Arial Bold,10'"

''https://www.rapidtables.com/web/color/RGB_Color.html

plot_command=plot_command+" background rgb 'cyan'" 'green'
plot_command=plot_command+" position 1,1"
plot_command=plot_command+" size 640,480"
plot_command=plot_command+" title 'sin(x)/x function plot';"
plot_command=plot_command+" plot sin(x)/x with lines"+chr(34) 'the plot command
shell("gnuplot -p -e " + plot_command)

Image
Tourist Trap
Posts: 2768
Joined: Jun 02, 2015 16:24

Re: using gnuplot in Windows

Postby Tourist Trap » May 16, 2019 14:57

srvaldez wrote:you could do a plot with a simple command like

Nice. Very easy. Thanks for the tip.
srvaldez
Posts: 2134
Joined: Sep 25, 2005 21:54

Re: using gnuplot in Windows

Postby srvaldez » May 16, 2019 15:21

multiple functions plot

Code: Select all

dim as string plot_command
plot_command=chr(34)+"set terminal windows"
plot_command=plot_command+" color"
plot_command=plot_command+" solid" '" dashed"
plot_command=plot_command+" enhanced" '" noenhanced"
plot_command=plot_command+" font 'Arial Bold,10'"
plot_command=plot_command+" background rgb 'gray90'"
plot_command=plot_command+" position 1,1"
plot_command=plot_command+" size 640,480"
plot_command=plot_command+" title 'sin(x), cos(x) and tan(x) function plot';"
plot_command=plot_command+" plot [-pi*2:pi*2] [-1:1] sin(x), cos(x),tan(x);"+chr(34)
shell("gnuplot -p -e " + plot_command)
srvaldez
Posts: 2134
Joined: Sep 25, 2005 21:54

Re: using gnuplot in Windows

Postby srvaldez » May 16, 2019 16:35

multiple windows

Code: Select all

dim as string plot_command
plot_command=chr(34)+"set terminal windows 1"
plot_command=plot_command+" color" '
plot_command=plot_command+" solid" '
plot_command=plot_command+" enhanced"
plot_command=plot_command+" font 'Arial Bold,10'"
plot_command=plot_command+" background rgb 'cyan'"
plot_command=plot_command+" position 1,1"
plot_command=plot_command+" size 640,480"
plot_command=plot_command+" title 'sin(x) function plot';"
plot_command=plot_command+" plot sin(x) with lines;"
plot_command+="set terminal windows 2"
plot_command=plot_command+" color"
plot_command=plot_command+" solid"
plot_command=plot_command+" enhanced"
plot_command=plot_command+" font 'Arial Bold,10'"
plot_command=plot_command+" background rgb '0x87CEEB'"
plot_command=plot_command+" position 1,1"
plot_command=plot_command+" size 640,480"
plot_command=plot_command+" title 'cos(x) function plot';"
plot_command=plot_command+" plot cos(x) with lines;"+chr(34) 'the plot command
shell("gnuplot -p -e " + plot_command)
srvaldez
Posts: 2134
Joined: Sep 25, 2005 21:54

Re: using gnuplot in Windows

Postby srvaldez » May 16, 2019 17:06

1, 3 plots in 1 window

Code: Select all

dim as string plot_command
   
'
' Gnuplot version 5.0 demo of multiplot auto-layout capability
'
plot_command=chr(34)+"set terminal windows;"
plot_command+="set multiplot layout 1,3 title 'Multiplot layout 1, 3' font ',14';"
plot_command+="set xtics rotate;"
plot_command+="set bmargin 5;"

plot_command+="set title 'sin(x)/x';"
plot_command+="unset key;"
plot_command+="plot sin(x)/x;"

plot_command+="set title 'cos(x)';"
plot_command+="unset key;"
plot_command+="plot cos(x);"
'
plot_command+="set title 'tan(x)';"
plot_command+="unset key;"
plot_command+="plot tan(x);"
plot_command+="unset multiplot"+chr(34)
shell("gnuplot -p -e " + plot_command)
Last edited by srvaldez on May 16, 2019 18:27, edited 2 times in total.
srvaldez
Posts: 2134
Joined: Sep 25, 2005 21:54

Re: using gnuplot in Windows

Postby srvaldez » May 16, 2019 17:20

3, 1 plots in 1 window

Code: Select all

dim as string plot_command

' Gnuplot version 5.0 demo of multiplot auto-layout capability
'
plot_command=chr(34)+"set terminal windows;"
'plot_command+="set multiplot layout 3, 1 title 'Multiplot layout 3, 1' font ',14';"
plot_command+="set multiplot layout 3, 1;"
plot_command+="set tmargin 2;"
plot_command+="set title 'sin(x)/x';"
plot_command+="unset key;"
plot_command+="plot sin(x)/x;"
'
plot_command+="set title 'cos(x)';"
plot_command+="unset key;"
plot_command+="plot cos(x);"
'
plot_command+="set title 'tan(x)';"
plot_command+="unset key;"
plot_command+="plot tan(x);"
'
plot_command+="unset multiplot"+chr(34)
shell("gnuplot -p -e " + plot_command)
Carlos Herrera
Posts: 78
Joined: Nov 28, 2011 13:29
Location: Dictatorship

Re: using gnuplot in Windows

Postby Carlos Herrera » May 16, 2019 18:33

I use gnuplot for plotting live data in my project. The small demo program is attached below.
I use Open Pipe trick, which I have learned from this forum. I am mot sure if the same results
can be obtained using shell command.
Carlos

Code: Select all

'
' Gnuplot plotting of live data demo
'
#Include Once "crt\stdio.bi"
#Include Once "crt.bi"
#Include Once "file.bi"
'
Dim Shared gnuplot_hand As FILE Ptr
Dim Shared pltstr As String
Dim Shared gplpipe As Integer
Dim Shared LFeed As String
LFeed = Chr(10)
Dim As Integer ii
'
Sub Gnuplot_Pipe ()
   '
   ' open pipe to gnuplot
   '
   Dim As Integer res
   Dim As String ts
   '
   ' change path as needed, test with persistent mode
   '
   ts = """c:\program files\gnuplot\bin\gnuplot.exe"" -persist"
   gplpipe = Freefile
   res = Open Pipe (ts For Output As #gplpipe)
   Print "Gnuplot Pipe:"; res
   gnuplot_hand = Cast(FILE Ptr,Fileattr(gplpipe,fbFileAttrHandle))
   '
End Sub   
'
Sub Gnuplot_Setup ( )
   '
   ' must be called AFTER Gnuplot_Pipe
   '
   pltstr = ""
   pltstr = pltstr + "set terminal windows linewidth 2" + LFeed
   pltstr = pltstr + "set datafile separator ','" + LFeed
   pltstr = pltstr + "set datafile commentschars '#!%'" + LFeed
   pltstr = pltstr + "set y2tics" + LFeed
   pltstr = pltstr + "set autoscale y2" + LFeed
   pltstr = pltstr + "set minussign" + LFeed
   Print #gplpipe, pltstr
   fflush(gnuplot_hand)
End Sub
'
Sub Gnuplot_Online (labx As String, labyl As String, labyp As String, _
                colx As String, colyl As String, colyp As String)
   '
   ' must be called AFTER Gnuplot_Pipe and Gnuplot_Setup
   '
   pltstr = ""
   pltstr = pltstr + "set xlabel " + labx + LFeed
   pltstr = pltstr + "set ylabel " + labyl + LFeed
   pltstr = pltstr + "set y2label " + labyp + LFeed
   pltstr = pltstr + "plot 'online.csv' using "+colx+":"+colyl+" w lp pt 7 lc 7 axes x1y1,\" + LFeed
   pltstr = pltstr + "'online.csv' using "+colx+":"+colyp+" w lp pt 11 lc 11 axes x1y2"
   Print #gplpipe, pltstr
   fflush(gnuplot_hand)
   '
End Sub
'
Sub Create_Online_File
    '
    ' create online file
    '
    Dim As Integer fil
   '
    fil = Freefile
    Open "online.csv" For Output As #fil 
    Print #fil, "# online data"
    Close #fil
    ' 
End Sub
'
Sub Add_Data_to_File ( idx As Integer)
    '
    ' appends data to obline.csv
    '
    Dim As Integer filo
    Randomize 
    filo = Freefile
    Open "online.csv" For Append As #filo
    Print #filo, Str(idx) + "," + Str(Sin(0.3*idx) + 0.4*Rnd) + "," + Str(0.01*idx+0.1*Cos(0.2*idx) + 0.03*Rnd)
    Print Str(idx) + "," + Str(Sin(0.3*idx) + 0.4*Rnd) + "," + Str(0.01*idx+0.1*Cos(0.2*idx) + 0.03*Rnd)       
    Close #filo
    '
End Sub
'
' Generate and plot live data
'
Gnuplot_Pipe
Gnuplot_Setup
Create_Online_File
For ii = 0 To 100
   Add_Data_to_File (ii)
   Sleep 300
   Gnuplot_Online ("'index'", "'Y_1'", "'Y_2'", "1", "2", "3")
Next ii
Sleep
jj2007
Posts: 1259
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: using gnuplot in Windows

Postby jj2007 » May 16, 2019 18:45

Sorry if this is a stupid question: How would you display the plot inside an application, i.e. in a control or a window?
srvaldez
Posts: 2134
Joined: Sep 25, 2005 21:54

Re: using gnuplot in Windows

Postby srvaldez » May 16, 2019 19:01

thank you Carlos Herrera, nice example :-)
badidea
Posts: 1582
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: using gnuplot in Windows

Postby badidea » May 16, 2019 20:10

Nice, I got all examples working on linux as well, by changing terminal windows to x11 or qt (looking better). However,
- terminal x11 does not support the color command it seems.
- terminal qt does not support the color and background command it seems.
The live plot by Carlos Herrera works as well, only the set minussign is reported as unrecognized.
srvaldez
Posts: 2134
Joined: Sep 25, 2005 21:54

Re: using gnuplot in Windows

Postby srvaldez » May 16, 2019 20:31

@badidea
I also found that color, background and position options are not available in macOS.
in macOS the terminal I use is aqua, and when gnuplot launches aqua with the plot, the FB program is free to end while the plot-window remains open until it's closed, not so in windows, the FB program waits for the gnuplot window to close before it ends, I tried exec and run instead of shell, but so far no luck.
Carlos Herrera
Posts: 78
Joined: Nov 28, 2011 13:29
Location: Dictatorship

Re: using gnuplot in Windows

Postby Carlos Herrera » May 16, 2019 20:34

@jj2007
Since I don't now how to create "window" or "control" in a simple way,
such problems do not bother me much. But of course, it would be nice to "dock" gnuplot
terminal within window created with winAPI. However, this question should be
asked to "resident experts" here, since it applies also to the fbgfx terminal, isn't it?
badidea
Posts: 1582
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: using gnuplot in Windows

Postby badidea » May 16, 2019 20:41

Same behavior here as on macOS using shell. With exec and run, I never get a plot window.
dodicat
Posts: 5984
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: using gnuplot in Windows

Postby dodicat » May 21, 2019 8:59

Srvaldez.
I got hold of gnuplot for windows.
gp526-win64-mingw.7z
It works well here with the examples.
Thank you.

Return to “General”

Who is online

Users browsing this forum: Google [Bot] and 1 guest