# Implicit plot with plots

**URL:** <https://discourse.julialang.org/t/implicit-plot-with-plots/6585>\
**Category:** New to Julia\
**Tags:** plotting, plots, implicit-equation\
**Created:** [October 20, 2017, 5:34pm UTC](https://discourse.julialang.org/t/implicit-plot-with-plots/6585 "2017-10-20T17:34:22Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![harven](https://avatars.discourse-cdn.com/v4/letter/h/3da27b/32.png) [@harven](https://discourse.julialang.org/u/harven)\
**Post date:** [October 20, 2017, 5:34pm UTC](https://discourse.julialang.org/t/implicit-plot-with-plots/6585/1 "2017-10-20T17:34:22Z")

</div>

How do I make an implicit plot with the plots package?

pyplot has a contour function which admits a fourth argument to plot a single level line.  
But it does not seem to be implemented in plots.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [October 20, 2017, 10:20pm UTC](https://discourse.julialang.org/t/implicit-plot-with-plots/6585/2 "2017-10-20T22:20:18Z")

</div>

There is a contour function in Plots. It’s called contour IIRC.

---

<div class="post-metadata">

**Author:** ![harven](https://avatars.discourse-cdn.com/v4/letter/h/3da27b/32.png) [@harven](https://discourse.julialang.org/u/harven)\
**Post date:** [October 21, 2017, 12:25pm UTC](https://discourse.julialang.org/t/implicit-plot-with-plots/6585/3 "2017-10-21T12:25:32Z")

</div>

Yes but it does not plot a single level line. Or am I missing something?

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [October 21, 2017, 12:32pm UTC](https://discourse.julialang.org/t/implicit-plot-with-plots/6585/4 "2017-10-21T12:32:31Z")

</div>

There’s lots of stuff in matplotlib that isn’t implemented in Plots yet. To know if your use case is, could you describe (or screenshot) what an implicit contour is?

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [October 21, 2017, 2:20pm UTC](https://discourse.julialang.org/t/implicit-plot-with-plots/6585/5 "2017-10-21T14:20:58Z")

</div>

The following works:

```julia
contour(-3:0.01:3, -3:0.01:3, (x,y) -> x^2 + 2y^2, levels=[2.0, 3.0])

```

But specifying only one level throws an `InexactError`.  
That seems to be a bug.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [October 21, 2017, 2:22pm UTC](https://discourse.julialang.org/t/implicit-plot-with-plots/6585/6 "2017-10-21T14:22:05Z")

</div>

As @mkborregaard said, we need to know exactly what you want to do. Could you please give the PyPlot command and output?

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [October 21, 2017, 2:23pm UTC](https://discourse.julialang.org/t/implicit-plot-with-plots/6585/7 "2017-10-21T14:23:08Z")

</div>

Hmm, that works in GR but Plotly ignores the `level` attribute.

---

<div class="post-metadata">

**Author:** ![harven](https://avatars.discourse-cdn.com/v4/letter/h/3da27b/32.png) [@harven](https://discourse.julialang.org/u/harven)\
**Post date:** [October 21, 2017, 7:33pm UTC](https://discourse.julialang.org/t/implicit-plot-with-plots/6585/8 "2017-10-21T19:33:25Z")

</div>

So for example if I want to plot the curve given by the implicit equation `x^2+y^2 = 1`, I can use the following python code.

```
   import numpy as np
   import matplotlib.pyplot as plt
   y,x=np.ogrid[-5:5:100j,-5:5:100j]
   plt.contour(x.ravel(),y.ravel(),x^2+y^2,[1])
   plt.show()

```

The result is a circle of radius 1 centered at the origin. Now using the contour function for an implicit plot is as far as I understand a hack.

I can see that sympy has now a proper plot\_implicit function that is documented (and live demonstrated) together with its source code [here](http://docs.sympy.org/latest/modules/plotting.html).

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [October 21, 2017, 10:51pm UTC](https://discourse.julialang.org/t/implicit-plot-with-plots/6585/9 "2017-10-21T22:51:57Z")

</div>

One can use [https://github.com/JuliaIntervals/IntervalConstraintProgramming.jl](https://github.com/JuliaIntervals/IntervalConstraintProgramming.jl) or [https://github.com/jverzani/ImplicitEquations.jl](https://github.com/jverzani/ImplicitEquations.jl) for this task. The latter is a bit slower, but has a slightly easier to use syntax:

```julia
using ImplicitEquations, Plots
f(x,y) = x^2 + y^2
plot(f ⩵ 1) # \Equal[tab]

```

The SymPy solution can be called from within SymPy.jl, but that would be PyPlot specific.

---

<div class="post-metadata">

**Author:** ![mforets](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mforets/32/298_2.png) [@mforets](https://discourse.julialang.org/u/mforets)\
**Post date:** [September 11, 2020, 5:57pm UTC](https://discourse.julialang.org/t/implicit-plot-with-plots/6585/10 "2020-09-11T17:57:36Z")

</div>

Here are two new examples that may be useful for some (tested in Julia v1.5):

### Using [ImplicitPlots.jl](https://github.com/saschatimme/ImplicitPlots.jl)

```julia
using ImplicitPlots, Plots

f(x,y) = (x^4 + y^4 - 1) * (x^2 + y^2 - 2) + x^5 * y

implicit_plot(f; xlims=(-2,2), ylims=(-2,2), lab="f(x) = 0")

```

 ![Screenshot from 2020-09-11 14-59-29](https://global.discourse-cdn.com/julialang/original/3X/8/d/8dac97b46037804d5cdd777a257ee0daf2e5173f.png)

### Using [IntervalConstraintProgramming.jl](https://github.com/JuliaIntervals/IntervalConstraintProgramming.jl):

```julia
using IntervalConstraintProgramming, IntervalArithmetic, Plots

dom = IntervalBox(-2 .. 2, -2 .. 2)
@function f(x, y) = (x^4 + y^4 - 1) * (x^2 + y^2 - 2) + x^5 * y
C = @constraint f(x, y) <= 0
p = pave(C, dom, 0.01)

plot(p.inner, lw=1, ratio=1, lab="f(x, y) ≤ 0", title="")
plot!(p.boundary, lw=1, ratio=1, lab="boundary")

```

 ![Screenshot from 2020-09-11 14-41-28](https://global.discourse-cdn.com/julialang/original/3X/b/5/b5d2565d4e354935231ece956cc2a7f602319962.png)

Using ModelingToolkit and a standard Julia function also works:

```julia
using IntervalConstraintProgramming, IntervalArithmetic, ModelingToolkit, Plots

dom = IntervalBox(-2..2, -2..2)
	
gg(x, y) = (x^4 + y^4 - 1) * (x^2 + y^2 - 2) + x^5 * y
	
vars = @variables x, y
	
C = Separator(vars, gg(x, y) < 0)
p = pave(C, dom, 0.05)
	
plot(p.inner, lw=0, ratio=1, lab="f(x, y) ≤ 0", title="")
plot!(p.boundary, lw=0, ratio=1, lab="boundary")

```

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [September 11, 2020, 6:12pm UTC](https://discourse.julialang.org/t/implicit-plot-with-plots/6585/11 "2020-09-11T18:12:24Z")

</div>

For completeness, there is also now the `MDBM` package, illustrated below with `Plots`:

```julia
f(x,y) = (x^4 + y^4 - 1) * (x^2 + y^2 - 2) + x^5 * y
xs, ys = -2:.25:2, -2:.25:2

using MDBM
m = MDBM_Problem(f, [xs, ys])
solve!(m, 4)
p = plot(xlim=extrema(xs), ylim=extrema(ys), legend=false)
mdt=MDBM.connect(m)
for i in 1:length(mdt)
    dt=mdt[i]
    P1=getinterpolatedsolution(m.ncubes[dt[1]], m)
    P2=getinterpolatedsolution(m.ncubes[dt[2]], m)
    plot!(p, [P1[1],P2[1]],[P1[2],P2[2]])
end
p

```

---

<div class="post-metadata">

**Author:** ![VivMendes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vivmendes/32/16477_2.png) [@VivMendes](https://discourse.julialang.org/u/VivMendes)\
**Post date:** [March 4, 2021, 11:08pm UTC](https://discourse.julialang.org/t/implicit-plot-with-plots/6585/12 "2021-03-04T23:08:11Z")

</div>

I have tried both ImplicitPlots.jl and ImplicitEquations.jl. There is one point that I am missing: how can I change the color of the lines? Apparently, the latter only accepts “black” (by default), while the former only accepts blue (by default). Sometimes, if we want to plot two implicit functions, it makes sense to have the possibility to change colors.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [March 4, 2021, 11:21pm UTC](https://discourse.julialang.org/t/implicit-plot-with-plots/6585/13 "2021-03-04T23:21:36Z")

</div>

You should just be able to use Plots.jl’s contour function.

---

<div class="post-metadata">

**Author:** ![VivMendes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vivmendes/32/16477_2.png) [@VivMendes](https://discourse.julialang.org/u/VivMendes)\
**Post date:** [March 4, 2021, 11:40pm UTC](https://discourse.julialang.org/t/implicit-plot-with-plots/6585/14 "2021-03-04T23:40:57Z")

</div>

@dpsanders thanks a lot. It works, it’s fast and versatile.

---

<div class="post-metadata">

**Author:** ![hisacro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hisacro/32/202631_2.png) [@hisacro](https://discourse.julialang.org/u/hisacro)\
**Post date:** [December 20, 2023, 12:28am UTC](https://discourse.julialang.org/t/implicit-plot-with-plots/6585/15 "2023-12-20T00:28:56Z")

</div>

sorry for reviving a old thread, but can you provide an example to use contour

I looked at [Contour Plots · Plots](https://docs.juliaplots.org/stable/series_types/contour/) it not evident how to incorporate implicitplot with this

```julia
using ImplicitEquations, plots

f(θ1,θ2) = (sin(θ1-θ2)+sin(θ1));
g(θ1,θ2) = sin(θ2-θ1)-sin(θ2);

plot(xlabel="θ1")
plot!(ylabel="θ2")
plot!(Eq(f,0.), xlim=(-pi,pi), ylim=(-pi,pi))
plot!(Eq(g, 0.), xlim=(-pi,pi), ylim=(-pi,pi))

```

how to plot 2 colored lines

---

<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:** [December 20, 2023, 4:21pm UTC](https://discourse.julialang.org/t/implicit-plot-with-plots/6585/16 "2023-12-20T16:21:28Z")

</div>

> [@hisacro](#):
>
> how to plot 2 colored lines

In your example, you can use `fillcolor` (`fc`):

```julia
plot!(Eq(f,0.), lim=(-π,π), fc=:red)
plot!(Eq(g, 0.), ratio=1, fc=:blue)

```

And to add ImplicitPlots.jl contours on top of previous plot:

```julia
using ImplicitPlots
h(x,y) = (x^4 + y^4 - 1) * (x^2 + y^2 - 2) + x^5 * y
implicit_plot!(h; lim=(-π,π), lc=:green)

```

---

<div class="post-metadata">

**Author:** ![hisacro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hisacro/32/202631_2.png) [@hisacro](https://discourse.julialang.org/u/hisacro)\
**Post date:** [December 20, 2023, 4:25pm UTC](https://discourse.julialang.org/t/implicit-plot-with-plots/6585/17 "2023-12-20T16:25:45Z")

</div>

> [@rafael.guerra](#):
>
> `fc=:red`
> 
> `lc=:green`

this is like magic I some know missed these in the documentation, mind knowing how you looked up!

both implicitplots as well as implicitequations methods work, Cheers ; )

---

<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:** [December 20, 2023, 4:29pm UTC](https://discourse.julialang.org/t/implicit-plot-with-plots/6585/18 "2023-12-20T16:29:28Z")

</div>

> [@hisacro](#):
>
> mind knowing how you looked up

Plots .jl [series attributes docs.](https://docs.juliaplots.org/latest/generated/attributes_series/)
