2
votes

I am using gnuplot 5.2 in WSL with Ubuntu 18.04 and I'm trying to fit a png image in my generated graph.

The data I am using called "data_sof" is:

loc Exp Measured
0. 3 3.2
0.05 1 1.1
0.10 1.5 1.33
0.15 5.34 5.8
0.20 4.26 5.22
0.25 5.70 4.88
0.30 6.21 6.17
0.35 5.15 5.10

The code (as a script) that I am using to plot the data is the following:

set terminal pngcairo font "helvetica,20" size 1000, 800
set border lw 1.5
set tmargin 1.5
set rmargin 20
set xrange [0:7]
set yrange [0:0.45]
set grid
set output "stack.png"

plot \
     "data_sof" u 2:1 w p ps 1.5 pt 2 lc rgb "#3D554F", \
     "data_sof" u 3:1 w p ps 1.5 pt 4 lc rgb "#56B4E9"

The generated graph looks like this: base plot

What I am trying to do is to put the image image.png at the dedicated white space next to the main plot.

I have tried appending the code as follows:

plot \
     "data_sof" u 2:1 w p ps 1.5 pt 2 lc rgb "#3D554F" notitle, \
     "data_sof" u 3:1 w p ps 1.5 pt 4 lc rgb "#56B4E9" notitle, \
     "image.png" binary filetype=png with rgbalpha notitle

But I get the following output which is not what expected. failed attempt

I've also tried scaling the image with dx and dy keywords but with no success.

The image.png can be found here: image.png

EDIT: The desired output, generated with a different tool, is: desired output image

Any help would be very much appreciated!

2
What is the "dedicated white space" (maybe the upper left corner of your plot?) and what is "next" to the main plot? (next or within your graph?) Please add a sketch to make clear what you would like to achieve. - theozh
@theozh By "dedicated white space" I mean the white space within the plot - the one created by increasing the right margin. I hope the added desired output figure will help. - petrosca

2 Answers

2
votes

My understanding is that if you "plot" an image, it has to be within a graph. For your case, you could use multiplot, check help multiplot. You simply set the origins and sizes and remove the border and labels for the second plot. Furthermore, in order to avoid distortion you set the plot size ratio of the second plot the same as as your image (in your case 973 x 673 pixels). Check the following example as a starting point for further tweaking.

Code:

### add/plot image next to graph
reset session

$Data <<EOD
0.      3       3.2
0.05    1       1.1
0.10    1.5     1.33
0.15    5.34    5.8
0.20    4.26    5.22
0.25    5.70    4.88
0.30    6.21    6.17
0.35    5.15    5.10
EOD

unset key

set multiplot
    set origin 0,0
    set size   0.6,1.0
    set grid x,y
    plot $Data u 2:1 w p ps 1.5 pt 2 lc "red",\
            '' u 3:1 w p ps 1.5 pt 4 lc "blue"
    
    set origin 0.6,0.1
    set size   0.4,0.5
    set border 0
    unset tics
    unset label
    set size ratio 973./673
    plot "image.png" binary filetype=png with rgbalpha
    
unset multiplot
### end of code

Result:

enter image description here

1
votes

I realize that you have specified gnuplot version 5.2, but for completeness I will point out that in version 5.4 there is separate data structure type pixmap that holds an image to be placed at any arbitrary position in or out of the plot.

set pixmap 1 "image.png" 
set pixmap 1 at screen 0.75, graph 0.0 width screen 0.2
set rmargin at screen 0.7
plot "data_sof" u 2:1 w p ps 1.5 pt 2 lc rgb "#3D554F" notitle, \
     "data_sof" u 3:1 w p ps 1.5 pt 4 lc rgb "#56B4E9" notitle

enter image description here