我正在使用以下命令来绘制条形图。
\begin{tikzpicture}
\pgfplotstableread{ % Read the data into a table macro
Label Female Male
2006-2008 3 1
2007-2009 2 0
2008-2010 9 2
2019-2011 3 3
2010-2012 11 1
2011-2013 6 0
2012-2014 12 7
2013-2015 10 7
2014-2016 10 3
}\datatable
\begin{axis}[
compat=newest, %Better label placement
% axis on top,
ybar stacked, % Stacked horizontal bars
xmin=0, % Start x axis at 0
ymax=25,
ymin=0, ylabel={Number of students}, xlabel={Batches},bar width=10pt,
y axis line style={opacity=0},
yticklabels={\empty},
ytick style={draw=none},enlarge y limits={upper, value=0.2},
xtick=data, % Use as many tick labels as x coordinates
enlarge x limits = .2,
xticklabels from table={\datatable}{Label} % Get the labels from the Label column of the \datatable
]
\addplot [fill=yellow] table [y=Male, x expr=\coordindex] {\datatable}; % Plot the "First" column against the data index
\addplot [fill=green!70!blue]table [y=Female, x expr=\coordindex] {\datatable};
% \addplot [fill=red!80!yellow] table [y=Third, x expr=\coordindex] {\datatable};
\legend{Male,Female}
\end{axis}
\end{tikzpicture}
我得到这样的照片,
如何正确获取 x 轴上的空间?
如何将 y 值放置在相应的条形上?
如何将标签旋转90度?
怎样除去上面的蜱虫?
还有其他方式来表示这些数据吗(仅限二维)?(可选)
我希望所有问题都能在一个答案中得到解答?
答案1
见下文。
添加
nodes near coords
到axis
选项中。xticklabel style={rotate=90}
您基本上已经自己回答了这个问题:
xtick style={draw=none}
\documentclass[border=4mm,tikz]{standalone}
\usepackage{pgfplots,pgfplotstable}
\begin{document}
\begin{tikzpicture}
\pgfplotstableread{ % Read the data into a table macro
Label Female Male
2006-2008 3 1
2007-2009 2 0
2008-2010 9 2
2019-2011 3 3
2010-2012 11 1
2011-2013 6 0
2012-2014 12 7
2013-2015 10 7
2014-2016 10 3
}\datatable
\begin{axis}[
compat=newest, %Better label placement
% axis on top,
ybar stacked, % Stacked horizontal bars
xmin=0, % Start x axis at 0
ymax=25,
ymin=0, ylabel={Number of students}, xlabel={Batches},bar width=10pt,
y axis line style={opacity=0},
xtick style={draw=none}, % added
yticklabels={\empty},
ytick style={draw=none},enlarge y limits={upper, value=0.2},
xtick=data, % Use as many tick labels as x coordinates
enlarge x limits = .2,
xticklabels from table={\datatable}{Label}, % Get the labels from the Label column of the \datatable
xticklabel style={rotate=90}, % added
nodes near coords % added
]
\addplot [fill=yellow] table [y=Male, x expr=\coordindex] {\datatable}; % Plot the "First" column against the data index
\addplot [fill=green!70!blue]table [y=Female, x expr=\coordindex] {\datatable};
% \addplot [fill=red!80!yellow] table [y=Third, x expr=\coordindex] {\datatable};
\legend{Male,Female}
\end{axis}
\end{tikzpicture}
\end{document}
第2部分
我认为没有自动增加宽度以适应宽刻度标签的选项,因此您必须设置width=<some large enough length>
。下面我稍微减小了 xticklabels 的字体大小,并修改了的值enlarge x limits
。
\documentclass[border=4mm,tikz]{standalone}
\usepackage{pgfplots,pgfplotstable}
\begin{document}
\begin{tikzpicture}
\pgfplotstableread{ % Read the data into a table macro
Label Female Male
2006-2008 3 1
2007-2009 2 0
2008-2010 9 2
2019-2011 3 3
2010-2012 11 1
2011-2013 6 0
2012-2014 12 7
2013-2015 10 7
2014-2016 10 3
}\datatable
\begin{axis}[
compat=newest, %Better label placement
% axis on top,
ybar stacked, % Stacked horizontal bars
xmin=0, % Start x axis at 0
ymax=25,
ymin=0, ylabel={Number of students}, xlabel={Batches},bar width=10pt,
y axis line style={opacity=0},
xtick style={draw=none}, % added
yticklabels={\empty},
ytick style={draw=none},enlarge y limits={upper, value=0.2},
xtick=data, % Use as many tick labels as x coordinates
enlarge x limits = 0.02, % modified
xticklabels from table={\datatable}{Label}, % Get the labels from the Label column of the \datatable
nodes near coords,
width=14cm,
xticklabel style={font=\footnotesize}
]
\addplot [fill=yellow] table [y=Male, x expr=\coordindex] {\datatable}; % Plot the "First" column against the data index
\addplot [fill=green!70!blue]table [y=Female, x expr=\coordindex] {\datatable};
% \addplot [fill=red!80!yellow] table [y=Third, x expr=\coordindex] {\datatable};
\legend{Male,Female}
\end{axis}
\end{tikzpicture}
\end{document}