我目前正在尝试将数据格式化为条形图。在这里,我希望第一阶段的条形与同一图中的其他条形颜色不同。我尝试使用 4 个不同的图来实现这一点,并将其他图表中的值设置为 0,但现在其他图表应该存在这些间隙。图片下方是我当前的代码。有没有办法只更改第一阶段条形的颜色,而不为其他阶段创建不可见的条形?提前致谢!
\centering
\begin{tikzpicture}
\begin{axis}[symbolic x coords={Stage 1, Stage 2, Stage 3, Stage 4, Stage 5}, xtick=data, xbar = .1cm, x = 2cm, ylabel = {Times chosen}, ymajorgrids = true, ymin=0, xtick style={draw=none}]
\addplot[ybar, fill=green] coordinates {
(Stage 1, 69)
(Stage 2, 0)
(Stage 3, 0)
(Stage 4, 0)
(Stage 5, 0)
};
\addplot[ybar, fill=orange] coordinates {
(Stage 1, 231)
(Stage 2, 0)
(Stage 3, 0)
(Stage 4, 0)
(Stage 5, 0)
};
\addplot[ybar, fill=blue] coordinates {
(Stage 1, 0)
(Stage 2, 84)
(Stage 3, 234)
(Stage 4, 111)
(Stage 5, 198)
};
\addplot[ybar, fill=red] coordinates {
(Stage 1, 0)
(Stage 2, 216)
(Stage 3, 57)
(Stage 4, 189)
(Stage 5, 102)
};
\end{axis}
\end{tikzpicture}
\caption{Caption}
\label{fig:amt_result}
\end{figure}
答案1
\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
symbolic x coords={Stage 1, Stage 2, Stage 3, Stage 4, Stage 5},
xtick=data,
xbar=0.1cm,
x=2cm,
ylabel={Times chosen},
ymajorgrids=true,
ymin=0,
xtick style={draw=none},
]
\addplot[ybar, fill=green, forget plot, bar shift=-0.5*(\pgfplotbarwidth+0.1cm)] coordinates {
(Stage 1, 69)
};
\addplot[ybar, fill=orange, forget plot, bar shift=0.5*(\pgfplotbarwidth+0.1cm)] coordinates {
(Stage 1, 231)
};
\addplot[ybar, fill=blue] coordinates {
(Stage 2, 84)
(Stage 3, 234)
(Stage 4, 111)
(Stage 5, 198)
};
\addplot[ybar, fill=red] coordinates {
(Stage 2, 216)
(Stage 3, 57)
(Stage 4, 189)
(Stage 5, 102)
};
\end{axis}
\end{tikzpicture}
\end{document}