8

I want two axis environments next to each other in one figure environment. I want them to be vertically aligned and scaled to the width of the text. I am trying to achieve this by means of the tikzscale package and \matrix group plot capability of pgfplots, but I get an error.

Compiling

\documentclass{article}
\usepackage{pgfplots}
\usepackage{tikzscale}

\begin{document}
    \includegraphics{tikzpicture.tikz}
\end{document}

where tikzpicture.tikz has the content

\begin{tikzpicture} % THIS EXAMPLE STEMS FROM PAGE 288 FROM THE PGFPLOTS MANUAL REVISION 1.8 (2013/03/17).
    \matrix
    {
        \begin{axis}
            \addplot {x};
        \end{axis}
        &
        % differently large labels are aligned automatically:
        \begin{axis}
        [
            ylabel={$f(x)=x^2$},
            ylabel style={font=\Huge}
        ]
            \addplot {x^2};
        \end{axis}
        \\
        %
        \begin{axis}
        [
            xlabel=$x$,
            xlabel style={font=\Huge}
        ]
            \addplot {x^3};
        \end{axis}
        &
        \begin{axis}
            \addplot {x^4};
        \end{axis}
        \\
    };
\end{tikzpicture}

works just fine. But when I then try to scale the (too wide) picture with the tikzscale package by changing

\includegraphics{tikzpicture.tikz}

to

\includegraphics[witdh=\textwidth]{tikzpicture.tikz}

I get the error

./tikzpicture.tikz:6: Package pgfplots Error: Error: Plot width `-100.58875pt' is too small. This cannot be implemented while maintaining constant size for labels. Sorry, label sizes are only approximate. You will need to adjust your width..

How do I fix this?

1
  • Why not using \usepgfplotslibrary{groupplots} from pgfplots-package? ;)
    – haver
    Nov 23, 2019 at 11:02

1 Answer 1

1

To obtain the results you are looking for I would work directly on the axis environments without using \matrix. The advantage is that you control directly the dimensions and positions of your graphs. Furthermore, you have the possibility to scale things globally at the end through the tikzpicture options.

enter image description here

The code is below. The coordinates of the lower-left corner of axis are controlled by the key=value at={(a, b)}. You may find Torbjørn T.'s answer at Positioning pgfplots axis in tikzpicture useful.

\documentclass[11pt, a4paper]{article}
\usepackage[top=105pt, bottom=75pt, left=75pt, right=75pt]{geometry}
\setlength{\headsep}{15pt}
\setlength{\footskip}{45pt}

\usepackage{lipsum}
\usepackage{pgfplots}

\begin{document}

\thispagestyle{empty}
\begin{flushleft}
  \Large\bfseries Scaling \verb|axis|
\end{flushleft}


\lipsum[1]

\begin{center}
  \begin{tikzpicture}[scale=1.05, every node/.style={scale=1}]

    \begin{axis}[width=0.43\textwidth, height=6cm, at={(0, 0)}]
      \addplot {x};
    \end{axis}
    
    \begin{axis}[ylabel={$f(x)=x^2$}, ylabel style={font=\Huge},
      width=0.45\textwidth, height=6cm, at={(0.5\textwidth, 0)}]
      \addplot {x^2};
    \end{axis}

    \begin{axis}[xlabel=$x$, xlabel style={font=\Huge},
      width=0.43\textwidth, height=6cm, at={(0, -6 cm)}]
      \addplot {x^3};
    \end{axis}

    \begin{axis}[width=0.45\textwidth, height=6cm,
      at={(0.5\textwidth, -6 cm)}]
      \addplot {x^4};
    \end{axis}
 \end{tikzpicture}
\end{center}

\lipsum[2]

\end{document}

You must log in to answer this question.

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