0
votes

I am trying to overlay a basic plot onto an image. It is not clear based on the Julia Docs: http://docs.juliaplots.org/latest/examples/pyplot/

How can I plot on an image? Here is my code in Julia:

using Plots

x_coordinate_holder = [30515.4, 30515.4, 30490.9, 30470.7, 30470.7, 30450.9, 30450.0,
30450.0, 30450.0, 30450.0, 30450.0, 30430.8, 30449.2, 30469.4,
30469.4, 30469.4, 30489.2, 30489.2, 30489.2, 30509.4, 30529.1,
30549.4, 30569.2, 30589.3, 30590.0, 30609.2, 30610.0, 30610.0,
30590.7, 30590.7, 30590.7, 30570.8, 30570.0, 30570.0, 30589.4,
30589.4, 30589.4, 30590.0, 30570.6, 30550.8, 30569.2, 30569.2,
30589.4, 30589.4, 30570.6, 30570.6, 30570.6, 30570.6, 30550.8,
30530.6, 30510.8, 30510.0, 30510.0, 30529.3, 30549.1, 30549.1,
30569.3, 30570.0, 30589.3, 30590.0, 30590.0, 30590.0, 30609.3,
30610.0, 30610.0, 30610.0, 30629.2, 30649.4, 30669.2, 30689.4,
30709.2, 30710.0, 30710.0]
y_coordinate_holder = [14186.3, 14186.3, 14209.2, 14229.3, 14229.3, 14249.1, 14269.3,
14269.3, 14269.3, 14269.3, 14269.3, 14289.1, 14290.0, 14290.0,
14290.0, 14290.0, 14309.2, 14309.2, 14309.2, 14329.4, 14330.0,
14349.4, 14369.2, 14389.3, 14370.5, 14350.8, 14330.6, 14330.6,
14330.0, 14330.0, 14330.0, 14349.2, 14330.8, 14330.8, 14310.6,
14310.6, 14310.6, 14290.8, 14270.6, 14270.0, 14270.0, 14270.0,
14270.0, 14270.0, 14270.0, 14270.0, 14270.0, 14270.0, 14270.0,
14250.6, 14250.0, 14230.7, 14249.1, 14230.7, 14210.9, 14210.9,
14190.7, 14209.1, 14190.7, 14209.5, 14209.5, 14209.5, 14210.0,
14229.3, 14210.9, 14190.7, 14209.2, 14210.0, 14190.8, 14190.0,
14190.0, 14209.3, 14209.3]

plotly()
plot(x_coordinate_holder, y_coordinate_holder, color = :blue, linewidth=2)
gui() 

Note: I looked at How plot on Images with Plots.jl?

This example doesn't work anymore which is why I am asking the question again. Here is my updated code.

plotly()
img = Image("/path/to/image/file/exmaple/test.png)

plot(img)
plot!(x_coordinate_holder, y_coordinate_holder, color = :blue, linewidth=2)
gui() #Should open up a browser window/tab and show the plot in there.

When running this I get the following error:

* **ERROR: LoadError: UndefVarError: Image not defined** *

Can anyone provide a complete example?

1
Possible duplicate of How plot on Images with Plots.jl?hckr
All you need to do install Images package and see here: stackoverflow.com/questions/41704558/… It should work with plotly backend as well, albeit a bit slowly.hckr
That solution does not work. Either due to a syntax change in Julia or some other reason, I need to have a fresh set of eyes on this question.logankilpatrick
Have you installed Image package? I have tried the solution there before flagging duplicate. Also make sure you import the package using Images.hckr
Yes, Images is installed. I also have using Images.logankilpatrick

1 Answers

1
votes

Here is the code to plot an image with a plot on it, and have it auto scale:

using Plots 
using Images
img = load("/Users/xxxx/xxxx/xxxx-xxxx.png")

xMin = minimum(x_coordinate_holder)-30
xMax = maximum(x_coordinate_holder)+30
yMin = minimum(y_coordinate_holder)-30
yMax = maximum(y_coordinate_holder)+30

#print("X-Coords: ", xMin, ", ", xMax, " Y-Coords: ", yMin, ", ", yMax, "\n")

plot1 = plot(img, xlim=(xMin,xMax), ylim=(yMin, yMax), yflip = false)
plot1 = plot!(x_coordinate_holder, y_coordinate_holder, color = :black, linewidth=0.4)
plot2 = plot(e_holder, color = :orange, linewidth=2)
plot(plot1, plot2)
gui()

Note that xlim and ylim allow the image I am plotting on to scale proportionally with the size of the plot(i.e. if the plot is larger, it auto scales the image to show the full plot).

Note that x_coordinate_holder, y_coordinate_holder, and e_holder are just arrays defined before I run this code.

This code makes plot1 and plot2 show up next to one another.

It is also worth mentioning that I tried to do this using plotly and was unsuccessful. I had it working without proper image scaling, but when I tried to scale the image it no longer worked so I went to the default backend.