# Intersection of two plots

**URL:** <https://discourse.julialang.org/t/intersection-of-two-plots/98687>\
**Category:** General Usage\
**Tags:** question, plotting\
**Created:** [May 11, 2023, 3:40pm UTC](https://discourse.julialang.org/t/intersection-of-two-plots/98687 "2023-05-11T15:40:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Desmond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/desmond/32/47964_2.png) [@Desmond](https://discourse.julialang.org/u/Desmond)\
**Post date:** [May 11, 2023, 3:40pm UTC](https://discourse.julialang.org/t/intersection-of-two-plots/98687/1 "2023-05-11T15:40:49Z")

</div>

I have two lines on a plot and I want Julia to find the intersection between the two lines but I can’t seem to find a package that can help with that.

 ![myplot](https://global.discourse-cdn.com/julialang/original/3X/1/9/1968d9872800e455036a59924c2368ccfae7fcbb.png)

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 11, 2023, 3:44pm UTC](https://discourse.julialang.org/t/intersection-of-two-plots/98687/2 "2023-05-11T15:44:14Z")

</div>

see

> [@Mark the Intersection Points on a Plot Graph](https://discourse.julialang.org/t/mark-the-intersection-points-on-a-plot-graph/90828/8):
>
> Some example code for computing the intersection of curves from discrete data points using polynomial fits. This code blocks creates some artificial data. 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 …

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 11, 2023, 3:45pm UTC](https://discourse.julialang.org/t/intersection-of-two-plots/98687/3 "2023-05-11T15:45:35Z")

</div>

or even:

> [@Find intersection between curves in julia](https://discourse.julialang.org/t/find-intersection-between-curves-in-julia/70494):
>
> Hello, I have two curves with the values: col\_y = [8.02511788606e7, 9.98481296252e7, 2.328332483329e8, 6.148181496526e8, 9.453171952624e8, 1.2331292887706e9, 1.4394296796998e9, 1.460066157955e9, 1.3570403824485e9, 1.4269447174002e9] vin\_y = [1.434628836798e8, 1.318375623746e8, 1.108524843855e8, 6.91176196241e7, 4.19137382458e7, 1.48676444645e7, 1.00659726053e7, 7.8734512643e6] Is it possible to find the intersection x between these two curves? what is the approach in julia? Thank you

---

<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:** [May 11, 2023, 3:46pm UTC](https://discourse.julialang.org/t/intersection-of-two-plots/98687/4 "2023-05-11T15:46:30Z")

</div>

The GMT `fill_between` function uses the `gmtspatial` module to do that.

[https://www.generic-mapping-tools.org/GMTjl\_doc/examples/plotting\_functions/02\_1filledlines/#fill\_between](https://www.generic-mapping-tools.org/GMTjl_doc/examples/plotting_functions/02_1filledlines/#fill_between)

---

<div class="post-metadata">

**Author:** ![math4mad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math4mad/32/49486_2.png) [@math4mad](https://discourse.julialang.org/u/math4mad)\
**Post date:** [May 12, 2023, 5:38pm UTC](https://discourse.julialang.org/t/intersection-of-two-plots/98687/5 "2023-05-12T17:38:07Z")

</div>

## Idea

> basically, if we don’t have explicit function expression, the curve is a set of data  
> simplify way is link two points to segment ,then check whether two segment intersect  
> this is naive method.  
> But I thought this is not a simple question, for numerical method, neighbors are problem

## step

- package Meshes.jl for Segment method `Intersection`

```julia
using Meshes
s1 = Segment((0.0,0.0), (1.0,0.0))
s2 = Segment((0.5,0.0), (2.0,0.0))
inst=s1 ∩ s2

```

`typeof(inst)==Point2`

if segment does’nt intersect, then return nothing

- make data

- iterate to find intersection point

* * *

code

* * *

```julia-auto
using Meshes

step = 100
xspan = range(-2, 2, step)
arr = []
d1 = [(x, x^2) for x in xspan] .|> (d -> (round(d[1], digits=4), round(d[2], digits=4)))
d2 = [(x, 2 * x) for x in xspan] .|> (d -> (round(d[1], digits=4), round(d[2], digits=4)))

for i in 1:step-1

    local s1 = Segment(d1[i], d1[i+1])
    local s2 = Segment(d2[i], d2[i+1])
    local bool = s1 ∩ s2 |> d -> (typeof(d) == Point2)
    (bool == true) && push!(arr, res)
end

arr

```

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