Hello,
when including a jpg or png into a plot, it is quite difficult to keep the original aspect ratio of the image, especially, if the x and y axis have different ranges and the output size is non-square. I did find a way to re-establish the correct aspect ratio, but the workaround is quite cumbersome:
set term pdfcairo color size 8.5cm,5.95cm
set xr [3210:3680]
set yr [0:6000]
# first dummy call to plot in order to establish GPVAL-variables
set out "rubbish.pdf"
plot "some.data"
set out "correct.pdf"
XRANGE = GPVAL_X_MAX-GPVAL_X_MIN
YRANGE = GPVAL_Y_MAX-GPVAL_Y_MIN
scale = 1.0e6 #or whatever size is wanted
plot "some.data", "image.png" binary filetype=png center=(3400,3000) dx=scale/(GPVAL_TERM_XSIZE*YRANGE) dy=scale/(GPVAL_TERM_YSIZE*XRANGE) w rgbalpha
!rm rubbish.pdf
Honestly, I don't understand why dx has to be divided by X-size but Y-range, and dy by X-range and Y-size, but somehow this gives correct results. (Is this a bug?)
It would be nice to have a new option like preserve_aspect_ratio that does this job and eliminates the need of dual-plotting.
Is there a reason you cannot use the command
set view equal xy?Unfortunately, I fail to see any difference when using
set view equal xy– I still have to set up dx and dy in the same manner as above. To be honest, I don't see how the unit length of the axis can be equal in the example given above: x covers a range of 470 on a length of 8.5cm, while y covers 6000 on a length of 5.95cm. This is determined by the data I am plotting.And isn't the command
set viewonly applied tosplot?I guess the situation will get even worse when working with logarithmic axis…
My apologies. The 2D equivalent command is
set size ratio -1. This is documented somewhere but it's hard to find. Here is an example. First I create a junk png plot of known dimensions.Then I start a new session and use your example layout to read in the image.
Of course this reveals that the chosen ranges on x and y may not be appropriate, but it does show that the aspect ratio of the image is preserved.
Your example shows that preserving the png aspect ratio via
set size ratio -1"destroys" the rest of the plot – your result foo.png is obviously not useful.The x- and y-range are determined by the dataset I am plotting, they are representing physical quantities, which is why I can't set them to be equally scaled.
Another workaround for including non-distorted images would be via cairolatex terminal and
set label '\includegraphics[scale=0.1]{image.png}' at 3400,3600, but then positioning will need lots of iterative tests.The example I gave does show how to preserve the aspect ratio of the included png image. If that is not what you wanted, please go back and try to explain more clearly what you do want. Is it that you want to rescale the included image as well as preserving the aspect ratio? That could be done by adding qualifiers
dx=scale dy=scalefor whatever scaling factor you choose.Edit: Or is it that you don't actually care about the size of the included image, i.e. you want it shown independent of the axis scaling? Sorry if I didn't understand that from your original query. Will it be the case that you always know the exact size in pixels of the included image in advance?
Last edit: Ethan Merritt 2019-09-08
Exactly that is what I am asking for, I would like to "detach" the image pixels from x and y (and x2/y2), and simply place it somewhere on the graph. A scaling option like "width = 0.1 graph width" (or screen) would be nice as well.
Here is an example of a very similar picture: https://doi.org/10.1039/c9sc01555h (Open Access)
In Fig. 1 and 2 there are some data plotted against the axis and a small image of a molecule is included.
Unfortunately not, in my specific case I wanted to include four images which all have slightly different sizes. I could of course just look up the sizes.
Related request:
https://sourceforge.net/p/gnuplot/feature-requests/359/
Oh, apologies that I didn't find this old thread. Yeah, a completely empty multiplot with just the image on top of the "real" plot would probably be a possible workaround as well… but probably not less fiddly than my one :-)
Now in the development version as an experimental feature. Compile with -DPIXMAPS.
Unfortunately I can't compile it – configure tells that it doesn't know the option "-DPIXMAPS" (the only file containing this word is gnuplot.doc)… am I doing something wrong?
Ah, sorry for the confusion. I didn't make a configuration option for it. You can either add -DPIXMAPS to the standard compiler options:
Or run your normal configuration procedure and then add a line
#define PIXMAPS
at the end of ./config.h
It should be considered "experimental" at this stage. After testing and feedback I'll either remove the conditional flags or make it a configuration option.
Thank you very much, I used this new feature to create some images for a conference poster that I'm going to present soon. While testing two small possible improvements came to my mind:
width <w>autoscales the height, and it would be nice to have the opposite optionheight <h>that autoscales the width. Maybe it would be possible to merge the three options size, width and height to a singlesizeoption, where an asterisk or some other special character marks autoscaling:scale <w>,*would be the same aswidth <w>is now, andscale *,<h>would be the height.