5
$\begingroup$

I have an image (image1) in which I am detecting bright objects and showing them in image2.

Now I am determining the number of objects versus the horizontal position x inside of sliding window of dx. The resut (Transpose[{x,number}]) is stored in data.

I would like to show in a single plot

  • data curve of number of objects versus x
  • image1 above image2 above the data curve, preserving the aspect ratio of the images

Question:

How can I inset (! from knowing the plot range, size of images, ... )

  • image1 starting directly from the upper left corner of the plot
  • image2 directly below image1 (without gap)

In principle it is not necessary to inset the images into the plot, but instead somehow to produce a graphics containing in one column from top to bottom image1, image2, and the data curve, whereby the images have the same width as the frame of the plot of data.

What I did:

data = << "https://pastebin.com/raw/rgtKLSE9";

image1 = Import["https://i.imgur.com/EGkUvfU.png"];
image2 = Import["https://i.imgur.com/672v5NF.png"];

ylabel = "object number per dx=2.84 mm";

xPixelSize = 0.01420; (* in mm *)

xmax = ImageDimensions[image1][[1]]*xPixelSize;

plot = ListLinePlot[data, InterpolationOrder -> 0, PlotStyle -> {Blue}, 
 Epilog -> {Inset[image1, {0, 450}, {0, 0}, Scaled[1]], 
   Inset[image2, {0, 350}, {0, 0}, Scaled[1]]}, 
 Frame -> True, FrameLabel -> {{ylabel, ""}, {"x (mm)", ""}}, 
 PlotRange -> {{0, xmax}, {100, 550}}, 
 BaseStyle -> {FontWeight -> "Bold", FontSize -> 20, 
   FontFamily -> "Calibri"}, ImageSize -> 800, 
 PlotStyle -> {Blue, Thick}, AxesStyle -> Thick, FrameStyle -> Thick, 
 AspectRatio -> 1.5]

enter image description here

$\endgroup$
3
  • $\begingroup$ I don't think I'd use inset. I would most likely just display image1, image2 and the plot in a Column expression. $\endgroup$
    – m_goldberg
    Commented Feb 6, 2018 at 12:38
  • $\begingroup$ @m_Goldberg: But how can I ensure that the left and right edge of the images fit exactly to the frame of the plotted curve? $\endgroup$
    – mrz
    Commented Feb 6, 2018 at 12:41
  • $\begingroup$ By specifying ImageSize for each item in the column. $\endgroup$
    – m_goldberg
    Commented Feb 6, 2018 at 13:09

2 Answers 2

4
$\begingroup$

ImageAssemple the images and use it in Inset:

inset = Inset[ImageAssemble[{{image1}, {image2}}], Scaled@{0, 1}, {Left, Top}, Scaled@1];
ListLinePlot[data, InterpolationOrder -> 0, PlotStyle -> {Blue}, Epilog -> inset,
 Frame -> True, FrameLabel -> {{ylabel, ""}, {"x (mm)", ""}}, 
 PlotRange -> {{0, xmax}, {100, 550}}, ImageSize -> 500, 
 BaseStyle -> {FontWeight -> "Bold", FontSize -> 20, FontFamily -> "Calibri"}, 
 PlotStyle -> {Blue, Thick}, AxesStyle -> Thick, FrameStyle -> Thick, AspectRatio -> 1.5]

enter image description here

$\endgroup$
2
  • $\begingroup$ Thanks a lot. This solution is what I was looking for. It works also if image1 and image2 have other sizes. $\endgroup$
    – mrz
    Commented Feb 7, 2018 at 14:26
  • $\begingroup$ @mrz, my pleasure. Thank you for the accept. $\endgroup$
    – kglr
    Commented Feb 7, 2018 at 18:39
3
$\begingroup$

Use:

ListLinePlot[data, InterpolationOrder -> 0, PlotStyle -> {Blue}, 
Epilog -> {Inset[
GraphicsColumn[{image1, image2}, Spacings -> {0, 
-360*(1. - ImageDimensions[image1][[2]]/ImageDimensions[image1][[1]])}], 
Scaled[{0., 1}], {0, 0}, Scaled[1.1]]}, Frame -> True, 
FrameLabel -> {{ylabel, ""}, {"x (mm)", ""}}, 
PlotRange -> {{0, xmax}, {100, 550}}, 
BaseStyle -> {FontWeight -> "Bold", FontSize -> 20, 
FontFamily -> "Calibri"}, ImageSize -> 800, 
PlotStyle -> {Blue, Thick}, AxesStyle -> Thick, FrameStyle -> Thick, 
AspectRatio -> 1.5]

enter image description here

$\endgroup$
7
  • $\begingroup$ Thank you for your help. How did you chose the value -252 in Spacings -> {0, -252}], and the 1.2 in Scaled[1.2]]}? Can this somehow be found automatically from the sizes of the images and the plotRange=PlotRange[plot]? $\endgroup$
    – mrz
    Commented Feb 6, 2018 at 12:48
  • $\begingroup$ in a hurry, it was by trial-error, but I agree and guess that those values could be found from the images size... $\endgroup$ Commented Feb 6, 2018 at 12:57
  • $\begingroup$ I think this is what I want to know ... I would like to find an automatic solution. $\endgroup$
    – mrz
    Commented Feb 6, 2018 at 13:24
  • $\begingroup$ Visually, I would prefer a column with image1, image2, and the plot... $\endgroup$ Commented Feb 6, 2018 at 13:35
  • $\begingroup$ ok, but can you show an automatic finding procedure of the values you found (as you said by trial-error). I mean I also was able to try that out ... but as soon the images size changes the result is not what I want. $\endgroup$
    – mrz
    Commented Feb 6, 2018 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.