2

I'm trying to generate two different axes above each other with the bottom one being a more zoomed in version of the x-axis of the top one. The data is fetched from a csv file.

The CSV reading and generating is based on this question

An example of what I'm trying to accomplish from this article (I'm not the author).

I've got the following code:

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\usepgfplotslibrary{statistics}
\pgfplotsset{compat=1.16}
\makeatletter
\pgfplotsset{
  boxplot prepared from table/.code={
    \def\tikz@plot@handler{\pgfplotsplothandlerboxplotprepared}%
    \pgfplotsset{
      /pgfplots/boxplot prepared from table/.cd,
      #1,
    }
  },
  /pgfplots/boxplot prepared from table/.cd,
  table/.code={\pgfplotstablecopy{#1}\to\boxplot@datatable},
  row/.initial=0,
  make style readable from table/.style={
    #1/.code={
      \pgfplotstablegetelem{\pgfkeysvalueof{/pgfplots/boxplot prepared from table/row}}{##1}\of\boxplot@datatable
      \pgfplotsset{boxplot/#1/.expand once={\pgfplotsretval}}
    }
  },
  make style readable from table=lower whisker,
  make style readable from table=upper whisker,
  make style readable from table=lower quartile,
  make style readable from table=upper quartile,
  make style readable from table=median,
  make style readable from table=lower notch,
  make style readable from table=upper notch,
  make style readable from table=average
}
\makeatother

\begin{document}

\pgfplotstableread[col sep=comma]{./results/request_durations.csv}\datatable
\pgfplotstableread[col sep=comma]{./results/request_durations_large.csv}\datatablelarge

\begin{figure}
  \centering
  \begin{tikzpicture}
    \begin{axis}[
      boxplot,
      xmin=0,
      width=\textwidth,
      scale only axis,
      ytick=\empty,
      /pgfplots/boxplot/box extend=0.3,
      cycle list={},
      name=main plot,
      ]
      \pgfplotstablegetrowsof{\datatablelarge}
      \pgfmathtruncatemacro\TotalRows{\pgfplotsretval-1}
      \pgfplotsinvokeforeach{0,...,\TotalRows}
      {
        \addplot+[
        boxplot prepared from table={
          table=\datatablelarge,
          row=#1,
          lower whisker=min_duration_ms,
          upper whisker=max_duration_ms,
          lower quartile=lower_quartile,
          upper quartile=upper_quartile,
          median=median,
          average=avg_duration_ms,
        },
        boxplot prepared,
        black
        ]
        coordinates {};
        % node [below,inner sep=0.4cm,anchor=south west] at (200,#1+1)
        % {\pgfplotstablegetelem{#1}{http}\of\datatablelarge \scriptsize\pgfplotsretval};
      }
    \end{axis}
    \begin{axis}[
      boxplot,
      scale only axis,
      at={(main plot.below south west)},
      xmax=30000,
      width=\textwidth,
      ytick=\empty,
      /pgfplots/boxplot/box extend=0.3,
      cycle list={},
      yshift=0.1cm,
      ]
      \pgfplotstablegetrowsof{\datatable}
      \pgfmathtruncatemacro\TotalRows{\pgfplotsretval-1}
      \pgfplotsinvokeforeach{0,...,\TotalRows}
      {
        \addplot+[
        boxplot prepared from table={
          table=\datatable,
          row=#1,
          lower whisker=min_duration_ms,
          upper whisker=max_duration_ms,
          lower quartile=lower_quartile,
          upper quartile=upper_quartile,
          median=median,
          average=avg_duration_ms,
        },
        boxplot prepared,
        black
        ]
        coordinates {};
        % node [below,inner sep=0.4cm,anchor=south west] at (200,#1+1)
        % {\pgfplotstablegetelem{#1}{http}\of\datatable \scriptsize\pgfplotsretval};
      }
    \end{axis}
  \end{tikzpicture}
  \caption{Response times in milliseconds}
\end{figure}

\end{document}

.. but this results in the following oddly overlapping figure:

faulty overlapping axes

Heres an example of the csv files (they're the same format; slightly bigger x-axis data in the other):

http,frequency,min_duration_ms,lower_quartile,median,upper_quartile,max_duration_ms,avg_duration_ms,stddev_duration_ms,iqr
GET /endpoint,525,128,628,892,1398,2963,962,410,2835

The lines between them aside, how do I even get them to stack on top of each other properly?

1 Answers1

1

You missed to add a proper anchor ...

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
  \begin{tikzpicture}
    \begin{axis}[
      name=main plot,
    ]
    \end{axis}
    \begin{axis}[
      at={(main plot.below south west)},
      anchor=north west,    % <-- added
      yshift=-1ex,          % <-- adjusted
      ]
    \end{axis}
  \end{tikzpicture}
\end{document}

image showing the result of above code

Stefan Pinnow
  • 29,535