![两个相互对齐的轴,具有不同的 x 轴值](https://linux22.com/image/398010/%E4%B8%A4%E4%B8%AA%E7%9B%B8%E4%BA%92%E5%AF%B9%E9%BD%90%E7%9A%84%E8%BD%B4%EF%BC%8C%E5%85%B7%E6%9C%89%E4%B8%8D%E5%90%8C%E7%9A%84%20x%20%E8%BD%B4%E5%80%BC.png)
我正在尝试生成两个不同的轴,底部的轴是顶部轴的 x 轴的放大版本。数据是从 csv 文件中提取的。
我想要实现的一个例子从本文(我不是作者)。
我有以下代码:
\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}
..但这会导致以下奇怪重叠的图形:
这是 csv 文件的一个示例(它们是相同的格式;另一个中的 x 轴数据稍大一些):
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
除了它们之间的界线之外,我怎样才能让它们正确地堆叠在一起?
答案1
您忘了添加适当的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}