# Mark the Intersection Points on a Plot Graph

**URL:** https://discourse.julialang.org/t/mark-the-intersection-points-on-a-plot-graph/90828
**Category:** New to Julia
**Tags:** plotting, plot
**Created:** [November 25, 2022, 8:22pm UTC](https://discourse.julialang.org/t/mark-the-intersection-points-on-a-plot-graph/90828 "2022-11-25T20:22:34Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![wrisurcuriosity](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wrisurcuriosity/32/34597_2.png) [@wrisurcuriosity](https://discourse.julialang.org/u/wrisurcuriosity)
#### Post date: [November 25, 2022, 8:22pm UTC](https://discourse.julialang.org/t/mark-the-intersection-points-on-a-plot-graph/90828/1 "2022-11-25T20:22:34Z")

</div>

Hi guys,

I am struggling to point the intersection of two graphs. I am currently working on time complexity on sorting algorithms in insertion and merge. My data sets for the graphs will follow like:

```julia
plot(elements,times , label = "insertion sort" , background_color=RGB(40/255, 0/255, 60/255))
plot!(elements2,times2 , label = "merge sort", background_color=RGB(40/255, 0/255, 60/255))

```

Output graph is like:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/e/8eb58c9cc74180fae5ed6cfe77b69f689ab6f378.png)

I am trying to show something like this:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/4/74a036392385a320a8d0a282150587d7162547df.png)

How can I mark the intersection point ?

The Best, WR

---

<div class="post-metadata">

### Author: ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)
#### Post date: [November 25, 2022, 8:31pm UTC](https://discourse.julialang.org/t/mark-the-intersection-points-on-a-plot-graph/90828/2 "2022-11-25T20:31:02Z")

</div>

Assuming the data for `elements` and `elements2` are different, I would make a low-order polynomial model of each curve, based on data spanning the intersection and using the `Polynomials` package, subtract the two polynomials, and then find the root of the difference polynomial using `NLsolve` or `PolynomialRoots`.

EDIT: I mistakenly treated the wrong variable as the independent variable. Fixed that.

---

<div class="post-metadata">

### Author: ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)
#### Post date: [November 25, 2022, 8:40pm UTC](https://discourse.julialang.org/t/mark-the-intersection-points-on-a-plot-graph/90828/3 "2022-11-25T20:40:53Z")

</div>

But since the data is from simulation, you should be able to do timings at the same `elements` values. Then you could subtract the discrete data, looking for a sign change in the difference, model the difference with a polynomial over a few data points with the sign change near the center, and then solve for a root of the difference polynomial. That would be easier to automate.

---

<div class="post-metadata">

### Author: ![wrisurcuriosity](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wrisurcuriosity/32/34597_2.png) [@wrisurcuriosity](https://discourse.julialang.org/u/wrisurcuriosity)
#### Post date: [November 25, 2022, 10:09pm UTC](https://discourse.julialang.org/t/mark-the-intersection-points-on-a-plot-graph/90828/4 "2022-11-25T22:09:17Z")

</div>

