# implicit_plot doubled straight line

How come this plotted straight line is doubled?

x, y = var('x,y')
show(implicit_plot((y-x-1/2)^2,(x,-5,5),(y,-5,5), color='red'),gridlines=True)


I am looking for the zeros of the polynomial (y-x-1/2)^2. Obviously this has the same zeros as (y-x-1/2). Somehow the plot draws two parallel straight lines.

My attempt to display a plot here failed and I cannot upload any files, yet, due to a lack of points on this forum.

edit retag close merge delete

from contour_plot??

# When there's only one level (say, zero), matplotlib doesn't
# handle it well. If all of the data lie on one side of that
# level -- for example, if f(x,y) >= 0 for all x,y -- then it
# will fail to plot the points where f(x,y) == 0. This is
# especially catastrophic for implicit_plot(), which tries to
# do just that. Here we handle that special case: if there's
# only one level, and if all of the data lie on one side of
# it, we perturb the data a bit so that they don't. The resulting
# plots don't look great, but they're not empty, which is an
# improvement.

( 2022-11-19 21:20:41 +0100 )edit

Ugly workaround

x, y = var('x,y')
contour_plot((y-x-1/2)^2,(x,-5,5),(y,-5,5),
fill=False,contours=[0.001],plot_points=200)

( 2022-11-19 21:23:49 +0100 )edit

Sort by » oldest newest most voted

Using implicit_plot "raw" (i. e. without wrapping in show) allows you to see the warning emitted by this function :

sage: implicit_plot((y-x-1/2)^2,(x,-5,5),(y,-5,5), color='red')
/usr/local/sage-9/src/sage/plot/contour_plot.py:988: UserWarning: pathological contour plot of a function whose values all lie on one side of the sole contour; we are adding more plot points and perturbing your function values.
warn("pathological contour plot of a function whose "
Launched png viewer for Graphics object consisting of 1 graphics primitive


wich, IMNSHO, describes exactly what happens.

Since your function is polynomial, it is tempting to plot the ideal that your polynomial defines. However, since the same implicit_plot is ultimately used, the same problem happens :

sage: R1.<u, v>=QQ[]
sage: R1.ideal((v-u-1/2)^2).plot()
/usr/local/sage-9/src/sage/plot/contour_plot.py:988: UserWarning: pathological contour plot of a function whose values all lie on one side of the sole contour; we are adding more plot points and perturbing your function values.
warn("pathological contour plot of a function whose "
Launched png viewer for Graphics object consisting of 1 graphics primitive


Here, a bit of reflection helps. as long as $x\in\mathbb{R}$, $x^2=0\Rightarrow{}x=0$. This is also true if you substitute $y-x-\frac{1}{2}$ for $x$. Therefore,

sage: implicit_plot(y-x-1/2, (x, -5, 5), (y, -5, 5))
Launched png viewer for Graphics object consisting of 1 graphics primitive


suffices.

HTH,

more