我有以下图表:
\usepackage{pgfplots}
\begin{tikzpicture}
\begin{axis}[y dir = reverse,
width=12cm,
height=10cm,
xtick={1,...,6},
ytick={-1,...,-1000},
ymax=-600,
xlabel = Number of tasks trained,
ylabel = Cost
]
\addplot+[ycomb, very thick] coordinates {(1, -612) (2, -612) (3, -611) (4,-611) (5, -611)};
\addplot+[ycomb, very thick] coordinates { (2, -611.5) (3, -611) (4,-611) (5, -610.5)};
\addplot+[ycomb, very thick] coordinates { (3, -612) (4,-611) (5, -610.9)};
\addplot+[ycomb, very thick] coordinates { (4,-613) (5, -613)};
\addplot+[ycomb, very thick] coordinates { (5, -612.5)};
\end{axis}
\end{tikzpicture}
问题是所有梳状图的条形图都重叠了。有没有办法像条形图一样将它们展开?
答案1
半自动,xshift
每个添加一个\addplot
:
\documentclass[border=3mm]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[y dir = reverse,
width=12cm,
height=10cm,
xtick={1,...,6},
ytick={-1,...,-1000},
ymax=-600,
xlabel = Number of tasks trained,
ylabel = Cost
]
\addplot+[ycomb, very thick,xshift=-4mm] coordinates {(1, -612) (2, -612) (3, -611) (4,-611) (5, -611)};
\addplot+[ycomb, very thick,xshift=-2mm] coordinates { (2, -611.5) (3, -611) (4,-611) (5, -610.5)};
\addplot+[ycomb, very thick] coordinates { (3, -612) (4,-611) (5, -610.9)};
\addplot+[ycomb, very thick,xshift=2mm] coordinates { (4,-613) (5, -613)};
\addplot+[ycomb, very thick,xshift=4mm] coordinates { (5, -612.5)};
\end{axis}
\end{tikzpicture}
\end{document}