3

I want that the following two plots share their x-axis using pgfplots and tikzpicture. It is important that the first one is fully logarithmic and at the second plot only the x-axis (semilogx). I did not find an idea for this special case. In addition to that I still want to have two plots. So the first plot, then a line and below the second plot with the x-axis. Only this x-axis shall be shared.

\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}

\include{pgfpages}
\makeatletter

\usepackage {tikz}
\usepackage{pgfplots}
\begin{document}

\begin{center}
\begin{tikzpicture}
   \begin{loglogaxis}[width=0.8\textwidth,height=0.3\textheight]
     \addplot[red] plot coordinates{(1,1)(2,4)(3,9)(4,16)};
   \end{loglogaxis}
\end{tikzpicture}

\begin{tikzpicture}
   \begin{semilogxaxis}[width=0.8\textwidth,height=0.15\textheight]
     \addplot[blue] plot coordinates{(1,0)(2,0.2)(3,0.2)(4,0.4)};
   \end{semilogxaxis}
\end{tikzpicture}   
\end{center}
\end{document}

So all in all there should be one tikzpicture. It would be great if the CSV import would work with that too, because in my paper I import it that way, but left it out here to keep it as simple as possible.

Stefan Pinnow
  • 29,535
  • But that is not what I want. It is important that the two plots are not in the same field. I Hope it is clearer now. – Alex Heintz Jun 27 '18 at 09:24

1 Answers1

4

Assuming that accepting my answer is equivalent with "that is what I was searching for, I revised my answer adding some comments to the code and also provided a second attempt giving the same result using the pgfplots.groupplots library.

For details please have a look at the comments in the code.

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
    \begin{loglogaxis}[
        % don't show any `xticklabels' on the first (upper) plot
        xticklabels={},
        % give the plot a (coordinate/node) `name'
        name=first plot,
    ]
        \addplot [red]  coordinates { (1,1)(2,4)(3,9)(4,16) };
    \end{loglogaxis}
    \begin{semilogxaxis}[
        % plot the second plot relative to the first one ...
        at=(first plot.south),
        % ... and use an appropriate `anchor'
        anchor=north,
%        % if you need an "offset" between the plots, you can add e.g. a `yshift'
%        yshift=-5mm,
    ]
        \addplot [blue] coordinates { (1,0)(2,0.2)(3,0.2)(4,0.4) };
    \end{semilogxaxis}
\end{tikzpicture}
\end{document}

And here the solution with the pgfplots.groupplots library.

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \usepgfplotslibrary{groupplots}
\begin{document}
\begin{tikzpicture}
    \begin{groupplot}[
        group style={
            % set how the plots should be organized
            group size=1 by 2,
            % only show ticklabels and axis labels on the bottom
            x descriptions at=edge bottom,
            % set the `vertical sep' to zero
            vertical sep=0pt,
        },
        % set the x axis default to logarithmic
        xmode=log
    ]
    % start the first plot
    \nextgroupplot[
        ymode=log,
    ]
        \addplot [red]  coordinates { (1,1)(2,4)(3,9)(4,16) };

    % start the second plot
    \nextgroupplot[
        ymode=normal,
    ]
        \addplot [blue] coordinates { (1,0)(2,0.2)(3,0.2)(4,0.4) };
    \end{groupplot}
\end{tikzpicture}
\end{document}

image showing the result of above code

Stefan Pinnow
  • 29,535