2

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,
    ]
    \addplot3[domain=-3:3,
    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?

2 Answers 2

3

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

\documentclass[border=5mm]{standalone}
\usepackage{asymptote}
\begin{document}
\begin{asy}
import math;  // for grids
import contour;
unitsize(1cm);
// grid and subgrid
add(shift(-3,-3)*scale(.2)*grid(30,30,.1gray+.9white));
add(shift(-3,-3)*grid(6,6,.5gray+.5white));

// 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.

enter image description here

// 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));
5
  • do you need to make the tick labels manually? Jan 10, 2021 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, 2021 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, 2021 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, 2021 at 15:36
  • 1
    +1 @Faceb Faceb: Even if you can not use the solution, you should upvote to show appreciation. Feb 23 at 19:10
0

You are creating a 3D plot and rotate it, so it looks like a 2D plot. There are no grid for 3D plots in PGFPlots. You can add it manually like this:

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18} 
\begin{document}
\begin{tikzpicture}
\begin{axis}[
title={$x^2-x\,y$},
axis lines=middle, view={0}{90},
xmin=-3.6, xmax=3.6,
ymin=-2.8, ymax=2.8,
xlabel=$x$, ylabel=$y$,
minor tick num=4,
small,
]
\addplot3[mesh, gray!10, domain=-3.6:3.6, samples=37, domain y=-2.8:2.8, samples y=29, forget plot] {0};
\addplot3[mesh, gray!50, domain=-4:4, samples=9, domain y=-3:3, samples y=7, forget plot] {0};
\addplot3[
domain=-3.2:3.2, samples=50,
domain y=-3:3, samples y=50,
contour gnuplot={levels={-1,1},labels=false},
thick,
] {x^2-x*y};
\end{axis}
\end{tikzpicture}
\end{document}

Graph with red and blue curves and a grid

Edit: There is actually a kind of 3D grid. The option 3d box=complete draws a full grid on the sides of the 3D box. -It is incompatible with axis lines=middle.

The same plot can be made with 3d box=complete and two axis like this:

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18} 
\begin{document}
\begin{tikzpicture}
\begin{axis}[
3d box=complete,
title={$x^2-x\,y$},
view={0}{90},
xmin=-3.6, xmax=3.6,
ymin=-2.8, ymax=2.8,
xlabel=$x$, ylabel=$y$,
grid=both,
minor tick num=4,
grid style = {line width = .1pt, draw = gray!10},
major grid style = {line width = .2pt, draw = gray!50},
axis line style={draw=none},
tick style={draw=none},
]
\end{axis}
\begin{axis}[
axis lines=middle,
view={0}{90},
xmin=-3.6, xmax=3.6,
ymin=-2.8, ymax=2.8,
]
\addplot3[
domain=-3.2:3.2, samples=50,
domain y=-3:3, samples y=50,
contour gnuplot={levels={-1,1},labels=false},
minor tick num=4,
thick,
] {x^2-x*y};
\end{axis}
\end{tikzpicture}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .