Gnuplotting

Create scientific plots using gnuplot

October 13th, 2010 | 1 Comment

If you have to place a lot of labels in your plot and don’t know their place exactly, it can take a very long time to find the right positions. You will specify a position in the set label command and execute it, adjust the position and execute it again, etc.

But their is an easier way. You can use a Gnuplot script to place your labes interactive by hand and get the corresponding label position coordinates. To give you a clue how it functions, have a look at this video:

Fig. 1 Video to show interactive label placing (code to produce this figure, label_loop function)

The Gnuplot code to generate this video is stored in two files. One normal Gnuplot file for plotting the points, using a technique presented in the Plotting single points entry. At the end of the file, after the plot command another Gnuplot file is called with the label to place. Also the set mouse command enables the use of the mouse for our interactive purposes.

# Enable interactive use of the mouse
set mouse

# Adding the labels (INTERACTIVE)
print "\nPosition the labels with your left mouse until you are \ convinced with their position by pressing another key.\n"

call 'label_loop.gnu' 'square'
call 'label_loop.gnu' 'circle'
call 'label_loop.gnu' 'triangle'

print "Done"

Here the call command calls the function label_loop.gnu with the given label as a parameter. In the label_loop.gnu file the label placing is handled. A label is added or placed at a new position as long as the left mouse key is clicked. If the right mouse key (or any other key) is pressed the label is positioned at the last chosen position. This position is also printed to the standard output every time a new position is chosen.

# Initialize a label number
if (!exists("label_number")) label_number = 1;

# Waiting for the key press and display the given label
pause mouse any "$0"

# Check if the left mouse key is pressed and add the given label to the
# plot. Otherwise stop the loop and count the added label
if( MOUSE_BUTTON==1 ) \
    set label label_number "$0" at MOUSE_X,MOUSE_Y textcolor ls 1;\
    print "\n $0 at ",MOUSE_X,MOUSE_Y;\
    replot;\
    reread;\
else \
    label_number = label_number+1;\
    print "\n"

The variable label_number is needed in order to work with more than one label.

If you want to position the center of the label at the mouse position, you can change the set label line to

set label label_number "$0" at MOUSE_X,MOUSE_Y center textcolor ls 1;\

1 Comment

  1. Christoph says:

    I modified your script slightly to work with gnuplot 5.0:

    # Modified version of  'label_loop.gnu' to work with gnuplot 5.0
    
    # Initialize a label number
    if (!exists("label_number") ) { label_number = 1; }
    
    # Waiting for the  key press
    pause mouse any ARG1
    
    # Check if the left mouse key is pressed and add the given label to the plot.
    # Otherwise stop the loop and count the added label
    
    if( MOUSE_BUTTON == 1 ) {
        set label label_number ARG1 at MOUSE_X,MOUSE_Y textcolor ls 1
        print "\n ".ARG1." at ",MOUSE_X,MOUSE_Y
        replot
        call 'label_loop.gnu' ARG1
    }
    if( MOUSE_BUTTON == 3 ) {
        label_number = label_number + 1
        print "\n"
    }