我需要在 tikz 箱线图的 x 轴上显示绿色条,如下图所示。(绿色条表示当值高或低时,与该值对应的问题是否为正。)
我使用以下 Latex 代码绘制了类似的箱线图。但我无法在 x 轴(顶部和底部)上绘制绿色条。
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
\pgfplotsset{width=10cm,compat=1.9}
\begin{document}
\begin{figure}[htbp]
\begin{tikzpicture}
\begin{axis}[
boxplot/draw direction=y,
ylabel={Scores},
ytick={1,1.5,2,2.5,3,3.5,4,4.5,5},
yticklabels={1,1.5,2,2.5,3,3.5,4,4.5,5},
ymin = 0.8, ymax = 5.3,
width=\textwidth,
boxplot={
box extend=0.3,
},
xtick={0.5,...,20},
x tick label as interval,
xticklabels={
{Q1},{Q2},{Q3},{Q4},{Q5},{Q6},{Q7},{Q8},{Q9},{Q10}
},
x tick label style={
text width=1.5cm,
align = center
},
xmin=0,xmax=11
]
\addplot[draw=black,fill={rgb:red,4;green,2;yellow,1}]
table[row sep=\\,y index=0] {data\\5\\4\\5\\5\\4\\4\\3\\5\\};
\addplot[draw=black,fill={rgb:red,2;green,1;yellow,2}]
table[row sep=\\,y index=0] {data\\1\\2\\1\\1\\1\\2\\2\\1\\};
\addplot[draw=black,fill=green!50!red]
table[row sep=\\,y index=0] {data\\4\\5\\5\\5\\5\\4\\4\\5\\};
\addplot[draw=black, fill=black!50]
table[row sep=\\,y index=0] {data\\1\\1\\1\\2\\1\\2\\2\\3\\};
\addplot[draw=black, fill=blue!50!red]
table[row sep=\\,y index=0] {data\\5\\4\\5\\5\\4\\5\\4\\5\\};
\addplot[draw=black, fill=green!50!blue]
table[row sep=\\,y index=0] {data\\1\\1\\2\\1\\1\\2\\2\\3\\};
\addplot[draw=black, fill=red!50!yellow]
table[row sep=\\,y index=0] {data\\5\\4\\4\\5\\5\\4\\3\\4\\};
\addplot[draw=black, fill=black!20]
table[row sep=\\,y index=0] {data\\2\\1\\1\\1\\1\\2\\1\\2\\};
\addplot[draw=black, fill={rgb:red,1;green,2;blue,3}]
table[row sep=\\,y index=0] {data\\4\\4\\5\\5\\5\\4\\4\\5\\};
\addplot[draw=black, fill={rgb:red,0;green,0.5;blue,1}]
table[row sep=\\,y index=0] {data\\2\\1\\2\\2\\1\\2\\2\\2\\};
\end{axis}
\end{tikzpicture}
\caption{SUS Usability}
\end{figure}
\end{document}
对应图:
我怎样才能创建如第一幅图所示的绿色条?
答案1
欢迎!您可以循环添加它们。
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
\pgfplotsset{width=10cm,compat=1.9}
\begin{document}
\begin{figure}[htbp]
\begin{tikzpicture}
\begin{axis}[
boxplot/draw direction=y,
ylabel={Scores},
ytick={1,1.5,2,2.5,3,3.5,4,4.5,5},
yticklabels={1,1.5,2,2.5,3,3.5,4,4.5,5},
ymin = 0.8, ymax = 5.3,
width=\textwidth,
boxplot={
box extend=0.3,
},
xtick={0.5,...,20},
x tick label as interval,
xticklabels={
{Q1},{Q2},{Q3},{Q4},{Q5},{Q6},{Q7},{Q8},{Q9},{Q10}
},
x tick label style={
text width=1.5cm,
align = center
},
xmin=0,xmax=11
]
\addplot[draw=black,fill={rgb:red,4;green,2;yellow,1}]
table[row sep=\\,y index=0] {data\\5\\4\\5\\5\\4\\4\\3\\5\\};
\addplot[draw=black,fill={rgb:red,2;green,1;yellow,2}]
table[row sep=\\,y index=0] {data\\1\\2\\1\\1\\1\\2\\2\\1\\};
\addplot[draw=black,fill=green!50!red]
table[row sep=\\,y index=0] {data\\4\\5\\5\\5\\5\\4\\4\\5\\};
\addplot[draw=black, fill=black!50]
table[row sep=\\,y index=0] {data\\1\\1\\1\\2\\1\\2\\2\\3\\};
\addplot[draw=black, fill=blue!50!red]
table[row sep=\\,y index=0] {data\\5\\4\\5\\5\\4\\5\\4\\5\\};
\addplot[draw=black, fill=green!50!blue]
table[row sep=\\,y index=0] {data\\1\\1\\2\\1\\1\\2\\2\\3\\};
\addplot[draw=black, fill=red!50!yellow]
table[row sep=\\,y index=0] {data\\5\\4\\4\\5\\5\\4\\3\\4\\};
\addplot[draw=black, fill=black!20]
table[row sep=\\,y index=0] {data\\2\\1\\1\\1\\1\\2\\1\\2\\};
\addplot[draw=black, fill={rgb:red,1;green,2;blue,3}]
table[row sep=\\,y index=0] {data\\4\\4\\5\\5\\5\\4\\4\\5\\};
\addplot[draw=black, fill={rgb:red,0;green,0.5;blue,1}]
table[row sep=\\,y index=0] {data\\2\\1\\2\\2\\1\\2\\2\\2\\};
\pgfplotsinvokeforeach{1,...,10}{%
\path ([xshift=-1ex]axis cs:#1,1) coordinate (aux-#1);
\ifodd#1
\fill[green!60!black] (aux-#1|-current axis.north) rectangle ++(2ex,-0.75ex);
\else
\fill[green!60!black] (aux-#1|-current axis.south) rectangle ++(2ex,0.75ex);
\fi}
\end{axis}
\end{tikzpicture}
\caption{SUS Usability}
\end{figure}
\end{document}