::gnuplotutilTop, Main, Index
CommandsTop, Main, Index
initArgStr [::gnuplotutil]Top, Main, Index
Parameters
optDict | Not documented. |
optName | Not documented. |
varName | Not documented. |
value | Not documented. |
proc ::gnuplotutil::initArgStr {optDict optName varName value} { if {[dexist $optDict $optName]} { uplevel {*}[list set $varName $value] } else { uplevel {*}[list set $varName {""}] } return }
initRangeStr [::gnuplotutil]Top, Main, Index
Parameters
optDict | Not documented. |
optName | Not documented. |
varName | Not documented. |
proc ::gnuplotutil::initRangeStr {optDict optName varName} { if {[dexist $optDict $optName]} { lassign [dget $optDict $optName] min max set str "set $optName \[${min}:${max}\]" uplevel {*}[list set $varName [list $str]] } else { uplevel {*}[list set $varName {""}] } return }
initTerminalStr [::gnuplotutil]Top, Main, Index
Parameters
optDict | Not documented. |
varName | Not documented. |
proc ::gnuplotutil::initTerminalStr {optDict varName} { if {[dexist $optDict terminal]} { uplevel set $varName [dget $optDict terminal] } else { global tcl_platform if {[string match -nocase *linux* $tcl_platform(os)]} { uplevel set $varName x11 } elseif {[string match -nocase "*windows nt*" $tcl_platform(os)]} { uplevel set $varName windows } else { uplevel set $varName qt } } }
multiplotXNYN [::gnuplotutil]Top, Main, Index
Plots 2D graphs in Gnuplot with individual x-values and using multiplot to display.
Parameters
-background | Enables running gnuplot in background, requires -nodelete or -inline switch. |
-darkmode | Enables dark mode for graph. |
-inline | Provides data directly in command string, without creating temporary file. |
-layout | List of layout configurations values, for example, {2 2} |
-nodelete | Disables deleting of temporary file after end of plotting. |
-optcmd | Provides optional string that may contain additional commands to gnuplot. |
-plots | Provides the list of individual plots, the number of plots is not restricted and must be provided at the end of command after all switches, the inputs syntax is the same as in plotXNYNMp |
-size | Provides size of the window, must be list with two elements: width and height in pixels. |
-terminal | Provides terminal, default 'x11' on linux and 'windows' on windows. |
Description
Example: set x1 [list 1 2 3 4 5 6] set y1 [list 1 4 9 16 25 36] set x2 [list 6 5 4 3 2 1] set y2 [list 36 25 16 9 4 1] set x3 [list 7 8 9 10] set y3 [list 49 64 81 100] set plot1 [list -xlog -ylog -xlabel "x label" -ylabel "y label" -grid -names [list name1 name2 name3] -columns $x1 $y1 $x2 $y2 $x3 $y3] set plot2 [list -ylabel "y label" -grid -names [list name1 name2] -columns $x1 $y1 $x2 $y2 ] set plot3 [list -xlabel "x label" -ylabel "y label" -names [list name3] -columns $x3 $y3 ] set plot4 [list -grid -names [list name2] -columns $x2 $y2 ] gnuplotutil::multiplotXNYN {2 2} -plots $plot1 $plot2 $plot3 $plot4
Return value
creates gnuplot window with plotted data, and returns string contains commands sent to gnuplot
proc ::gnuplotutil::multiplotXNYN {args} { # Plots 2D graphs in Gnuplot with individual x-values and using multiplot to display. # -layout - list of layout configurations values, for example, {2 2} # -nodelete - disables deleting of temporary file after end of plotting # -background - enables running gnuplot in background, requires -nodelete or -inline switch # -inline - provides data directly in command string, without creating temporary file # -optcmd - provides optional string that may contain additional commands to gnuplot # -terminal - provides terminal, default 'x11' on linux and 'windows' on windows # -size - provides size of the window, must be list with two elements: width and height in pixels # -darkmode - enables dark mode for graph # -plots - provides the list of individual plots, the number of plots is not restricted and must # be provided at the end of command after all switches, the inputs syntax is the same as in # [::gnuplotutil::plotXNYNMp] # Returns: creates gnuplot window with plotted data, and returns string contains commands sent to gnuplot # Synopsis: layoutList ?-background? ?-nodelete? ?-optcmd string? ?-terminal? ?-darkmode? ?-size list? # -columns plotList1 ?plotList2 ...? # ``` # Example: # set x1 [list 1 2 3 4 5 6] # set y1 [list 1 4 9 16 25 36] # set x2 [list 6 5 4 3 2 1] # set y2 [list 36 25 16 9 4 1] # set x3 [list 7 8 9 10] # set y3 [list 49 64 81 100] # set plot1 [list -xlog -ylog -xlabel "x label" -ylabel "y label" -grid -names [list name1 name2 name3] # -columns $x1 $y1 $x2 $y2 $x3 $y3] # set plot2 [list -ylabel "y label" -grid -names [list name1 name2] -columns $x1 $y1 $x2 $y2 ] # set plot3 [list -xlabel "x label" -ylabel "y label" -names [list name3] -columns $x3 $y3 ] # set plot4 [list -grid -names [list name2] -columns $x2 $y2 ] # gnuplotutil::multiplotXNYN {2 2} -plots $plot1 $plot2 $plot3 $plot4 # ``` set arguments [argparse -inline -help {Plots 2D graphs in Gnuplot with individual x-values and using multiplot to display} { {-layout= -help {List of layout configurations values, for example, {2 2}}} {-nodelete -forbid inline -help {Disables deleting of temporary file after end of plotting}} {-background -help {Enables running gnuplot in background}} {-inline -help {Provides data directly in command string, without creating temporary file}} {-optcmd= -help {Provides optional string that may contain additional commands to gnuplot}} {-terminal= -help {Selects terminal, default 'x11' on Linux and 'windows' on Windows}} {-size= -default {800 600} -type list -help {Provides size of the window, must be list with two elements: width and height in pixels}} {-darkmode -help {Enables dark mode for graph}} {-plots -catchall -help {Provides the list of individual plots, the number of plots is not restricted and must be provided at the end of command after all switches, the inputs syntax is the same as in [::gnuplotutil::plotXNYNMp]}} }] if {[dexist $arguments background]} { AliasesKeysCheck $arguments {nodelete inline} } initArgStr $arguments optcmd optcmdStr {[dget $arguments optcmd]} if {[dict exists $arguments layout]} { if {[llength [dict get $arguments layout]]>2} { return -code error {Length of layout arguments is more than 2} } else { set layoutStr "set multiplot layout [@ [dict get $arguments layout] 0],[@ [dict get $arguments layout] 1]" } } else { set layoutStr {set multiplot} } initTerminalStr $arguments terminalStr if {[dexist $arguments darkmode]} { set darkmodeStr "set term $terminalStr size [join [dget $arguments size] ,] noenhanced background rgb 'black'" append darkmodeStr "\n" $::gnuplotutil::darkmodeStyle } else { set darkmodeStr "set term $terminalStr size [join [dget $arguments size] ,] noenhanced" } set i 0 foreach plot [dget $arguments plots] { if {[dexist $arguments inline]} { set plotResults [gnuplotutil::plotXNYNMp -inline {*}$plot] } else { set plotResults [gnuplotutil::plotXNYNMp {*}$plot] lappend fileNames [dget $plotResults file] } lappend cmdStrings [dget $plotResults cmdString] incr i } set commandList [list $optcmdStr $darkmodeStr $layoutStr [join $cmdStrings \n] {unset multiplot} {pause mouse close}] set commandStr [join [lmap elem $commandList {= {$elem eq {} ? [continue] : $elem}}] "\n"] if {[dexist $arguments background]} { set status [catch {exec gnuplot << "\n$commandStr\n" & } errorStr] } else { set status [catch {exec gnuplot << "\n$commandStr\n"} errorStr] } if {![dexist $arguments nodelete] && ![dexist $arguments inline]} { foreach fileName $fileNames { file delete $fileName } } if {$status>0} { return $errorStr } else { return $commandStr } }
plotHist [::gnuplotutil]Top, Main, Index
Plots 2D histograms in Gnuplot with the common x-values.
Parameters
-background | Enables running gnuplot in background, requires -nodelete switch. |
-border | Provides border of columns with particular style, -fill argument is required. |
-boxwidth | Provides width of columns, must be in range (0,1] |
-columns | Provides the y data to plot, the number of columns is not restricted and must be provided at the end of command after all switches. |
-darkmode | Enables dark mode for graph. |
-density | Provides density of solid filling, -fill argument is required. |
-fill | Provides fill of columns, must be empty or solid. |
-gap | Provides gap between columns in clustered style, -style argument is required. |
-grid | Enables grid of histogram. |
-inline | Provides data directly in command string, without creating temporary file. |
-names | Enables setting the column names of provided data, value must be provided as list in the same order as data columns provided, must have the length equal to number of y data colums. |
-nodelete | Disables deleting of temporary file after end of plotting. |
-optcmd | Provides optional string that may contain additional commands to gnuplot. |
-output | Command redirects output to the specified file or device. |
-path | Provides location of temporary file, default is current location. |
-size | Provides size of the window, must be list with two elements: width and height in pixels. |
-style | Provides style of diagram, must be clustered, rowstacked or columnstacked. |
-terminal | Provides terminal, default 'x11' on linux and 'windows' on windows. |
-transparent | Enables transparency to filling of columns, -fill argument is required. |
-xlabel | Provides x-axis label to display, string must be provided after it. |
-ylabel | Provides y-axis label to display, string must be provided after it. |
x | List that contains x-point for 2D histogram. |
Description
Example: set x1 [list 0 1 2 3 4 5 6] set y1 [list 0 1 4 9 16 25 36] set y2 [list 0 1 8 27 64 125 216] gnuplotutil::plotHist $x1 -style clustered -fill solid -xlabel "x label" -ylabel "y label" -names [list y1 y2] -columns $y1 $y2]
Return value
creates gnuplot window with plotted data, and returns string contains commands sent to gnuplot
proc ::gnuplotutil::plotHist {args} { # Plots 2D histograms in Gnuplot with the common x-values. # x - List that contains x-point for 2D histogram # -nodelete - disables deleting of temporary file after end of plotting # -xlabel - provides x-axis label to display, string must be provided after it # -ylabel - provides y-axis label to display, string must be provided after it # -style - provides style of diagram, must be clustered, rowstacked or columnstacked # -gap - provides gap between columns in clustered style, -style argument is required # -boxwidth - provides width of columns, must be in range (0,1] # -fill - provides fill of columns, must be empty or solid # -grid - enables grid of histogram # -background - enables running gnuplot in background, requires -nodelete switch # -inline - provides data directly in command string, without creating temporary file # -terminal - provides terminal, default 'x11' on linux and 'windows' on windows # -size - provides size of the window, must be list with two elements: width and height in pixels # -path - provides location of temporary file, default is current location # -darkmode - enables dark mode for graph # -output - command redirects output to the specified file or device # -transparent - enables transparency to filling of columns, -fill argument is required # -density - provides density of solid filling, -fill argument is required # -border - provides border of columns with particular style, -fill argument is required # -optcmd - provides optional string that may contain additional commands to gnuplot # -names - enables setting the column names of provided data, value must be provided as list # in the same order as data columns provided, must have the length equal to number of y data colums # -columns - provides the y data to plot, the number of columns is not restricted and must be # provided at the end of command after all switches # Returns: creates gnuplot window with plotted data, and returns string contains commands sent to gnuplot # Synopsis: x -style ?-gap value? ?-boxwidth value? ?-fill value? ?-transparent? ?-density value? ?-border value? # ?-background? ?-nodelete? ?-xlabel string? ?-ylabel string? ?-optcmd string? ?-terminal? ?-grid? ?-darkmode? # ?-size list? ?-path string? ?-names list? -columns xList1 ?xList2 ...? # ``` # Example: # set x1 [list 0 1 2 3 4 5 6] # set y1 [list 0 1 4 9 16 25 36] # set y2 [list 0 1 8 27 64 125 216] # gnuplotutil::plotHist $x1 -style clustered -fill solid -xlabel "x label" -ylabel "y label" -names [list y1 y2] # -columns $y1 $y2] # ``` set arguments [argparse -inline -pfirst -help {Plots 2D histograms in Gnuplot with the common x-values} { {x -help {List that contains x-point for 2D histogram}} {-nodelete -forbid inline -help {Disables deleting of temporary file after end of plotting}} {-xlabel= -help {Provides x-axis label to display}} {-ylabel= -help {Provides y-axis label to display}} {-style= -enum {clustered rowstacked columnstacked} -required -help {Provides style of diagram}} {-gap= -require style -help {Provides gap between columns in clustered style}} {-optcmd= -help {Provides optional string that may contain additional commands to gnuplot}} {-grid -help {Enables display of grid}} {-background -help {Enables running gnuplot in background}} {-inline -help {Provides data directly in command string, without creating temporary file}} {-terminal= -help {Selects terminal, default 'x11' on Linux and 'windows' on Windows}} {-size= -default {800 600} -type list -help {Provides size of the window, must be list with two elements: width and height in pixels}} {-path= -default {} -help {Provides location of temporary file}} {-darkmode -help {Enables dark mode for graph}} {-output= -help {Command redirects output to the specified file or device}} {-names= -type list -help {Enables setting the column names of provided data, value must be provided as list in the same order as data columns provided, must have the length equal to the number of y data colums}} {-boxwidth= -help {Provides width of columns, must be in range (0,1]}} {-fill= -enum {empty solid} -help {Provides fill of columns}} {-density= -require fill -help {Provides density of solid filling}} {-transparent -require fill -help {Enables transparency to filling of columns}} {-border= -require fill -help {Provides border of columns with particular style}} {-columns -catchall -help {Provides the y data to plot, the number of columns is not restricted and must be provided at the end of command after all switches}} }] if {[dexist $arguments background]} { AliasesKeysCheck $arguments {nodelete inline} } initArgStr $arguments xlog xscaleStr {{set logscale x}} initArgStr $arguments ylog yscaleStr {{set logscale y}} initArgStr $arguments grid gridStr {{set grid}} initArgStr $arguments output outputStr {"set output '[dget $arguments output]'"} initArgStr $arguments xlabel xlabelStr {"set xlabel '[dget $arguments xlabel]'"} initArgStr $arguments ylabel ylabelStr {"set ylabel '[dget $arguments ylabel]'"} initArgStr $arguments names columnNames {[dget $arguments names]} initArgStr $arguments optcmd optcmdStr {[dget $arguments optcmd]} initArgStr $arguments boxwidth boxwidthStr {"set boxwidth '[dget $arguments boxwidth]'"} initTerminalStr $arguments terminalStr if {[dexist $arguments darkmode]} { set darkmodeStr "set term $terminalStr size [join [dget $arguments size] ,] noenhanced background rgb 'black'" append darkmodeStr "\n" $::gnuplotutil::darkmodeStyle } else { set darkmodeStr "set term $terminalStr size [join [dget $arguments size] ,] noenhanced" } if {[dexist $arguments fill]} { if {[dexist $arguments transparent]} { set fillStr "set style fill transparent [dget $arguments fill]" } else { set fillStr "set style fill [dget $arguments fill]" } if {[dexist $arguments density]} { append fillStr " [dget $arguments density]" } if {[dexist $arguments border]} { append fillStr " border lt [dget $arguments border]" } } else { set fillStr {} } if {[dexist $arguments style]} { if {([dexist $arguments gap]) && ([dget $arguments style] in {clustered})} { set styleStr "set style histogram [dget $arguments style] gap [dget $arguments gap]" } else { set styleStr "set style histogram [dget $arguments style]" } } else { set styleStr {} } set yColumnCount 0 foreach val [dget $arguments columns] { if {[llength $val] != [llength [dget $arguments x]]} { return -code error "Number of points '[llength $val]' of y-axis data (column $yColumnCount) doesn't match the number of points '[llength [dget $arguments x]]' of x-axis data" incr yColumnCount } } # fill output structure with values set numCol [llength [dget $arguments columns]] if {([dexist $arguments names])} { if {[llength $columnNames]!=[= {$numCol}]} { return -code error "Column names count '[llength $columnNames]' is not the same as count '$numCol' of data columns" } else { foreach columnName $columnNames { lappend processedNames "\"$columnName\"" } lappend outList "{ } [join $processedNames]" set autoTitleStr {set key autotitle columnheader} } } else { set autoTitleStr {} } set numRow [llength [dget $arguments x]] for {set i 0} {$i<$numRow} {incr i} { set row [@ [dget $arguments x] $i] foreach val [dget $arguments columns] { lappend row [@ $val $i] } lappend outList $row } # save data to temporary file or use inline data block if {[dexist $arguments inline]} { set inlineData "\$data << EOD\n[::csv::joinlist $outList " "]\nEOD\n" } else { set counter 0 while {true} { set filePath [file join [dget $arguments path] gnuplotTemp${counter}.csv] if {[file exists $filePath]} { incr counter } else { set resFile [open $filePath w+] break } } puts $resFile [::csv::joinlist $outList { }] close $resFile } # create command strings for gnuplot if {[dexist $arguments inline]} { set commandStr "$inlineData plot \$data using 2:xtic(1) " } else { set commandStr "plot 'gnuplotTemp${counter}.csv' using 2:xtic(1)" } for {set i 1} {$i<$numCol} {incr i} { set commandStr "${commandStr}, '' using [= {$i+2}]" } set commandList [list {set mouse} $outputStr $darkmodeStr {set style data histogram} $styleStr $fillStr $optcmdStr $gridStr $autoTitleStr $xlabelStr $ylabelStr $boxwidthStr $commandStr {pause mouse close}] set commandStr [join [lmap elem $commandList {= {$elem eq {} ? [continue] : $elem}}] "\n"] if {[dexist $arguments background]} { set status [catch {exec gnuplot << "\n$commandStr\n" & } errorStr] } else { set status [catch {exec gnuplot << "\n$commandStr\n"} errorStr] } if {![dexist $arguments nodelete] && ![dexist $arguments inline]} { file delete $filePath } if {$status>0} { return $errorStr } else { return $commandStr } }
plotXNYN [::gnuplotutil]Top, Main, Index
Plots 2D graphs in Gnuplot with individual x-values.
Parameters
-background | Enables running gnuplot in background, requires -nodelete or -inline switch. |
-columns | Provides the x and y data to plot, the number of columns is not restricted and must be provided at the end of command after all switches. |
-darkmode | Enables dark mode for graph. |
-grid | Enables display of grid. |
-inline | Provides data directly in command string, without creating temporary file. |
-lstyles | Set individual styles for each graph in the same order as data columns provided, must have the length 2*(number of x-y data pairs) |
-names | Enables setting the column names of provided data, value must be provided as list. |
-nodelete | Disables deleting of temporary file after end of plotting. |
-optcmd | Provides optional string that may contain additional commands to gnuplot. |
-output | Command redirects output to the specified file or device. |
-path | Provides location of temporary file, default is the current location. |
-size | Provides size of the window, must be list with two elements: width and height in pixels. |
-terminal | Selects terminal, default 'x11' on Linux and 'windows' on Windows. |
-xlabel | Provides x-axis label to display, string must be provided after it. |
-xlog | Enables log scale of x axis. |
-xrange | Provides minimum and maximum values of x-axis, must be list with two elements. |
-ylabel | Provides y-axis label to display, string must be provided after it. |
-ylog | Enables log scale of y axis. |
-yrange | Provides minimum and maximum values of y-axis, must be list with two elements. |
Description
Example: set x1 [list 0 1 2 3 4 5 6] set y1 [list 0 1 4 9 16 25 36] set x2 [list 0 1 2 3 4 5 6 7] set y2 [list 0 1 8 27 64 125 216 350] gnuplotutil::plotXNYN -xlog -ylog -xlabel "x label" -ylabel "y label" -darkmode -grid -names [list y1 y2] -columns $x1 $y1 $x2 $y2
Return value
creates gnuplot window with plotted data, and returns string contains commands sent to gnuplot
proc ::gnuplotutil::plotXNYN {args} { # Plots 2D graphs in Gnuplot with individual x-values. # -xlog - enables log scale of x axis # -ylog - enables log scale of y axis # -xrange - provides minimum and maximum values of x-axis, must be list with two elements # -yrange - provides minimum and maximum values of y-axis, must be list with two elements # -background - enables running gnuplot in background, requires -nodelete or -inline switch # -inline - provides data directly in command string, without creating temporary file # -nodelete - disables deleting of temporary file after end of plotting # -xlabel - provides x-axis label to display, string must be provided after it # -ylabel - provides y-axis label to display, string must be provided after it # -optcmd - provides optional string that may contain additional commands to gnuplot # -terminal - selects terminal, default 'x11' on Linux and 'windows' on Windows # -size - provides size of the window, must be list with two elements: width and height in pixels # -grid - enables display of grid # -darkmode - enables dark mode for graph # -output - command redirects output to the specified file or device # -path - provides location of temporary file, default is the current location # -names - enables setting the column names of provided data, value must be provided as list # -lstyles - set individual styles for each graph in the same order as data columns provided, # must have the length 2*(number of x-y data pairs) # -columns - provides the x and y data to plot, the number of columns is not restricted and must # be provided at the end of command after all switches # Returns: creates gnuplot window with plotted data, and returns string contains commands sent to gnuplot # Synopsis: ?-xlog? ?-ylog? ?-background? ?-nodelete? ?-xlabel string? ?-ylabel string? ?-optcmd string? # ?-terminal? ?-grid? ?-darkmode? ?-size list? ?-path string? ?-names list? ?-lstyles list? ?-output string? # -columns xList1 yList1 ?xList2 yList2 ...? # ``` # Example: # set x1 [list 0 1 2 3 4 5 6] # set y1 [list 0 1 4 9 16 25 36] # set x2 [list 0 1 2 3 4 5 6 7] # set y2 [list 0 1 8 27 64 125 216 350] # gnuplotutil::plotXNYN -xlog -ylog -xlabel "x label" -ylabel "y label" -darkmode -grid -names [list y1 y2] # -columns $x1 $y1 $x2 $y2 # ``` set arguments [argparse -inline -help {Plots 2D graphs in Gnuplot with individual x-values} { {-xlog -help {Enables log scale of x axis}} {-ylog -help {Enables log scale of y axis}} {-xrange= -type list -help {Provides minimum and maximum values of x-axis, must be list with two elements}} {-yrange= -type list -help {Provides minimum and maximum values of y-axis, must be list with two elements}} {-background -help {Enables running gnuplot in background}} {-inline -help {Provides data directly in command string, without creating temporary file}} {-nodelete -forbid inline -help {Disables deleting of temporary file after end of plotting}} {-xlabel= -help {Provides x-axis label to display}} {-ylabel= -help {Provides y-axis label to display}} {-optcmd= -help {Provides optional string that may contain additional commands to gnuplot}} {-terminal= -help {Selects terminal, default 'x11' on Linux and 'windows' on Windows}} {-size= -default {800 600} -type list -help {Provides size of the window, must be list with two elements: width and height in pixels}} {-grid -help {Enables display of grid}} {-darkmode -help {Enables dark mode for graph}} {-output= -help {Command redirects output to the specified file or device}} {-path= -default {} -help {Provides location of temporary file}} {-names= -type list -help {Enables setting the column names of provided data, value must be provided as list in the same order as data columns provided, must have the length equal to the number of y data colums}} {-lstyles= -type list -help {Sets individual styles for each graph in the same order as data columns provided, must have the length 2*(number of x-y data pairs)}} {-columns -catchall -help {Provides the y data to plot, the number of columns is not restricted and must be provided at the end of command after all switches}} }] if {[dexist $arguments background]} { AliasesKeysCheck $arguments {nodelete inline} } initArgStr $arguments xlog xscaleStr {{set logscale x}} initArgStr $arguments ylog yscaleStr {{set logscale y}} initRangeStr $arguments xrange xrangeStr initRangeStr $arguments yrange yrangeStr initArgStr $arguments grid gridStr {{set grid}} initArgStr $arguments output outputStr {"set output '[dget $arguments output]'"} initArgStr $arguments xlabel xlabelStr {"set xlabel '[dget $arguments xlabel]'"} initArgStr $arguments ylabel ylabelStr {"set ylabel '[dget $arguments ylabel]'"} initArgStr $arguments names columnNames {[dget $arguments names]} initArgStr $arguments lstyles lineStyles {[dget $arguments lstyles]} initArgStr $arguments optcmd optcmdStr {[dget $arguments optcmd]} initTerminalStr $arguments terminalStr if {[dexist $arguments darkmode]} { set darkmodeStr "set term $terminalStr size [join [dget $arguments size] ,] noenhanced background rgb 'black'" append darkmodeStr "\n" $::gnuplotutil::darkmodeStyle } else { set darkmodeStr "set term $terminalStr size [join [dget $arguments size] ,] noenhanced" } set columnsNum [llength [dget $arguments columns]] if {$columnsNum % 2 != 0} { return -code error {Number of data columns $columnsNum is odd} } set dataNum [= {int($columnsNum/2)}] set yColumnCount 0 for {set i 0} {$i<$dataNum} {incr i} { set xLen [llength [@ [dget $arguments columns] [= {int($i*2)}]]] set yLen [llength [@ [dget $arguments columns] [= {int($i*2+1)}]]] lappend xLengths $xLen if {$xLen != $yLen} { return -code error "Number of points '$yLen' of y-axis data (column [= {1+$yColumnCount}]) doesn't match the number of points '$xLen' of x-axis data (column $yColumnCount)" } incr yColumnCount } # fill output structure with values if {([dexist $arguments names])} { set headerString {} if {[llength $columnNames]!=$dataNum} { return -code error "Column names count '[llength $columnNames]' is not the same as count '$dataNum' of data columns" } else { for {set i 0} {$i<=$dataNum} {incr i} { set headerString "${headerString} { } \"[@ $columnNames $i]\"" } lappend outList $headerString set autoTitleStr {set key autotitle columnheader} } } else { set autoTitleStr {} } if {([dexist $arguments lstyles])} { if {[llength $lineStyles]!=$dataNum} { return -code error "Lines styles count '[llength $lineStyles]' is not the same as count '$dataNum' of data columns" } } else { for {set i 0} {$i<$dataNum} {incr i} { lappend lineStyles {with lines} } } set numRow [tcl::mathfunc::max {*}$xLengths] for {set i 0} {$i<$numRow} {incr i} { set row {} for {set j 0} {$j<$dataNum} {incr j} { set columns [dget $arguments columns] set xVal [@ [@ $columns [= {int($j*2)}]] $i] set yVal [@ [@ $columns [= {int($j*2+1)}]] $i] if {$xVal=={}} { lappend row { } { } } else { lappend row $xVal $yVal } } lappend outList $row } # save data to temporary file or use inline data block if {[dexist $arguments inline]} { set inlineData "\$data << EOD\n[::csv::joinlist $outList " "]\nEOD\n" } else { set counter 0 while {true} { set filePath [file join [dget $arguments path] gnuplotTemp${counter}.csv] if {[file exists $filePath]} { incr counter } else { set resFile [open $filePath w+] break } } puts $resFile [::csv::joinlist $outList { }] close $resFile } # create command strings for gnuplot if {[dexist $arguments inline]} { set commandStr "$inlineData plot \$data using 1:2 [@ $lineStyles 0] " } else { set commandStr "plot '[file join [dget $arguments path] gnuplotTemp${counter}.csv]' using 1:2 [@ $lineStyles 0] " } for {set i 1} {$i<$dataNum} {incr i} { set commandStr "${commandStr}, '' using [= {$i*2+1}]:[= {$i*2+2}] [@ $lineStyles $i] " } set commandList [list {set mouse} $outputStr $darkmodeStr $xrangeStr $yrangeStr $optcmdStr $autoTitleStr $xlabelStr $ylabelStr $xscaleStr $yscaleStr $gridStr $commandStr {pause mouse close}] set commandStr [join [lmap elem $commandList {= {$elem eq {} ? [continue] : $elem}}] "\n"] if {[dexist $arguments background]} { set status [catch {exec gnuplot << "\n$commandStr\n" & } errorStr] } else { set status [catch {exec gnuplot << "\n$commandStr\n"} errorStr] } if {![dexist $arguments nodelete] && ![dexist $arguments inline]} { file delete $filePath } if {$status>0} { return $errorStr } else { return $commandStr } }
plotXNYNMp [::gnuplotutil]Top, Main, Index
Auxilary function for gnuplotutil::multiplotXNYN, creates command strings and data files for individual plots
Parameters
-columns | Provides the x and y data to plot, the number of columns is not restricted and must be provided at the end of command after all switches. |
-grid | Enables display of grid. |
-inline | Provides data directly in command string, without creating temporary file, and passes datablock number. |
-lstyles | Set individual styles for each graph in the same order as data columns provided, must have the length 2*(number of x-y data pairs) |
-names | Enables setting the column names of provided data, value must be provided as list in the same order as data columns provided, must have the length 2*(number of x-y data pairs) |
-optcmd | Provides optional string that may contain additional commands to gnuplot. |
-origin | Set origin of the subplot. |
-path | Provides location of temporary file, default is current location. |
-size | Set size of the subplot. |
-xlabel | Provides x-axis label to display, string must be provided after it. |
-xlog | Enables log scale of x axis. |
-xrange | Provides minimum and maximum values of x-axis, must be list with two elements. |
-ylabel | Provides y-axis label to display, string must be provided after it. |
-ylog | Enables log scale of y axis. |
-yrange | Provides minimum and maximum values of y-axis, must be list with two elements. |
Return value
list that contains command script and name of data file
proc ::gnuplotutil::plotXNYNMp {args} { # Auxilary function for gnuplotutil::multiplotXNYN, creates command strings and data files # for individual plots # -xlog - enables log scale of x axis # -ylog - enables log scale of y axis # -xrange - provides minimum and maximum values of x-axis, must be list with two elements # -yrange - provides minimum and maximum values of y-axis, must be list with two elements # -xlabel - provides x-axis label to display, string must be provided after it # -ylabel - provides y-axis label to display, string must be provided after it # -grid - enables display of grid # -size - set size of the subplot # -origin - set origin of the subplot # -optcmd - provides optional string that may contain additional commands to gnuplot # -path - provides location of temporary file, default is current location # -inline - provides data directly in command string, without creating temporary file, and passes datablock number # -names - enables setting the column names of provided data, value must be provided as list # in the same order as data columns provided, must have the length 2*(number of x-y data pairs) # -lstyles - set individual styles for each graph in the same order as data columns provided, # must have the length 2*(number of x-y data pairs) # -columns - provides the x and y data to plot, the number of columns is not restricted and must # be provided at the end of command after all switches # Returns: list that contains command script and name of data file # Synopsis: ?-xlog? ?-ylog? ?-xlabel string? ?-ylabel string? ?-grid? ?-path string? ?-names list? # -columns xList1 yList1 ?xList2 yList2 ...? set arguments [argparse -inline -help {Auxilary function for gnuplotutil::multiplotXNYN, creates command strings and data files for individual plots} { {-xlog -help {Enables log scale of x axis}} {-ylog -help {Enables log scale of y axis}} {-xrange= -type list -help {Provides minimum and maximum values of x-axis, must be list with two elements}} {-yrange= -type list -help {Provides minimum and maximum values of y-axis, must be list with two elements}} {-inline -help {Provides data directly in command string, without creating temporary file}} {-xlabel= -help {Provides x-axis label to display}} {-ylabel= -help {Provides y-axis label to display}} {-grid -help {Enables display of grid}} {-size= -type list -help {Provides size of the window, must be list with two elements: width and height in pixels}} {-origin= -help {Set origin of the subplot}} {-optcmd= -help {Provides optional string that may contain additional commands to gnuplot}} {-path= -default {} -help {Provides location of temporary file}} {-names= -type list -help {Enables setting the column names of provided data, value must be provided as list in the same order as data columns provided, must have the length equal to the number of y data colums}} {-lstyles= -type list -help {Sets individual styles for each graph in the same order as data columns provided, must have the length 2*(number of x-y data pairs)}} {-columns -catchall -help {Provides the y data to plot, the number of columns is not restricted and must be provided at the end of command after all switches}} }] initArgStr $arguments size sizeStr {"set size [join [dget $arguments size] ,]"} initArgStr $arguments origin originStr {"set origin [join [dget $arguments origin] ,]"} initArgStr $arguments xlog xscaleStr {{set logscale x}} initArgStr $arguments xlog xscaleStrUnset {{unset logscale x}} initRangeStr $arguments xrange xrangeStr initArgStr $arguments xrange xrangeStrUnset {{unset xrange}} initRangeStr $arguments yrange yrangeStr initArgStr $arguments yrange yrangeStrUnset {{unset yrange}} initArgStr $arguments ylog yscaleStr {{set logscale y}} initArgStr $arguments ylog yscaleStrUnset {{unset logscale y}} initArgStr $arguments grid gridStr {{set grid}} initArgStr $arguments grid gridStrUnset {{unset grid}} initArgStr $arguments lstyles lineStyles {[dget $arguments lstyles]} initArgStr $arguments names columnNames {[dget $arguments names]} initArgStr $arguments xlabel xlabelStr {"set xlabel '[dget $arguments xlabel]'"} initArgStr $arguments xlabel xlabelStrUnset {{unset xlabel}} initArgStr $arguments ylabel ylabelStr {"set ylabel '[dget $arguments ylabel]'"} initArgStr $arguments ylabel ylabelStrUnset {{unset ylabel}} initArgStr $arguments optcmd optcmdStr {[dget $arguments optcmd]} set columnsNum [llength [dget $arguments columns]] if {$columnsNum % 2 != 0} { return -code error "Number of data columns '$columnsNum' is odd" } set dataNum [= {int($columnsNum/2)}] set yColumnCount 0 for {set i 0} {$i<$dataNum} {incr i} { set xLen [llength [@ [dget $arguments columns] [= {int($i*2)}]]] set yLen [llength [@ [dget $arguments columns] [= {int($i*2+1)}]]] lappend xLengths $xLen if {$xLen != $yLen} { return -code error "Number of points '$yLen' of y-axis data (column [= {1+$yColumnCount}]) doesn't match the number of points '$xLen' of x-axis data (column $yColumnCount)" } incr yColumnCount } # fill output structure with values if {([dexist $arguments names])} { set headerString {} if {[llength $columnNames]!=$dataNum} { return -code error "Column names count '[llength $columnNames]' is not the same as count '$dataNum' of data columns" } else { for {set i 0} {$i<=$dataNum} {incr i} { set headerString "${headerString} { } \"[@ $columnNames $i]\"" } lappend outList $headerString set autoTitleStr {set key autotitle columnheader} } } else { set autoTitleStr {} } if {([dexist $arguments lstyles])} { if {[llength $lineStyles]!=$dataNum} { return -code error "Lines styles count '[llength $lineStyles]' is not the same as count '$dataNum' of data columns" } } else { for {set i 0} {$i<$dataNum} {incr i} { lappend lineStyles {with lines} } } set numRow [tcl::mathfunc::max {*}$xLengths] for {set i 0} {$i<$numRow} {incr i} { set row {} for {set j 0} {$j<$dataNum} {incr j} { set columns [dget $arguments columns] set xVal [@ [@ $columns [= {int($j*2)}]] $i] set yVal [@ [@ $columns [= {int($j*2+1)}]] $i] if {$xVal=={}} { lappend row { } { } } else { lappend row $xVal $yVal } } lappend outList $row } # save data to temporary file if {[dexist $arguments inline]} { set inlineData "\$data[dget $arguments inline] << EOD\n[::csv::joinlist $outList " "]\nEOD\n" } else { set counter 0 while {true} { set filePath [file join [dget $arguments path] gnuplotTemp${counter}.csv] if {[file exists $filePath]} { incr counter } else { set resFile [open $filePath w+] break } } puts $resFile [::csv::joinlist $outList { }] close $resFile } # create command strings for gnuplot if {[dexist $arguments inline]} { set commandStr "$inlineData plot \$data[dget $arguments inline] using 1:2 [@ $lineStyles 0] " } else { set commandStr "plot '[file join [dget $arguments path] gnuplotTemp${counter}.csv]' using 1:2 [@ $lineStyles 0] " } for {set i 1} {$i<$dataNum} {incr i} { set commandStr "${commandStr}, '' using [= {$i*2+1}]:[= {$i*2+2}] [@ $lineStyles $i] " } set commandList [list $optcmdStr $sizeStr $originStr $autoTitleStr $xlabelStr $ylabelStr $xrangeStr $yrangeStr $xscaleStr $yscaleStr $gridStr $commandStr $xlabelStrUnset $ylabelStrUnset $xscaleStrUnset $yscaleStrUnset $gridStrUnset $xrangeStrUnset $yrangeStrUnset] set commandStr [join [lmap elem $commandList {= {$elem eq {} ? [continue] : $elem}}] "\n"] if {[dexist $arguments inline]} { return [dict create cmdString $commandStr] } else { return [dict create cmdString $commandStr file $filePath] } }
plotXYN [::gnuplotutil]Top, Main, Index
Plots 2D graphs in Gnuplot with the common x-values.
Parameters
-background | Enables running gnuplot in background, requires -nodelete or -inline switch. |
-columns | Provides the y data to plot, the number of columns is not restricted and must be provided at the end of command after all switches. |
-darkmode | Enables dark mode for graph. |
-grid | Enables display of grid. |
-inline | Provides data directly in command string, without creating temporary file. |
-lstyles | Set individual styles for each graph in the same order as data columns provided, must have the length 2*(number of x-y data pairs) |
-names | Enables setting the column names of provided data, value must be provided as list in the same order as data columns provided, must have the length equal to the number of y data colums. |
-nodelete | Disables deleting of temporary file after end of plotting. |
-optcmd | Provides optional string that may contain additional commands to gnuplot. |
-output | Command redirects output to the specified file or device. |
-path | Provides location of temporary file, default is the current location. |
-size | Provides size of the window, must be list with two elements: width and height in pixels. |
-terminal | Selects terminal, default 'x11' on Linux and 'windows' on Windows. |
-xlabel | Provides x-axis label to display, string must be provided after it. |
-xlog | Enables log scale of x axis. |
-xrange | Provides minimum and maximum values of x-axis, must be list with two elements. |
-ylabel | Provides y-axis label to display, string must be provided after it. |
-ylog | Enables log scale of y axis. |
-yrange | Provides minimum and maximum values of y-axis, must be list with two elements. |
x | List of x-point for 2D graph. |
Description
Example: set x [list 0 1 2 3 4 5 6] set y1 [list 0 1 4 9 16 25 36] set y2 [list 0 1 8 27 64 125 216] gnuplotutil::plotXYN $x -xlog -ylog -xlabel "x label" -ylabel "y label" -grid -names [list y1 y2] -columns $y1 $y2
Return value
creates gnuplot window with plotted data, and returns string contains commands sent to gnuplot
proc ::gnuplotutil::plotXYN {args} { # Plots 2D graphs in Gnuplot with the common x-values. # x - list of x-point for 2D graph # -xlog - enables log scale of x axis # -ylog - enables log scale of y axis # -xrange - provides minimum and maximum values of x-axis, must be list with two elements # -yrange - provides minimum and maximum values of y-axis, must be list with two elements # -background - enables running gnuplot in background, requires -nodelete or -inline switch # -inline - provides data directly in command string, without creating temporary file # -nodelete - disables deleting of temporary file after end of plotting # -xlabel - provides x-axis label to display, string must be provided after it # -ylabel - provides y-axis label to display, string must be provided after it # -optcmd - provides optional string that may contain additional commands to gnuplot # -terminal - selects terminal, default 'x11' on Linux and 'windows' on Windows # -grid - enables display of grid # -darkmode - enables dark mode for graph # -output - command redirects output to the specified file or device # -size - provides size of the window, must be list with two elements: width and height in pixels # -path - provides location of temporary file, default is the current location # -names - enables setting the column names of provided data, value must be provided as list # in the same order as data columns provided, must have the length equal to the number of y data colums # -lstyles - set individual styles for each graph # in the same order as data columns provided, must have the length 2*(number of x-y data pairs) # -columns - provides the y data to plot, the number of columns is not restricted and must be provided at the end # of command after all switches # Returns: creates gnuplot window with plotted data, and returns string contains commands sent to gnuplot # Synopsis: x ?-xlog? ?-ylog? ?-background? ?-nodelete? ?-xlabel string? ?-ylabel string? ?-optcmd string? # ?-terminal? ?-grid? ?-darkmode? ?-size list? ?-path string? ?-names list? ?-lstyles list? ?-output string? # -columns yList1 ?yList2 ...? # ``` # Example: # set x [list 0 1 2 3 4 5 6] # set y1 [list 0 1 4 9 16 25 36] # set y2 [list 0 1 8 27 64 125 216] # gnuplotutil::plotXYN $x -xlog -ylog -xlabel "x label" -ylabel "y label" -grid -names [list y1 y2] # -columns $y1 $y2 # ``` set arguments [argparse -inline -pfirst -help {Plots 2D graphs in Gnuplot with the common x-values} { {x -help {List of x-point for 2D graph}} {-xlog -help {Enables log scale of x axis}} {-ylog -help {Enables log scale of y axis}} {-xrange= -type list -help {Provides minimum and maximum values of x-axis, must be list with two elements}} {-yrange= -type list -help {Provides minimum and maximum values of y-axis, must be list with two elements}} {-background -help {Enables running gnuplot in background}} {-inline -help {Provides data directly in command string, without creating temporary file}} {-nodelete -forbid inline -help {Disables deleting of temporary file after end of plotting}} {-xlabel= -help {Provides x-axis label to display}} {-ylabel= -help {Provides y-axis label to display}} {-optcmd= -help {Provides optional string that may contain additional commands to gnuplot}} {-terminal= -help {Selects terminal, default 'x11' on Linux and 'windows' on Windows}} {-size= -default {800 600} -type list -help {Provides size of the window, must be list with two elements: width and height in pixels}} {-grid -help {Enables display of grid}} {-darkmode -help {Enables dark mode for graph}} {-output= -help {Command redirects output to the specified file or device}} {-path= -default {} -help {Provides location of temporary file}} {-names= -type list -help {Enables setting the column names of provided data, value must be provided as list in the same order as data columns provided, must have the length equal to the number of y data colums}} {-lstyles= -type list -help {Sets individual styles for each graph in the same order as data columns provided, must have the length 2*(number of x-y data pairs)}} {-columns -catchall -help {Provides the y data to plot, the number of columns is not restricted and must be provided at the end of command after all switches}} }] if {[dexist $arguments background]} { AliasesKeysCheck $arguments {nodelete inline} } initArgStr $arguments xlog xscaleStr {{set logscale x}} initArgStr $arguments ylog yscaleStr {{set logscale y}} initRangeStr $arguments xrange xrangeStr initRangeStr $arguments yrange yrangeStr initArgStr $arguments grid gridStr {{set grid}} initArgStr $arguments output outputStr {"set output '[dget $arguments output]'"} initArgStr $arguments xlabel xlabelStr {"set xlabel '[dget $arguments xlabel]'"} initArgStr $arguments ylabel ylabelStr {"set ylabel '[dget $arguments ylabel]'"} initArgStr $arguments names columnNames {[dget $arguments names]} initArgStr $arguments lstyles lineStyles {[dget $arguments lstyles]} initArgStr $arguments optcmd optcmdStr {[dget $arguments optcmd]} initTerminalStr $arguments terminalStr if {[dexist $arguments darkmode]} { set darkmodeStr "set term $terminalStr size [join [dget $arguments size] ,] noenhanced background rgb 'black'" append darkmodeStr "\n" $::gnuplotutil::darkmodeStyle } else { set darkmodeStr "set term $terminalStr size [join [dget $arguments size] ,] noenhanced" } set yColumnCount 0 foreach val [dget $arguments columns] { if {[llength $val]!=[llength [dget $arguments x]]} { return -code error "Number of points '[llength $val]' of y-axis data (column $yColumnCount) doesn't match the number of points '[llength [dget $arguments x]]' of x-axis data" incr yColumnCount } } # fill output structure with values set numCol [llength [dget $arguments columns]] if {([dexist $arguments names])} { if {[llength $columnNames]!=[= {$numCol}]} { return -code error "Column names count '[llength $columnNames]' is not the same as count '$numCol' of data columns" } else { foreach columnName $columnNames { lappend processedNames "\"$columnName\"" } lappend outList "{ } [join $processedNames]" set autoTitleStr {set key autotitle columnheader} } } else { set autoTitleStr {} } if {([dexist $arguments lstyles])} { if {[llength $lineStyles]!=$numCol} { return -code error "Lines styles count '[llength $lineStyles]' is not the same as count '$numCol' of data columns" } } else { for {set i 0} {$i<$numCol} {incr i} { lappend lineStyles {with lines} } } set numRow [llength [dget $arguments x]] for {set i 0} {$i<$numRow} {incr i} { set row [@ [dget $arguments x] $i] foreach val [dget $arguments columns] { lappend row [@ $val $i] } lappend outList $row } # save data to temporary file or use inline data block if {[dexist $arguments inline]} { set inlineData "\$data << EOD\n[::csv::joinlist $outList " "]\nEOD\n" } else { set counter 0 while {true} { set filePath [file join [dget $arguments path] gnuplotTemp${counter}.csv] if {[file exists $filePath]} { incr counter } else { set resFile [open $filePath w+] break } } puts $resFile [::csv::joinlist $outList { }] close $resFile } # create command strings for gnuplot if {[dexist $arguments inline]} { set commandStr "$inlineData plot \$data using 1:2 [@ $lineStyles 0] " } else { set commandStr "plot '[file join [dget $arguments path] gnuplotTemp${counter}.csv]' using 1:2 [@ $lineStyles 0] " } for {set i 1} {$i<$numCol} {incr i} { set commandStr "${commandStr}, '' using 1:[= {$i+2}] [@ $lineStyles $i] " } set commandList [list {set mouse} $outputStr $darkmodeStr $xrangeStr $yrangeStr $optcmdStr $autoTitleStr $xlabelStr $ylabelStr $xscaleStr $yscaleStr $gridStr $commandStr {pause mouse close}] set commandStr [join [lmap elem $commandList {= {$elem eq {} ? [continue] : $elem}}] "\n"] if {[dexist $arguments background]} { set status [catch {exec gnuplot << "\n$commandStr\n" & } errorStr] } else { set status [catch {exec gnuplot << "\n$commandStr\n"} errorStr] } if {![dexist $arguments nodelete] && ![dexist $arguments inline]} { file delete $filePath } if {$status>0} { return $errorStr } else { return $commandStr } }