我有以下 tikz 代码
\begin{tikzpicture}
\begin{axis}[
xbar,
y axis line style = {opacity = 0},
axis x line = none,
tickwidth = 0pt,
enlarge y limits = 1.8,
legend style={at={(0.91,0.8)},anchor=east},
symbolic y coords = {2016,2009},
ytick = data,
nodes near coords
]
\addplot [color=black,fill=red] coordinates { (8518,2009) (10891,2016) };
\addplot [color=black,fill=blue] coordinates { (5827,2009) (5845,2016) };
\legend{DEB,LMR}
\end{axis}
\end{tikzpicture}
这给了我这个:
tiikz 图通过以下方式包含到我的主要 .tex 文件中
\begin{figure}[!htb]
\centering
\input{barchart.tex}
\caption{Here is the description of the plot}
\label{fig:barchartplot}
\end{figure}
但是,条形图的上方和下方都留有大量空白。我该如何“剪切”此图像?上方和下方都有很多文本,我不希望图像周围有那么多“白色区域”!
提前感谢您的帮助!
安德烈
答案1
默认值为bar width
10pt,y 值相同的两个条形之间的间隔为 2pt(默认值)。您有两个图,因此和xbar=2pt
之间所需的空间至少为 (2*10pt+2pt)+2pt=24pt。因此您可以使用2009
2016
y=30pt,
enlarge y limits={abs=15pt}% the value must be at least half of y
然后你必须删除enlarge y limits = 1.8
并更改legend style
。一种可能性是
legend style={at={(1,1-|{axis cs:10891,2016})},anchor=north east},
其他
legend style={at={(1,1-|{axis cs:10891,2016})},anchor=south east},
还有其他可能性。
代码:
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
y=30pt,
enlarge y limits={abs=15pt},
xbar,
y axis line style = {opacity = 0},
axis x line = none,
tickwidth = 0pt,
%legend style={at={(1,1-|{axis cs:10891,2016})},anchor=north east},
legend style={at={(1,1-|{axis cs:10891,2016})},anchor=south east},
symbolic y coords = {2016,2009},
ytick = data,
nodes near coords
]
\addplot [color=black,fill=red] coordinates { (8518,2009) (10891,2016) };
\addplot [color=black,fill=blue] coordinates { (5827,2009) (5845,2016) };
\legend{DEB,LMR}
\end{axis}
\end{tikzpicture}
\end{document}