> [@John\_Gibson](#):
>
> `NLsolve`

The answer is too theoretical for a newbie like me 🙂 I am looking for an example code for any similar given issue. Thank you for your contribution.

---

<div class="post-metadata">

### Author: ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)
#### Post date: [November 25, 2022, 10:14pm UTC](https://discourse.julialang.org/t/mark-the-intersection-points-on-a-plot-graph/90828/5 "2022-11-25T22:14:29Z")

</div>

Ok, I’ll give you some example code later tonight, after kids are in bed!

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [November 26, 2022, 12:18am UTC](https://discourse.julialang.org/t/mark-the-intersection-points-on-a-plot-graph/90828/6 "2022-11-26T00:18:36Z")

</div>

Here is some possible code to achieve this goal:

```julia
using Interpolations, Optim # install packages if needed

y1 = interpolate((elements,), times, Gridded(Linear()))
y2 = interpolate((elements2,), times2, Gridded(Linear()))

domain = intersect(
  range(extrema(elements)...),
  range(extrema(elements2)...)
)
x = optimize(x -> abs(y1(x)-y2(x)), 
             first(domain), last(domain)).minimizer
y = (y1(x)+y2(x))/2

# now (x,y) is the intersection point

```

Main idea:

1. Interpolate both series into continuous functions.
2. find the minimum absolute distance, which should be intersection.

Problem:

1. Interpolation might need to be improved (many methods to interpolate/smooth functions)
2. Functions may not be have single intersection which would complicate things.

Hope this helps, and @John_Gibson is welcome to add some stuff when the house is quiet!

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [November 26, 2022, 6:10am UTC](https://discourse.julialang.org/t/mark-the-intersection-points-on-a-plot-graph/90828/7 "2022-11-26T06:10:37Z")

</div>

> [@wrisurcuriosity](#):
>
> I am looking for an example code for any similar given issue.

Fyi, this problem was [posted before](https://discourse.julialang.org/t/find-intersection-between-curves-in-julia/70494/4) in Discourse, with a simple solution using splines.

---

<div class="post-metadata">

### Author: ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)
#### Post date: [November 26, 2022, 3:00pm UTC](https://discourse.julialang.org/t/mark-the-intersection-points-on-a-plot-graph/90828/8 "2022-11-26T15:00:01Z")

</div>

Some example code for computing the intersection of curves from discrete data points using polynomial fits.

This code blocks creates some artificial data.

```julia
using Polynomials, Plots

# two functions that intersect on an interval (a,b)
f1(x) = 2x + 12
f2(x) = x^2

(a,b) = (2,7) # the interval of interest
n = 10 # number of data points
ynoise = 0.3 # noise level, absolute

# create some noisy data from those two functions 
x = range(a, b, length=n)
y1 = f1.(x) + ynoise*randn(n)
y2 = f2.(x) + ynoise*randn(n)

```

This code block computes polynomial fits, finds the intersection point as a root of the difference polynomial, and plots the results To see how it works and the types it generates, you might want to step through it line by line at the REPL.

```julia
y1poly = fit(x,y1, 3) # make 3rd order polynomial fit to y1 data
y2poly = fit(x,y2, 3) # same for y2
ydiffpoly = y2poly - y1poly # compute difference of y2,y1 polynomials

xroots = roots(ydiffpoly) # find roots of difference polynomial 
j = findall(z -> a ≤ z ≤ b, xroots) # find the indices of the roots with interval (a,b)
xr = xroots[j[1]] # select the first root in the interval
yr = y1poly(xr) # compute y value at intersection point

scatter(x, y1, label="y1 datapoints", mc=:blue)
scatter!(x, y2, label="y2 datapoints", mc=:green)

plot!(x, y1poly.(x), label="y1 polynomial fit", lc=:blue)
plot!(x, y2poly.(x), label="y2 polynomial fit", lc=:green)
plot!(x, ydiffpoly.(x), label="diff polynomial", lc=:red)

scatter!(x, y1, label="", mc=:blue)
scatter!(x, y2, label="", mc=:green)

scatter!([xr], [ydiffpoly(xr)], label="root of difference", mc=:black)
scatter!([xr], [yr], label="intersection", mc=:white)
plot!(xl="x", yl="y", legend=:topleft)
plot!(title="intersection of discrete data via polynomial fit")

```

![intersection](https://global.discourse-cdn.com/julialang/original/3X/f/8/f8587f6ad428893164b54703f2b2645f32ac46cc.png)

---

<div class="post-metadata">

### Author: ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)
#### Post date: [November 26, 2022, 4:15pm UTC](https://discourse.julialang.org/t/mark-the-intersection-points-on-a-plot-graph/90828/9 "2022-11-26T16:15:24Z")

</div>

Or, with GMT (intersection point is computed from linear interpolation, I think)

```julia
using GMT

f1(x) = 2x + 12;
f2(x) = x^2;

x = range(2, 7, length=10);

y1 = f1.(x);
y2 = f2.(x);

int = gmtspatial(([x y1],[x y2]), intersections=:e);

plot([x y1], limits=(2,7,0,50), marker=:circ, mc=:blue, ms="6p", lw=0.5, lc=:blue)
plot!([x y2], marker=:circ, mc=:green, ms="6p", lw=0.5, lc=:green)
plot!(int, marker=:circ, mc=:red, ms="8p", show=true)

```

 ![GMTjl_tmp](https://global.discourse-cdn.com/julialang/original/3X/7/e/7e7c1a68d5286e69ddb29a1953a8ed4d55bc6d71.png)

---

<div class="post-metadata">

### Author: ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)
#### Post date: [November 26, 2022, 5:55pm UTC](https://discourse.julialang.org/t/mark-the-intersection-points-on-a-plot-graph/90828/10 "2022-11-26T17:55:55Z")

</div>

Nice! Though in all honestly I feel the acronym package name GMT hinders discoverability. The name GeneralizedMappingTools.jl would be better for the general public.

EDIT: I see now that GMT.jl is a wrapper for the GMT C library, so the acronym name follows the Julia naming standards.

---

<div class="post-metadata">

### Author: ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)
#### Post date: [November 26, 2022, 6:11pm UTC](https://discourse.julialang.org/t/mark-the-intersection-points-on-a-plot-graph/90828/11 "2022-11-26T18:11:41Z")

</div>

Yes, and we also have a

- GMT/MEX, for Matlab
- pyGMT, for Python
- GMT.jl for Julia

Not to mention that `GMT` is a _brand_ with \> 30 years, very well known in Geophysics.
