PyGnuplot icon indicating copy to clipboard operation
PyGnuplot copied to clipboard

Another suggestion

Open steview2000 opened this issue 8 years ago • 1 comments

Hi Ben,

For analyzing data, I often run the same python script in parallel, with slightly different parameters. In this case saving the values to be plotted sometimes cause interferences. It is in this case helpful to pipe data directly into gnuplot instead of saving a file first. Fortunately gnuplot provides the plot "-" option.

I have changed the plot-function, so that it can take multiple data points to be plotted and as well as strings with the plot option, such as:

plot([x,y],"u 1:2 w p ls 1",[x2,y2],"u 1:2 w l ls 2")

The plot function:

def plot(data,*args): """ Sends data to gnuplot via pipe """

x,y = data
# identifier necessary to check whether two consecutive arguments are strings

lastdata = 1

# create data string
data_string = ''
for i in range(np.size(x)):
    data_string = data_string +('%lf %lf\n'%(x[i],y[i]))
data_string = data_string+'e\n'

# create plot string
plot_string = 'plot "-"'

# Now go through all arguments and check whether it is a data, or a string with plot
# specification

for i in range(len(args)):
    arg = args[i]
    if type(arg) == str:
        if lastdata == 1:
            plot_string = plot_string+arg
            lastdata=0
        else:
            print("Error!! Data expected in %d argument!"%i)
            break
    elif type(arg)==list:
        if lastdata==1:
            plot_string = plot_string+" u 1:2 notitle w p "
        x,y = arg
        for i in range(np.size(x)):
            data_string = data_string +('%lf %lf\n'%(x[i],y[i]))
        data_string = data_string+'e\n'
        plot_string = plot_string+', "-"'
        lastdata=1

    else:
        print("Wrong data type in argument %d "%i)
        print("Str or List expected, but "+type(arg)+" found!")
        break

if lastdata==1:
    plot_string = plot_string+" u 1:2 notitle w p "

# Send plot_string to gnuplot
c(plot_string)

# Send data_string to gnuplot
c(data_string)

`

steview2000 avatar Jun 28 '17 20:06 steview2000

Hi,

This is another very nice idea, thank you!

benschneider avatar Jun 30 '17 21:06 benschneider