Implicit plot with plots

plotting

#1

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.


Contour plot of density function
#2

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


#3

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


#4

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?


#5

The following works:

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.


#6

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


#7

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


#8

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.

Thanks for your help!


#9

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

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.