![pgfplots 堆积条形图:到轴的距离,条形之间的填充](https://linux22.com/image/247118/pgfplots%20%E5%A0%86%E7%A7%AF%E6%9D%A1%E5%BD%A2%E5%9B%BE%EF%BC%9A%E5%88%B0%E8%BD%B4%E7%9A%84%E8%B7%9D%E7%A6%BB%EF%BC%8C%E6%9D%A1%E5%BD%A2%E4%B9%8B%E9%97%B4%E7%9A%84%E5%A1%AB%E5%85%85.png)
我有以下图表:
\documentclass{article}
\usepackage{xcolor}
\usepackage{tikz}
\usepackage{pgfplots}
\definecolor{transfertoserver}{HTML}{D7191C}
\definecolor{database}{HTML}{FDAE61}
\definecolor{transfertoclient}{HTML}{ABDDA4}
\definecolor{rendering}{HTML}{2B83BA}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[xbar stacked,
legend style={legend columns=4,at={(0,-0.35)},anchor=north west,draw=none},
ytick={0,1},
axis y line*=none,
axis x line*=bottom,
tick label style={font=\footnotesize},
legend style={font=\footnotesize},
label style={font=\footnotesize},
xtick={0,5,10,15,20,25},
width=.8\textwidth,
height=4cm,
bar width=6mm,
xlabel={Time in Seconds},
yticklabels={Database Optimizations, Reference Timing},
xmin=0,
xmax=25,
area legend,
enlarge y limits=0.3,
]
\addplot[transfertoserver,fill=transfertoserver] coordinates
% Transfer
{(0.38,0) (0.38,1)};
\addplot[database,fill=database] coordinates
% Database
{(2.4,0) (9.66,1)};
\addplot[transfertoclient,fill=transfertoclient] coordinates
% Transfer
{(0.23,0) (0.23,1)};
\addplot[rendering,fill=rendering] coordinates
% Rendering
{(14.66,0) (14.66,1)};
\legend{Transfer,Database,Transfer,Rendering}
\end{axis}
\end{tikzpicture}
\caption{Performance Benefit by Database Optimizations}
\label{fig:performance:database}
\end{figure}
\end{document}
但是我希望 x 轴和底部条之间有几毫米的距离。我可以通过放大 y 限制=0.2 来实现这一点。但是我必须为每个数字找到一个匹配因子。我想定义一个绝对测量值。另一件事是我怎样才能让条形图彼此更接近?
在一个问题中执行此操作以避免发送过多垃圾邮件。
答案1
您应该指定一个y
具有绝对维度的值(例如y=8mm
),其中该值应等于bar width
加上您想要的条形之间的间隙。因此,如果您的bar width
值为6mm
,y=8mm
则将为您提供2mm
条形之间的间隙。要获得 x 轴和条形之间的相同间隙,您可以添加enlarge y limits={abs=<value>}
,其中<value>
应为0.5 + 0.5*(y - bar width) / y
,因此在本例中为0.5 + 0.5 * (8-6)/8 = 0.625
。如果您只想要条形和轴之间一半的间隙,请设置 enlarge y limits={abs=0.5}
。
\documentclass{article}
\usepackage{xcolor}
\usepackage{tikz}
\usepackage{pgfplots}
\definecolor{transfertoserver}{HTML}{D7191C}
\definecolor{database}{HTML}{FDAE61}
\definecolor{transfertoclient}{HTML}{ABDDA4}
\definecolor{rendering}{HTML}{2B83BA}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
xbar stacked,
legend style={
legend columns=4,
at={(xticklabel cs:0.5)},
anchor=north,
draw=none
},
ytick=data,
axis y line*=none,
axis x line*=bottom,
tick label style={font=\footnotesize},
legend style={font=\footnotesize},
label style={font=\footnotesize},
xtick={0,5,10,15,20,25},
width=.8\textwidth,
bar width=6mm,
xlabel={Time in Seconds},
yticklabels={Database Optimizations, Reference Timing, Something Else},
xmin=0,
xmax=25,
area legend,
y=8mm,
enlarge y limits={abs=0.625},
]
\addplot[transfertoserver,fill=transfertoserver] coordinates
% Transfer
{(0.38,0) (0.38,1) (5,2)};
\addplot[database,fill=database] coordinates
% Database
{(2.4,0) (9.66,1)(5,2)};
\addplot[transfertoclient,fill=transfertoclient] coordinates
% Transfer
{(0.23,0) (0.23,1)(5,2)};
\addplot[rendering,fill=rendering] coordinates
% Rendering
{(14.66,0) (14.66,1)(5,2)};
\legend{Transfer,Database,Transfer,Rendering}
\end{axis}
\end{tikzpicture}
\caption{Performance Benefit by Database Optimizations}
\label{fig:performance:database}
\end{figure}
\end{document}