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.

    \begin{tikzpicture} \begin{axis}[
    axis lines = middle,
    enlarge x limits,
    xlabel=$x$, ylabel=$y$,
    % 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};

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.

enter image description here

import math;  // for grids
import contour;
// grid and subgrid

// axes, dashed line and labels
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
label("The graph of $x^2-xy=C$",truepoint(S)+(0,-.5));


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.

enter image description here

// http://asymptote.ualberta.ca/
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

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));

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)
    – Black Mild
    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
    – Black Mild
    Jan 20 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.