条形之间无空格,符号 x,多个图

条形之间无空格,符号 x,多个图

我想排版一个ybar图表,其中包含(可能)多个图、符号 x 坐标和指定宽度,以便条形图之间没有空格,也不会重叠。每个条形图都应该有一个节点来说明其高度(y 值)。

这类似于上一个问题,只是文档说ybar interval不能很好地与 配合使用node near coords

因此,我尝试bar width从其他参数(即总宽度、x 坐标数和绘图数)中得出。将其设置为width/(nbCoord*nbPlots)不起作用。

首先,必须使用 来抑制条形之间的跳跃bar shift auto={0pt}。同时,条形似乎向右移动,以便相对于标签居中。因此(?),我们必须提供额外的空间,其中enlarge x limits = {abs=XX}XX一组列宽度的一半(即bar width乘以图数除以 2)。

问题在于理解如何将这个宽度考虑在内,以便能够bar width从指定的总宽度中推断出。有了 3 个图,事情似乎合乎逻辑:设置bar width=13mm给出XX=19.5mm。绘制 9 个条形图乘以 3 应该得到 9*3*13mm + 19.5mm = 370.5mm,事实上,以下代码很好地对齐了

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}

\begin{tikzpicture}
\begin{axis}[width=370.5mm, height=35mm,
              enlarge y limits = false, enlarge x limits={abs=19.5mm}, ybar, bar shift auto={0pt}, fill, hide y axis, nodes near coords,
              axis x line*=none, xtick=data, tick style={draw opacity=0},
              bar width={13mm},
              symbolic x coords = {{[0,2[},{[2,4[},{[4,6[},{[6,8[},{[8,10[},{[10,12[},{[12,14[},{[14,16[},{[16,18[}},
              %xmin={[0,2[}, xmax={dummy},
              %every node near coord/.append style={anchor=middle},
              x tick label style={font=\scriptsize}, node near coord style={font=\scriptsize}]

\addplot coordinates {
({[0,2[}, 0 )
({[2,4[}, 0 )
({[4,6[}, 4 )
({[6,8[}, 2 )
({[8,10[}, 11 )
({[10,12[}, 3 )
({[12,14[}, 6 )
({[14,16[}, 8 )
({[16,18[}, 2 )
};

\addplot coordinates {
({[0,2[}, 0 )
({[2,4[}, 0 )
({[4,6[}, 4 )
({[6,8[}, 2 )
({[8,10[}, 11 )
({[10,12[}, 3 )
({[12,14[}, 6 )
({[14,16[}, 8 )
({[16,18[}, 2 )
};

\addplot coordinates {
({[0,2[}, 0 )
({[2,4[}, 0 )
({[4,6[}, 4 )
({[6,8[}, 2 )
({[8,10[}, 11 )
({[10,12[}, 3 )
({[12,14[}, 6 )
({[14,16[}, 8 )
({[16,18[}, 2 )
};

\end{axis}
\end{tikzpicture}
\end{document}

上述代码的结果

但是,对 2 个系列的钢筋采用相同的公式,得出的XX=13mm宽度为 9*2*13mm + 13mm = 247mm,但有轻微重叠:

具有新值的相同代码

仅使用一组条形,其XX=6.5mm宽度为 9*1*13mm+6.5mm = 123.5mm,重叠更大。

计算的值enlarge x limits似乎是正确的,因为第一个和最后一个条每次都与图的边界完美对齐。

但是对于整个宽度,有些东西需要考虑,但我现在看不到。那会是什么呢?

答案1

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar=0pt, bar width=10mm,
width=9*\numplots*\pgfplotbarwidth, height=35mm, scale only axis,
enlarge x limits={abs=0.5*\numplots*\pgfplotbarwidth}, enlarge y limits=false, 
hide y axis, 
nodes near coords,
axis x line*=none, xtick=data, tick style={draw=none},
symbolic x coords={{[0,2[},{[2,4[},{[4,6[},{[6,8[},{[8,10[},{[10,12[},{[12,14[},{[14,16[},{[16,18[}},
x tick label style={font=\scriptsize}, node near coord style={font=\scriptsize},
]
\addplot coordinates {({[0,2[}, 0 ) ({[2,4[}, 0 ) ({[4,6[}, 4 ) ({[6,8[}, 2 ) ({[8,10[}, 11 ) ({[10,12[}, 3 ) ({[12,14[}, 6 ) ({[14,16[}, 8 ) ({[16,18[}, 2 ) };
\addplot coordinates {({[0,2[}, 0 ) ({[2,4[}, 0 ) ({[4,6[}, 4 ) ({[6,8[}, 2 ) ({[8,10[}, 11 ) ({[10,12[}, 3 ) ({[12,14[}, 6 ) ({[14,16[}, 8 ) ({[16,18[}, 2 ) };
\addplot coordinates {({[0,2[}, 0 ) ({[2,4[}, 0 ) ({[4,6[}, 4 ) ({[6,8[}, 2 ) ({[8,10[}, 11 ) ({[10,12[}, 3 ) ({[12,14[}, 6 ) ({[14,16[}, 8 ) ({[16,18[}, 2 ) };
\end{axis}
\end{tikzpicture}
\end{document}

包含 27 个不同高度和标签的条形图

相关内容