3
$\begingroup$

Consider the following figure:

enter image description here

I would like to plot a test function

func[ma_]=1000*ma^5

over it. Is it possible somehow to do?

$\endgroup$
3
  • $\begingroup$ When you say figure, you mean this is an image of a plot only available as an image file? $\endgroup$
    – Syed
    Feb 28 at 11:52
  • $\begingroup$ @Syed : yes, that's it. $\endgroup$ Feb 28 at 15:01
  • 1
    $\begingroup$ My answer below works for raster images. But since you probably got the image from a PDF, you could also extract a vector graphics, and then overlay your plot. $\endgroup$
    – Domen
    Feb 28 at 15:17

1 Answer 1

5
$\begingroup$

You can use Prolog and Inset to position the image behind the plot, but some calibration is needed to get correct coordinates.

First, calibrate the image by determining the location of the bottom-left and top-right corners of the plot.

img = Import["https://i.stack.imgur.com/rUBAY.png", "Image", 
 ImageResolution -> 72];

imageData = ImageData[img, DataReversed -> True];
Manipulate[
 Graphics[{Raster[imageData, {{0, 0}, ImageDimensions[img]}]}, 
  ImageSize -> 500], {{bottomLeft, {0, 0}}}, {{topRight, {300, 
    300}}}, {{bottomLeft, {0, 0}}, Locator}, {{topRight, {300, 300}}, 
  Locator}]

Locator

Then assign the corresponding $x$ and $y$ range*, and use this data to calculate the correct aspect ratio and image scaling.

bottomLeft = {182, 151};
topRight = {1687, 966};

{xRange, yRange} = {{0, 3}, {-3, 7}};

aspectRatio = 1/Divide @@ (topRight - bottomLeft);
scaling = Abs[Subtract @@ xRange]*
   First@ImageDimensions[img]/First@(topRight - bottomLeft);

Finally, use Plot* to draw your function. Make sure the axes are properly aligned, then use Frame -> False and remove SetAlphaChannel to get the final image.

func[ma_] := 1000 * ma^5;

Plot[Log10[func[ma]], {ma, xRange[[1]], 
  xRange[[2]]}, {PlotRange -> {xRange, yRange}, 
  PlotStyle -> {Thick, Red}, 
  Prolog -> {Inset[
     SetAlphaChannel[img, .3], {xRange[[1]], yRange[[1]]}, bottomLeft,
      scaling]}, AxesOrigin -> {0, -3}, AspectRatio -> aspectRatio, 
  Axes -> False, ImageSize -> 500, Axes -> True, ImagePadding -> 50, 
  PlotRangeClipping -> False, Frame -> True}]

Image


* Due to plot coordinate system, it is more convenient to use Plot[Log10[ ...]] instead of LogPlot[].

$\endgroup$

Your Answer

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

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