Pgfplots:直方图中的 xbar 问题

Pgfplots:直方图中的 xbar 问题

我想创建一个直方图,其中包含相同颜色的分组条形,并且间隔均匀(任意)y-values。 我设法做到了这一点:

在此处输入图片描述

现在,我想将自定义ytick标签放在每个组的中心,但我只是不明白 PGFPlots 如何计算这些值,而且我不想通过反复试验来修改坐标。

有想法吗?

======

平均能量损失

 \documentclass{beamer}
 \usepackage{pgfplots}

 \begin{document}
 \begin{frame}{Title}

 How to put an ylabel in the exact center of each group of bars? (For that example, that would give coords like (0,55), (0,25), (0,-10) and (0,-50).)

 \begin{center}
    \begin{tikzpicture}
    \begin{axis}[
       enlarge y limits=4,
       xbar,
       xmin=0,
       bar width=7pt,
    ]
       \addplot[fill=blue!20] coordinates { (0.387,0) };
       \addplot[fill=blue!20] coordinates { (0.199,0) };
       \addplot[fill=blue!20] coordinates { (0.192,0) };
       \addplot[fill=green!20] coordinates { (0.145,5) };
       \addplot[fill=green!20] coordinates { (0.142,5) };
       \addplot[fill=green!20] coordinates { (0.137,5) };
       \addplot[fill=green!20] coordinates { (0.132,5) };
       \addplot[fill=green!20] coordinates { (0.131,5) };
       \addplot[fill=red!20] coordinates { (0.104,10) };
       \addplot[fill=red!20] coordinates { (0.103,10) };
       \addplot[fill=red!20] coordinates { (0.098,10) };
       \addplot[fill=orange!20] coordinates { (0.143,15) };
       \addplot[fill=orange!20] coordinates { (0.141,15) };
       \addplot[fill=orange!20] coordinates { (0.137,15) };
       \addplot[fill=orange!20] coordinates { (0.120,15) };
       \end{axis}
    \end{tikzpicture}
 \end{center}

 \end{frame}
 \end{document}

答案1

如果您将选项添加xbar\addplot而不是 选项axis,则条形图将放置在给定的 y 值处。因此,您可以对\addplot每个组使用一个具有不同 y 值的条形图。无需猜测 y 值,因为您为组选择了中间值。例如,如果条形图位于y = 0,1,2,则将 放置ytick在 处y = 1

输出

 \documentclass{beamer}
 \usepackage{pgfplots}

 \begin{document}
 \begin{frame}{Title}

 How to put an ylabel in the exact center of each group of bars? (For that example, that would give coords like (0,55), (0,25), (0,-10) and (0,-50).)

 \begin{center}
    \begin{tikzpicture}
    \begin{axis}[
       enlarge y limits=0.1,
       xmin=0,
       bar width=5pt,
       ytick={1,6,11,15.5},
       yticklabels={foo,bar,baz,foobar},
       tickwidth=0pt
    ]
       \addplot[xbar,fill=blue!20] coordinates { (0.387,0)(0.199,1)(0.192,2) };
       \addplot[xbar,fill=green!20] coordinates { (0.145,4)(0.142,5)(0.137,6)(0.132,7)(0.131,8) };
       \addplot[xbar,fill=red!20] coordinates { (0.104,10)(0.103,11)(0.098,12) };
       \addplot[xbar,fill=orange!20] coordinates { (0.143,14)(0.141,15)(0.137,16)(0.120,17) };
       \end{axis}
    \end{tikzpicture}
 \end{center}

 \end{frame}
 \end{document}

答案2

像这样吗?

在此处输入图片描述

\documentclass{beamer}
\usepackage{pgfplots}
    \pgfplotsset{compat=1.15}

 \begin{document}
 \begin{frame}{Title}

 How to put an ylabel in the exact center of each group of bars? (For that example, that would give coords like (0,55), (0,25), (0,-10) and (0,-50).)

 \begin{center}
    \begin{tikzpicture}
    \begin{axis}[
       enlarge y limits=4,
       xbar,
       xmin=0,
       bar width=7pt,
       ytick=\empty,
       extra y ticks={-45,-10,25,56},
       extra y tick labels={$-50$, $-10$, $25$, $55$},
    ]
       \addplot[fill=blue!20] coordinates { (0.387,0) };
       \addplot[fill=blue!20] coordinates { (0.199,0) };
       \addplot[fill=blue!20] coordinates { (0.192,0) };
       \addplot[fill=green!20] coordinates { (0.145,5) };
       \addplot[fill=green!20] coordinates { (0.142,5) };
       \addplot[fill=green!20] coordinates { (0.137,5) };
       \addplot[fill=green!20] coordinates { (0.132,5) };
       \addplot[fill=green!20] coordinates { (0.131,5) };
       \addplot[fill=red!20] coordinates { (0.104,10) };
       \addplot[fill=red!20] coordinates { (0.103,10) };
       \addplot[fill=red!20] coordinates { (0.098,10) };
       \addplot[fill=orange!20] coordinates { (0.143,15) };
       \addplot[fill=orange!20] coordinates { (0.141,15) };
       \addplot[fill=orange!20] coordinates { (0.137,15) };
       \addplot[fill=orange!20] coordinates { (0.120,15) };
       \end{axis}
    \end{tikzpicture}
 \end{center}

 \end{frame}
 \end{document}

相关内容