我正在创建一个带对数刻度的分组条形图,但遇到了一些问题。在下图中,您可以看到 x 刻度上的刻度位置不正确。开头是正确的,但例如“St.8”的位置太靠左了。
\documentclass[12pt,a4paper,onecolumn, openright]{report}
\usepackage{xcolor}
\usepackage{pgfplots}
\usepackage{tikz}
\definecolor{bblue}{HTML}{4F81BD}
\definecolor{rred}{HTML}{C0504D}
\definecolor{ggreen}{HTML}{9BBB59}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width = 0.9\textwidth,
height = 8cm,
bar width=4pt,
major x tick style = transparent,
symbolic x coords={St.~1,St.~2,St.~3,St.~4,St.~5,St.~6,St.~7,St.~8},
enlarge x limits=0.1,
xlabel={Stufe des Algorithmus},
x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
ymin = 0,
ybar = \pgflinewidth,
ymajorgrids = true,
ylabel = {Anzahl},
ymode=log,
log basis y={10},
legend cell align=left,
legend style={
at={(1,1.05)},
anchor=south east,
column sep=1ex
}
]
\addplot[style={bblue,fill=bblue,mark=none}] coordinates {(St.~1, 1) (St.~2,10006) (St.~3,99895) (St.~4, 99867)};
\addplot[style={rred,fill=rred,mark=none}] coordinates {(St.~1,1) (St.~2,10006) (St.~3,99448) (St.~4, 99487)};
\addplot[style={ggreen,fill=ggreen,mark=none}] coordinates {(St.~1,1) (St.~2,10) (St.~3,10) (St.~4, 10)};
\addplot[style={bblue!50,fill=bblue!50,mark=none}] coordinates {(St.~5,99810) (St.~6,99857) (St.~7, 80000) (St.~8, 40000)};
\addplot[style={rred!50,fill=rred!50,mark=none}] coordinates {(St.~5,99416) (St.~6,99463) (St.~7, 79685) (St.~8, 39842)};
\addplot[style={ggreen!50,fill=ggreen!50,mark=none}] coordinates {(St.~5,10) (St.~6,10) (St.~7, 10) (St.~8, 0)};
\legend{Muster,Subgruppen,Gekürzt}
\end{axis}
\end{tikzpicture}
\end{document}
我尝试重新排列绘图代码并获得了更好的结果,但条形不再分组。我该怎么做才能保持分组并更正 x 轴?
% blue
\addplot[style={bblue,fill=bblue,mark=none}] coordinates {(St.~1, 1) (St.~2,10006) (St.~3,99895) (St.~4, 99867)};
\addplot[style={bblue!50,fill=bblue!50,mark=none}] coordinates {(St.~5,99810) (St.~6,99857) (St.~7, 80000) (St.~8, 40000)};
% red
\addplot[style={rred,fill=rred,mark=none}] coordinates {(St.~1,1) (St.~2,10006) (St.~3,99448) (St.~4, 99487)};
\addplot[style={rred!50,fill=rred!50,mark=none}] coordinates {(St.~5,99416) (St.~6,99463) (St.~7, 79685) (St.~8, 39842)};
% green
\addplot[style={ggreen,fill=ggreen,mark=none}] coordinates {(St.~1,1) (St.~2,10) (St.~3,10) (St.~4, 10)};
\addplot[style={ggreen!50,fill=ggreen!50,mark=none}] coordinates {(St.~5,10) (St.~6,10) (St.~7, 10) (St.~8, 0)};
答案1
那么,您希望获得如下内容吗?
% used PGFPlots v1.14
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.3}
\definecolor{bblue}{HTML}{4F81BD}
\definecolor{rred}{HTML}{C0504D}
\definecolor{ggreen}{HTML}{9BBB59}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=0.9\textwidth,
height=8cm,
bar width=4pt,
% commented to see, if the result is correct
% major x tick style=transparent,
symbolic x coords={St.~1,St.~2,St.~3,St.~4,St.~5,St.~6,St.~7,St.~8},
enlarge x limits=0.1,
xlabel={Stufe des Algorithmus},
x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
% changed from `\pgflinewidth' to 1 easier see that the result is correct
ymin=1,
% changed to 0 to make the computation of the shift easier
ybar=0pt,
ymajorgrids=true,
ylabel={Anzahl},
ymode=log,
legend cell align=left,
legend style={
at={(1,1.05)},
anchor=south east,
column sep=1ex
},
]
% add a scope around the to shift bars
\begin{scope}[
% shift the bars accordingly
xshift={0.75*\pgfplotbarwidth},
% and just "fill" the bars
% (to avoid overlapping of the more right bars to the more left ones)
draw=none,
]
% your style could heavily be simplified to just providing the `+'
% sign and the color as option
\addplot+ [bblue] coordinates
{(St.~1, 1) (St.~2,10006) (St.~3,99895) (St.~4, 99867)};
\addplot+ [rred] coordinates
{(St.~1,1) (St.~2,10006) (St.~3,99448) (St.~4, 99487)};
\addplot+ [ggreen] coordinates
{(St.~1,1) (St.~2,10) (St.~3,10) (St.~4, 10)};
\end{scope}
\begin{scope}[
xshift={-0.75*\pgfplotbarwidth},
draw=none,
]
\addplot+ [bblue!50] coordinates
{(St.~5,99810) (St.~6,99857) (St.~7, 80000) (St.~8, 40000)};
\addplot+ [rred!50] coordinates
{(St.~5,99416) (St.~6,99463) (St.~7, 79685) (St.~8, 39842)};
\addplot+ [ggreen!50] coordinates
{(St.~5,10) (St.~6,10) (St.~7, 10) (St.~8, 0)};
\end{scope}
\legend{Muster,Subgruppen,Gekürzt}
\end{axis}
\end{tikzpicture}
\end{document}