# How to draw grid lines with contour gnuplot?

This is a follow up question of Tick labels not showing when using contour gnuplot and axis line = middle. The difference is that I added the grid lines in the code. However it is not drawing it.

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.16}
\begin{document}
\begin{tikzpicture} \begin{axis}[
axis lines = middle,
title={$x^2-x\,y$},
enlarge x limits,
view={0}{90},
xlabel=$x$, ylabel=$y$,
small,
% grid
grid = both,
grid style = {line width = .1pt, draw = gray!10},
major grid style = {line width = .2pt, draw = gray!50},
ticks = both,
minor tick num = 4,
]
domain y=-3:3,
contour gnuplot={levels={-1,1},labels=false},
thick,samples=50,samples y=50,
] {x^2-x*y};
\end{axis}
\end{tikzpicture}
\end{document}


I tried to move the order of the lines, but it doesn't solve it. What can I do to draw the grid lines?

How about a plain Asymptote solution? PS: I am sure there is a pgfplots way if you look at its 571-page documentation carefully enough.

\documentclass[border=5mm]{standalone}
\usepackage{asymptote}
\begin{document}
\begin{asy}
import math;  // for grids
import contour;
unitsize(1cm);
// grid and subgrid

// axes, dashed line and labels
draw(Label("$x$",EndPoint,align=SW,Fill(white)),(-3,0)--(3,0));
draw(Label("$y$",EndPoint,align=SE,Fill(white)),(0,-3)--(0,3));
draw((1,0)--(1,2)--(0,2)^^(-1,0)--(-1,-2)--(0,-2),dashed);
label("$1$",(1,0),SE); label("$-1$",(-1,0),NW);
label("$2$",(0,2),W); label("$-2$",(0,-2),NW);

// plotting graph of implicit function
real f(real x, real y){return x^2-x*y;}
pair A=(-3,-3), B=(3,3);
real[] c1={1}, c2={-1}, c={0};
draw(contour(f,A,B,c,300),purple); // 2 asymptote straight lines
draw(contour(f,A,B,c1),blue);
draw(contour(f,A,B,c2),red);
label("The graph of $x^2-xy=C$",truepoint(S)+(0,-.5));

\end{asy}
\end{document}


Update For "auto ticks", I add the grid and subgrid using Step=1, step=.2 in LeftTicks, RightTicks of the xaxis and yaxis command. The module graph must be loaded. Compiling time seems a bit slower.

// http://asymptote.ualberta.ca/
unitsize(1cm);
import graph;
import contour;

real f(real x, real y){return x^2-x*y;}
pair A=(-3,-3), B=(3,3);
real[] c1={1}, c2={-1};
draw(A--B^^(0,A.y)--(0,B.y),purple); // 2 asymptote straight lines
draw(contour(f,A,B,c1),cyan+linewidth(1pt));
draw(contour(f,A,B,c2),magenta+linewidth(1pt));

pen thin=gray+linewidth(.2pt);
pen verythin=lightgray+linewidth(.2pt);
xaxis("$x$",BottomTop,LeftTicks(begin=false,end=false,Step=1,step=.2,extend=true, ptick=verythin,pTick=thin));
yaxis("$y$",LeftRight,RightTicks(begin=false,end=false,Step=1,step=.2,extend=true,ptick=verythin,pTick=thin));

label("The graph of $x^2-xy=C$",truepoint(N)+(0,.5));

• do you need to make the tick labels manually? Jan 10 at 19:50
• @FacebFaceb I like to make tick manually, to get exactly what I like to tick and label. Of course, auto ticks and labels can be made using import graph; see here asymptote.sourceforge.io/FAQ/section6.html if you are interested. (I though you are still waiting a pgfplots solution) Jan 10 at 19:58
• yes I am waiting for a solution with contour gnuplot and pgfplots, but I was just curious about this package Jan 10 at 20:21
• @FacebFaceb this may be helpful for your pgfplots figure (grids) latexdraw.com/linear-regression-in-latex-using-tikz Jan 20 at 15:36