12

I want to replicate some graphs that I drew by hand on millimeter paper. Here is a picture:

enter image description here

My current code is as follows (I didn't worry about the data yet):

\documentclass{report}

\usepackage{pgfplots}
\usepackage{siunitx}

\begin{document}

\begin{tikzpicture}[scale = 2]
            \begin{axis}[
                axis lines = middle,
                xlabel = t\lbrack\si{\second}\rbrack, 
                ylabel = x\lbrack\si{\metre}\rbrack, 
                x label style = {anchor = north west, font = \scriptsize},
                y label style = {anchor = north east, font = \scriptsize, shift={(0,0.5cm)}},
                xtick = {0,10,...,20},
                minor xtick = {1,2,3,4,5,6,7,8,9,11,12,13,14,15,16,17,18,19},
                ytick = {0,10,...,20},
                minor ytick = {1,2,3,4,5,6,7,8,9,11,12,13,14,15,16,17,18,19},
                tick style = {line width = 0.3pt},
                ticklabel style = {font = \tiny, fill = white},
                axis line style = {-latex},
                grid = both,
                ymin = 0, 
                ymax = 21, 
                xmin = 0, 
                xmax = 21,
                ]
                \addplot[
                only marks,
                mark size = 2,
                black, 
                error bars/.cd, 
                y dir = both, 
                x dir = both, 
                y explicit, 
                x explicit
                ]
                %table[x= temp, y = temp, x error = temp, y error = temo]
                %{\datafile};
                coordinates {(6.4,7.2) +-(1,0.5)};
            \end{axis}
        \end{tikzpicture}
\end{document}

(Note that I used dummy values, coordinates, sc and variables; they aren't important right now).

By setting the major and minor grid style I was able to get thick lines on my labeled coordinates and thin lines in between:

\pgfplotsset{minor grid style = {line width = 0.1pt}}
\pgfplotsset{major grid style = {line width = 0.4pt}}

But this is where I get stuck. There are several things that I'm not able to do by going this route:

  1. I need major grid lines every centimeter, independent of the label.

  2. On millimeter paper there are 3 grids; besides the major one with 1cm spacing and the minor one with 1mm spacing, there is a third one every 5mm (with a thickness somewhere between the 2 grids). Using pgfplots I am only able to edit the major and minor grid styles, but I can't seem to find a way to add further grids to a certain graph.

  3. Display the plot in "landscape mode" so that it takes up the whole page, as shown in the picture.

I'd appreciate it if somebody could help me out with those issues.

Thanks!

Timm
  • 803
Skydiver
  • 977
  • 2
    @those that are eager to close: Is this really a duplicate? It is nontrivial to use the tikz answer for pgfplots. – gernot Mar 05 '17 at 14:56
  • 2
    I neither consider this as a duplicate, since using the pgfplots environment also directly allows to draw the functions shown in the image, which would require significant work to do in pure tikz. – Timm Mar 05 '17 at 15:13

2 Answers2

10

The following example works for me so far

\documentclass{report}

\usepackage{pgfplots}
\usepackage{siunitx}

\begin{document}

% http://tex.stackexchange.com/a/15002/100384
\newcommand*{\getlength}[2]{%
   % Convert to `cm` and round to two fractional digits:
   \pgfmathsetmacro#1{round(0.0351459804*#2)}%
}

\rotatebox{90}{
    \begin{tikzpicture}
        % a factor for measures, since pfplots produces slightly bigger images than defined
        %   in width/height. This can be used to adjust that problem manually
        \pgfmathsetmacro{\factor}{0.9}

        % we will need some length in `cm`, without units, and rounded
        \getlength{\yMax}{\factor*\textwidth}
        \getlength{\xMax}{\factor*\textheight}

        \begin{axis}[
            set layers,
            width=\factor\textheight, height=\factor\textwidth,
            x=1cm, y=1cm,
            axis lines = middle,
            xlabel = {t\lbrack\si{\second}\rbrack}, 
            ylabel = {x\lbrack\si{\metre}\rbrack}, 
            x label style = {anchor = north west, font = \scriptsize},
            y label style = {anchor = north east, font = \scriptsize, shift={(0,0.5cm)}},
            xtick={0,1,...,\xMax},
            ytick={0,1,...,\yMax},
            minor xtick={0,0.1,0.2,...,\xMax},
            minor ytick={0,0.1,0.2,...,\yMax},
            tick style = {line width = 0.6pt, black!40!white},
            minor tick style = {line width = 0.3pt, black!40!white},
            ticklabel style = {font=\tiny, fill=white},
            axis line style = {-latex},
            grid = both,
            minor grid style={line width=0.3pt, black!40!white},
            grid style={line width=0.6pt, black!40!white},
            ymin=0, ymax=\yMax,
            xmin=0, xmax=\xMax,
            ]
            \addplot[only marks] coordinates {(6.4,7.2) +-(1,0.5)};

            % now manually draw the 0.5cm grid on the grid layer
            \begin{pgfonlayer}{axis grid}
                \draw[line width=0.45pt, step=0.5cm, black!40!white] (axis cs: 0, 0) grid (axis cs: \xMax, \yMax);
            \end{pgfonlayer}
        \end{axis}
    \end{tikzpicture}
}

\end{document}

Notes:

  • Do not use any scale, rotate or similar in the tikzpicture or axis environment. It will ruin everything. I tried. The rotatebox works fine, however.
  • To get the dimensions correct, you can force pgfplots to use a system of normal vectors of 1cm by setting them manually x=1cm, y=1cm
  • The third grid needs to be drawn manually. This is not as nice, but works anyway.
  • I tried to make it use as much space as exists, however you need a reduction \factor, since somehow images end up being larger and get shifted on page in the final document. You probably can get away with slightly larger values than 0.9, but you will need to adjust this manually.
  • The result in this case is still a very small image. I wold suggest you to use the geometry package to shrink the margins and thus reduce the whitespace around your image.
  • Seeing your example I assume you are familiar enough to adjust line width, font size, colours yourself.
  • I removed some irrelevant elements from your example, to shorten stuff a bit.

And this is how the results looked for me (without page margins): enter image description here

Timm
  • 803
10

Add the following lines to the options of axis

axis background/.style={postaction={path picture={
  \draw[line width=.1pt,draw=black!30]
     (axis cs:0,0) grid[step=1]
     (axis cs:\pgfkeysvalueof{/pgfplots/xmax},\pgfkeysvalueof{/pgfplots/ymax});
  \draw[line width=.1pt,draw=red!50]
     (axis cs:0,0) grid[step=5]
     (axis cs:\pgfkeysvalueof{/pgfplots/xmax},\pgfkeysvalueof{/pgfplots/ymax});
  \draw[line width=.2pt,draw=red!50]
     (axis cs:0,0) grid[step=10]
     (axis cs:\pgfkeysvalueof{/pgfplots/xmax},\pgfkeysvalueof{/pgfplots/ymax});
}}},

enter image description here

\documentclass[border=3pt]{standalone}
\usepackage{pgfplots,siunitx}
\begin{document}
\begin{tikzpicture}
            \begin{axis}[
                axis background/.style={postaction={path picture={
                \draw[line width=.1pt,draw=black!30]
                   (axis cs:0,0) grid[step=1]
                   (axis cs:\pgfkeysvalueof{/pgfplots/xmax},\pgfkeysvalueof{/pgfplots/ymax});
                \draw[line width=.1pt,draw=red!50]
                   (axis cs:0,0) grid[step=5]
                   (axis cs:\pgfkeysvalueof{/pgfplots/xmax},\pgfkeysvalueof{/pgfplots/ymax});
                \draw[line width=.2pt,draw=red!50]
                   (axis cs:0,0) grid[step=10]
                   (axis cs:\pgfkeysvalueof{/pgfplots/xmax},\pgfkeysvalueof{/pgfplots/ymax});
                }}},
                axis lines = middle,
                xlabel = t\lbrack\si{\second}\rbrack, 
                ylabel = x\lbrack\si{\metre}\rbrack, 
                x label style = {anchor = north west, font = \scriptsize},
                y label style = {anchor = north east, font = \scriptsize, shift={(0,0.5cm)}},
                xtick = {0,10,...,20},
                minor xtick = {1,2,3,4,5,6,7,8,9,11,12,13,14,15,16,17,18,19},
                ytick = {0,10,...,20},
                minor ytick = {1,2,3,4,5,6,7,8,9,11,12,13,14,15,16,17,18,19},
                tick style = {line width = 0.3pt},
                ticklabel style = {font = \tiny, fill = white},
                axis line style = {-latex},
%                grid = both,
                ymin = 0, 
                ymax = 21, 
                xmin = 0, 
                xmax = 21,
                ]
                \addplot[
                only marks,
                mark size = 2,
                black, 
                error bars/.cd, 
                y dir = both, 
                x dir = both, 
                y explicit, 
                x explicit
                ]
                %table[x= temp, y = temp, x error = temp, y error = temo]
                %{\datafile};
                coordinates {(6.4,7.2) +-(1,0.5)};
            \end{axis}
        \end{tikzpicture}
\end{document}
gernot
  • 49,614
  • Thanks, this is also a nice workaround. I think I'll end up combining both answers to get the result I want. – Skydiver Mar 05 '17 at 15:50