5

I'd like to recreate a plot that I found on a book for my students. The plot has a grid (1 mm x 1 mm) and above it, the axis and the function graph.

I'm having trouble creating the grid, which has some emphasis every 5 mm. I assume I have to use minor grid style and major grid style to set the colors for the grid and it's emphasis.

So, how can I set the grid to have 1 mm by 1 mm divisions? I'd like to point out that setting "ticks" give me little black lines on the axis, which I do not want, only the grid.

Here's my MWE:

\documentclass{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\usepackage{tikz,tikz-3dplot} %Para fazer desenhos
\usetikzlibrary{shapes.multipart,shapes.geometric,calc,angles,positioning,intersections,quotes,decorations,babel,patterns,fit}
\usepackage{tkz-euclide}
\usetkzobj{all}

\begin{document}
\begin{tikzpicture}

\begin{axis}    [
axis lines = {center},
%width = {0.6\linewidth},
ylabel = {$y$},
xlabel = {$x$},
ytick distance = {2},
xtick distance = 1,
ymin=-1,
ymax=11,
xmin=-0.9,
xmax=4.9,
major grid style={gray},
minor grid style={lightgray},
grid=both
]

\addplot    [
mark = none, domain= -1:5, smooth %samples at = {-1,0,1,2,3,4,5}
]
{x^2 -5*x + 6};

\addplot    [
mark=*
]
coordinates {(2.5,-0.25)};
\end{axis}
\end{tikzpicture}
\end{document}

And here's the image I'm trying to base myself on. I know the function in the MWE isn't the same as the image, but it's just a reference for the grid. My idea is to have some examples of functions and also some instances with only the axis and grid, so the students can build the plots: enter image description here

4 Answers 4

5

as pure tikz picture:

enter image description here

\documentclass[tikz, margin=3mm]{standalone}

\usetikzlibrary{arrows.meta}

\begin{document}
\begin{tikzpicture}
% grid
\draw[     thin,brown!25]   (-2,-2) grid[step= 1mm] ++ (8,15);
\draw[semithick,brown!50]   (-2,-2) grid[step= 5mm] ++ (8,15);
\draw[    thick,brown!75]   (-2,-2) grid[step=10mm] ++ (8,15);
% axis
\draw[-Stealth] (0,-1) -- (0,11) node[below right] {$y$};
\draw[-Stealth] (-1,0) -- (5, 0) node[below  left] {$x$};
\foreach \i in {1,...,4}{\draw (\i,1mm) -- + (0,-2mm) node[below] {\i};}
\foreach \i in {2,4,...,10}{\draw (1mm,\i) -- + (-2mm,0) node[ left] {\i};}
% curve
\draw[very thick, red]  plot[domain= -1:5] (\x, \x*\x - 5*\x + 6);
\draw                   plot[mark=*]    coordinates {(2.5,-0.25)};
\end{tikzpicture}
\end{document}
5

Here an answer with pgfplots, three grid levels and the ticks removed:

In order to get a proper mm grid, you have to set the x- and y-vector to a fixed value, thus disabling auto scaling (x=1cm,y=1cm). Here 1cm is needed for the major grid. The minor grid is set by defining the number of ticks between 2 major ticks using minor x tick num=9 and minor y tick num=9.

pgfplots only offers two grid levels, but a third can be drawn manually. This requires to enable layers in order to draw the middle grid below the axis (done with \pgfplotsset{set layers=standard}). The \draw command has then to be put in \begin{pgfonlayer}{axis grid} ... \end{pgfonlayer}. In order to make the middle grid work for graph paper without plots, two \foreach loops are used.

If you want only the graph paper, just leave out the plots (i.e. no \addplot).

The ticks can be removed with \pgfplotsset{every major tick/.style={draw=none}, every minor tick/.style={draw=none}}.

I also took the freedom to make the axes thicker.

Here the result:

enter image description here

The code:

\documentclass[border=1mm]{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
%\usepackage{tikz,tikz-3dplot} %Para fazer desenhos
\usetikzlibrary{shapes.multipart,shapes.geometric,calc,angles,positioning,intersections,quotes,decorations,babel,patterns,fit}
\usepackage{tkz-euclide}
\usetkzobj{all}

\begin{document}
\begin{tikzpicture}[%
    % style for middle grid
    middle grid style/.style={lightgray,line width=0.5pt}
]
\pgfplotsset{%
    % enable layer, needed to draw middle grid below axis
    set layers=standard,
    % disable ticks
    every major tick/.style={draw=none},
    every minor tick/.style={draw=none},
}
\begin{axis}    [
axis lines = {center},
% set fixed scale to get mm grid
% note: this is for the major grid
x=1cm,
y=1cm,
ylabel = {$y$},
xlabel = {$x$},
ytick distance = 1,
xtick distance = 1,
% number of minor ticks between 2 major ticks
minor x tick num=9,
minor y tick num=9,
ymin=-1,
ymax=11,
xmin=-0.9,
xmax=4.9,
major grid style={lightgray,thick},
minor grid style={lightgray,very thin},
grid=both,
% optional stuff
% change axis line width
axis line style={thick},
% make tick labels cover the grid
%ticklabel style={inner sep=1pt,fill=white},
]

% draw middle grid
\begin{pgfonlayer}{axis grid}
% works only for x=y=1cm with plots
%\draw[middle grid style,step=0.5cm]
%    (axis cs:\pgfkeysvalueof{/pgfplots/xmin},\pgfkeysvalueof{/pgfplots/ymin}) grid
%    (axis cs:\pgfkeysvalueof{/pgfplots/xmax},\pgfkeysvalueof{/pgfplots/ymax});
% to be used for graph paper with no plots (and if x != y)
%               v-- first, second and last x-positions for middle grid
\foreach \x in {-0.5,0.5,...,4.5}{
    % \edef-trick, see manual page 541
    \edef\temp{\noexpand\draw[middle grid style]
        (axis cs:\x,\pgfkeysvalueof{/pgfplots/ymin}) --
        (axis cs:\x,\pgfkeysvalueof{/pgfplots/ymax});}
    \temp
}
%               v-- first, second and last y-positions for middle grid
\foreach \y in {-0.5,0.5,...,10.5}{
    \edef\temp{\noexpand\draw[middle grid style]
        (axis cs:\pgfkeysvalueof{/pgfplots/xmin},\y) --
        (axis cs:\pgfkeysvalueof{/pgfplots/xmax},\y);}
    \temp
}
\end{pgfonlayer}

\addplot    [
mark = none, domain= -1:5, smooth %samples at = {-1,0,1,2,3,4,5}
]
{x^2 -5*x + 6};

\addplot    [
mark=*
]
coordinates {(2.5,-0.25)};
\end{axis}
\end{tikzpicture}
\end{document}
1

You're missing a few parameters to the axis environment:

\begin{axis}[
    axis lines = {center},
    %width = {0.6\linewidth},
    ylabel = {$y$},
    xlabel = {$x$},
    ytick distance = 2,
    xtick distance = 1,
    ymin=-1,
    ymax=11,
    xmin=-0.9,
    xmax=4.9,
    grid=both,
    % here's what I changed/added:
    grid style={line width=.1pt,draw=lightgray},
    major grid style={line width=.2pt,draw=gray},
    minor tick num=4,
    enlargelimits={abs=0.5},
    tick style={draw=none},
    axis line style={latex-latex},
    ticklabel style={font=\tiny,fill=white},
    xlabel style={at={(ticklabel* cs:1)},anchor=north west},
    ylabel style={at={(ticklabel* cs:1)},anchor=south west}
  ]

The result is now close enough to your book:

result

Notice that the units in your axes are differently sized; this is because of your tick distances.

0

An answer with tikz and its plot command (I found that is more flexible than pgfplot's: addplot) Be careful with last scale that changes the function. You can use yscale or xscale too. Also a name of a defined color in the plot command, may be usefull.

\documentclass[11pt]{article}
\usepackage{tikz}

\begin{document}

Grid (with grid command):

\noindent\begin{tikzpicture}
   \draw[step=1mm,color=orange,very thin] (-5 cm,-3cm) grid (5cm,3cm);
\end{tikzpicture}


Custom Grid (with foreach command):

\xdef\xmin{-5}
\xdef\xsec{-4.9}
\xdef\xmax{5.01}
\xdef\ymin{-3}
\xdef\ysec{-2.9}
\xdef\ymax{3.01}
\noindent\begin{tikzpicture}
   \foreach \x in {\xmin,\xsec,...,\xmax} {
        \draw [thin,orange] (\x,\ymin) -- (\x,\ymax);
    }
    \foreach \y in {\ymin,\ysec,...,\ymax} {
        \draw [very thin,orange] (\xmin,\y) -- (\xmax,\y);
    }
\end{tikzpicture}
\pagebreak


Grid+Axes (with grid command):

\noindent\begin{tikzpicture}
   \draw[step=1mm,color=orange,very thin] (-5 cm,-3cm) grid (5cm,3cm);
    \draw[-latex] (-4.5,0)--(4.5,0) node [below] {$x$};
    \draw[-latex] (0,-2.5)--(0,2.5) node[left] {$y$};
\end{tikzpicture}


Custom Grid+Axes (with foreach command):

\xdef\xmin{-5}
\xdef\xsec{-4.9}
\xdef\xmax{5.01}
\xdef\ymin{-3}
\xdef\ysec{-2.9}
\xdef\ymax{3.01}
\noindent\begin{tikzpicture}
   \foreach \x in {\xmin,\xsec,...,\xmax} {
        \draw [thin,orange] (\x,\ymin) -- (\x,\ymax);
    }
    \foreach \y in {\ymin,\ysec,...,\ymax} {
        \draw [very thin,orange] (\xmin,\y) -- (\xmax,\y);
    }
    \draw[-latex] (-4.5,0)--(4.5,0) node [below] {$x$};
    \draw[-latex] (0,-2.5)--(0,2.5) node[left] {$y$};
\end{tikzpicture}
\pagebreak

Grid+Axes+Plot (with grid command):

\noindent\begin{tikzpicture}
   \draw[step=1mm,color=orange,very thin] (-5 cm,-3cm) grid (5cm,3cm);
    \draw[-latex] (-4.5,0)--(4.5,0) node [below] {$x$};
    \draw[-latex] (0,-2.5)--(0,2.5) node[left] {$y$};
    \draw[domain=-2.1:2.1,variable=\x] plot ({\x},{\x*\x-2}); %y=x^2-2
\end{tikzpicture}


Custom Grid+Axes+Plot (with foreach command):

\xdef\xmin{-5}
\xdef\xsec{-4.9}
\xdef\xmax{5.01}
\xdef\ymin{-3}
\xdef\ysec{-2.9}
\xdef\ymax{3.01}
\noindent\begin{tikzpicture}
   \foreach \x in {\xmin,\xsec,...,\xmax} {
        \draw [thin,orange] (\x,\ymin) -- (\x,\ymax);
    }
    \foreach \y in {\ymin,\ysec,...,\ymax} {
        \draw [very thin,orange] (\xmin,\y) -- (\xmax,\y);
    }
    \draw[-latex] (-4.5,0)--(4.5,0) node [below] {$x$};
    \draw[-latex] (0,-2.5)--(0,2.5) node[left] {$y$};
    \draw[domain=-1:4,variable=\x,scale=0.5] plot ({\x},{\x*\x-3*\x+1}); %y=x^2-3x+1
\end{tikzpicture}

\end{document}

Output:

enter image description here

enter image description here

enter image description here

You must log in to answer this question.

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