has2k1 / gnuplot_kernel

A Jupyter/IPython kernel for gnuplot
BSD 3-Clause "New" or "Revised" License
83 stars 25 forks source link

Multiple plots in for loop not working #34

Closed dengesCU closed 1 year ago

dengesCU commented 1 year ago

gnuplot_kernel successfully recognizes when there are multiple plot instructions such as

plot x**0 t "x^0"
plot x**1 t "x^1"
plot x**2 t "x^2"

However, when using for loops such as

do for [t=0:2] {
    plot x**t t sprintf("x^%d",t)
}

this fails and only the last plot is displayed.

Cause: In the function def add_inline_image_statements(self, code): in kernel.py the code is scanned for plot statements and for each plot statement a set output line is added (unless when using multiplot). Thus, each plot in the loop will be written to the same output file, hence overwriting any previous plot. Consequentially, only the last plot is displayed.

Possible fix: Detect for loops. When a for loop is detected, add a counter variable that is incremented in the loop everytime. Append the counter to the filename for the output file (which should not overwrite previous plots then).

has2k1 commented 1 year ago

@dengesCU, Thanks for the diagnosis, it definitely saved some me time. The solution is a little different. There is a counter for every set output. That way, nested loops are also automatically handled.

dengesCU commented 1 year ago

Great, thank you! Particularly for being so quick! Much appreciated.

I installed the developer version and it seems to work as intended now. Btw, it should read @main and not @master in the installation instructions for the Development version in the readme