条形图中两个具有不同比例的 y 轴

条形图中两个具有不同比例的 y 轴

我试图在下方 x 轴上以秒为单位显示测量值,而上方 x 轴应该显示不同的变量(消息计数)。我读过类似的问题,但它们并不适用于确切的问题。

如果我把所有图都放在第一个轴上,我似乎无法让上轴上的 x 刻度正确。它们很难控制。如果我把“不同”的图放在第二个轴上,它看起来就像下面的例子一样。能否让条形图看起来像属于任一类别并具有不同的轴?任何帮助都非常感谢!

MWE 图

我的 MWE 如下:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.3}

\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}%[scale=1.08]
\begin{axis}[
x tick label style={
    /pgf/number format/1000 sep=},
ylabel={some feature},
y label style={at={(axis description cs:-0.1,0.5)},anchor=north},
xlabel={time [s]},
enlargelimits=0.3,
enlarge y limits=0.55,
enlarge x limits={upper,value=0.1},
y=50pt,
axis x line*=bottom,
legend style={at={(0.5,1.25)},
    anchor=north},
symbolic y coords={enabled,disabled},
ytick=data,
xbar=5pt, xmin=0,xmax=35,
width=340pt,
y tick label style={rotate=315,anchor=east},
nodes near coords,
every node near coord/.append style={anchor=west, font=\small},
bar width=15pt,
]
\addplot [fill=gray] 
coordinates {(13.0,disabled) (14.9,enabled)};
\addplot [fill=darkgray]
coordinates {(32.4,disabled) (25.0,enabled)};
\legend{ Tasklet,  AppTask}
\end{axis}
\begin{axis}[   
xbar,
axis x line*=top,
xlabel={this is just a placeholder},
xlabel near ticks,
enlargelimits=0.3,
enlarge x limits={upper,value=0.1},
enlarge y limits=0.55,
axis y line=none,
xtick=data,xmin=0,
width=340pt,
y=50pt,
symbolic y coords={enabled,disabled},
nodes near coords,
every node near coord/.append style={anchor=west, font=\small},
bar width=15pt,
legend style={at={(0.5,1.25)},
anchor=north},
legend columns=3,
]
\addplot [fill=yellow]
coordinates {(168,disabled) (542,enabled)};%168 and 542
\addlegendentry{test}
\end{axis}
\end{tikzpicture}
\caption{Execution times for AppTasks and Tasklets. Parameters: 5 simulation runs, 1 broker, 100 providers, 50 consumers, forwarding enabled versus disabled, dynamic cache list sizing, filtered cache list composition mode, dynamic [$6s,3s,1s$] dissemination interval}
\label{fig:MeasurementForwarding100}
\end{figure}
\end{document}

答案1

我觉得你的做法很好,只需要为第三个情节腾出空间。

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.3}

\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}%[scale=1.08]
\begin{axis}[
x tick label style={
    /pgf/number format/1000 sep=},
ylabel={~~~~~~~some feature}, %<- added some spaces to avoid overlaps
y label style={at={(axis description cs:-0.1,0.5)},anchor=north},
xlabel={time [s]},
enlargelimits=0.3,
enlarge y limits=0.55,
enlarge x limits={upper,value=0.1},
y=75pt,
axis x line*=bottom,
legend style={at={(0.5,1.25)},
    anchor=north},
symbolic y coords={enabled,disabled},
ytick=data,
xbar=5pt, xmin=0,xmax=35,
width=340pt,
y tick label style={rotate=315,anchor=east},
nodes near coords,
every node near coord/.append style={anchor=west, font=\small},
bar width=15pt,
]
\addplot [fill=gray] 
coordinates {(13.0,disabled) (14.9,enabled)};
\addplot [fill=gray] 
coordinates {(0,disabled) (0,enabled)}; %<- added a placeholder plot
\addplot [fill=darkgray]
coordinates {(32.4,disabled) (25.0,enabled)};
\legend{ Tasklet,  AppTask}
\end{axis}
\begin{axis}[   
xbar,
axis x line*=top,
xlabel={},
xlabel near ticks,
enlargelimits=0.3,
enlarge x limits={upper,value=0.1},
enlarge y limits=0.55,
axis y line=none,
xtick=data,xmin=0,
width=340pt,
y=75pt,
symbolic y coords={enabled,disabled},
nodes near coords,
every node near coord/.append style={anchor=west, font=\small},
bar width=15pt,
legend style={at={(0.75,1.25)},
anchor=north},
legend columns=3,
]
\addplot [fill=yellow]
coordinates {(168,disabled) (542,enabled)};%168 and 542
\addlegendentry{test}
\end{axis}
\end{tikzpicture}
\caption{Execution times for AppTasks and Tasklets. Parameters: 5 simulation runs, 1 broker, 100 providers, 50 consumers, forwarding enabled versus disabled, dynamic cache list sizing, filtered cache list composition mode, dynamic [$6s,3s,1s$] dissemination interval}
\label{fig:MeasurementForwarding100}
\end{figure}
\end{document}

在此处输入图片描述

相关内容