我正在制作一个图表来表示比赛结果。每个团队在五个不同类别中都获得了分数,我想用一个条形图来表示每个团队的分数,x 轴上方有两组分数,下方有三组分数。
其中一个不可或缺的部分是创建条形图各部分的命令。现在基本上是一个高度<points>/100
和宽度为的矩形.5
,<points>
写在位置为(.25,<points>/200)
。以下命令产生正确的输出,但我希望能够定义它的位置。
\newcommand{\myrect}[1]{
\path[draw](0,0) rectangle +(.5,#1/100);
\node at (.25,#1/200) {#1};
}
我希望能够写一些类似这样的 MW(ish)E:
\documentclass[tikz]{standalone}
\usepackage{calc}
\newcommand{\myrect}[1]{
\path[draw](0,0) rectangle +(.5,#1/100);
\node at (0.5,#1/200) {#1};
}
\begin{document}
\begin{tikzpicture}
\path[draw,thick,->] (0,0)--(8,0);
\path[draw,thick,<->] (0,-3)--(0,3);
\myrect{86} at (.25,0);
\myrect{74} at (.25,.86);
\myrect{91} at (.25,-.91); %and so on
\end{tikzpicture}
\end{document}
我知道这可能是基本的 TikZ,但我所看到的与此相关的一切似乎都过于复杂。我该如何最好地做到这一点?
编辑:这
pgfplots
似乎是一个很好的解决方案,它让我更进一步。到目前为止,我已经
\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{polyglossia}
\setmainlanguage{swedish}
\pgfplotsset{width=7cm,compat=1.9}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar stacked,
nodes near coords,
ylabel={Poäng},
symbolic x coords={f,e,m},
enlargelimits=.1,
xtick={}
]
\addplot coordinates {(f,20) (e,50) (m,90)};
\addplot coordinates {(f,70) (e,24) (m,70)};
\addplot coordinates {(f,50) (e,60) (m,10)};
\end{axis}
\end{tikzpicture}
\end{document}
导致
我真正想要的是
也就是说,在 x 轴下方有一个新的堆叠条形图。有人有什么想法吗?
答案1
尝试这样的操作:
\documentclass[tikz]{standalone}
\usepackage{calc}
\newcommand{\myrect}[3]{
\path[draw](#2,#3) rectangle +(.6,#1/100);
\node[anchor=east] at (0.6+#2,#1/200+#3) {#1};
}
\begin{document}
\begin{tikzpicture}
\path[draw,thick,->] (0,0)--(8,0);
\path[draw,thick,<->] (0,-3)--(0,3);
\myrect{86}{0}{0}
\myrect{74}{0.6}{0};
\myrect{-91}{1.2}{0}
\end{tikzpicture}
\end{document}
或者更好的是:
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.8}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar,
enlargelimits=0.15,
legend style={at={(0.5,-0.15)},
anchor=north,legend columns=-1},
ylabel={\#participants},
symbolic x coords={Com1,Com2,Com3},
xtick=data,
nodes near coords,
nodes near coords align={vertical},
]
\addplot coordinates {(Com1,86) (Com2,74) (Com3,-91)};
\end{axis}
\end{tikzpicture}
\end{document}
堆积条形图来了解如何解决问题:
\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\begin{document}
\pgfplotsset{every axis/.append style={
font=\normalsize\rmfamily,
line width=1 pt,
tick style={line width=.6pt}},
%every node near coord/.append style={anchor=north}
}
\begin{tikzpicture}
\begin{axis}[
axis background/.style={shade,top color=gray,bottom color=white},
ybar stacked,
bar width=1cm,
enlarge x limits=.15,
ylabel={Count},
symbolic x coords={
Com1,Com2,Com3},
xtick=data,
nodes near coords,
every node near coord/.append style={anchor=south,yshift=-0.25cm,font=\scriptsize},
%nodes near coords align={vertical},
x tick label style={font=\scriptsize ,text width=2.7cm,align=center},
] \addplot+[black,fill=blue!25!gray] coordinates {
(Com1,3)
(Com2,1)
(Com3,3)
};
\addplot+[black,fill=green!25!gray] coordinates {
(Com1,3)
(Com2,1)
(Com3,3)
};
\addplot+[black,fill=red!75] coordinates {
(Com1,3)
(Com2,1)
(Com3,3)
};
\end{axis}
\end{tikzpicture}
\end{document}