# How to make a plot over some external figure?

Consider the following figure: I would like to plot a test function

func[ma_]=1000*ma^5


over it. Is it possible somehow to do?

• When you say figure, you mean this is an image of a plot only available as an image file?
– Syed
Feb 28 at 11:52
• @Syed : yes, that's it. Feb 28 at 15:01
• 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. Feb 28 at 15:17

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}] 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[],
xRange[]}, {PlotRange -> {xRange, yRange},
PlotStyle -> {Thick, Red},
Prolog -> {Inset[
SetAlphaChannel[img, .3], {xRange[], yRange[]}, bottomLeft,
scaling]}, AxesOrigin -> {0, -3}, AspectRatio -> aspectRatio,
Axes -> False, ImageSize -> 500, Axes -> True, ImagePadding -> 50,
PlotRangeClipping -> False, Frame -> True}] * Due to plot coordinate system, it is more convenient to use Plot[Log10[ ...]] instead of LogPlot[].