如何使用 pgfplots 显示条形图中条形之间的差异?

如何使用 pgfplots 显示条形图中条形之间的差异?

我正在尝试使用 pgfplots 重新创建一个如下所示的图表(只有两组):

在此处输入图片描述

到目前为止我已经能够制作出这个:

在此处输入图片描述

以下是我编写的代码:

\begin{center}
    \begin{tikzpicture}
        \begin{axis}[
            axis x line*=bottom, % show bottom x-axis and dont remove tick marks
            axis y line=none,
            symbolic x coords={North,South},
            xtick=data,
            scaled y ticks = false,
            enlargelimits=0.5,
            bar width=50pt,
            bar shift=-100pt,
            nodes near coords,
            nodes near coords style={
                anchor=south,
            },
            every node near coord/.append style={
                yshift=-20pt,
                font=\scriptsize
            },
            point meta=explicit symbolic,
            ybar % make bar chart
        ]
        \addplot[draw=black,fill=gray!30]
            coordinates {
                (North,1300) [1300]
                (South,1600) [1600]
        };
        \addplot[draw=black,fill=gray!50]
            coordinates {
                (North,1750) [1750]
                (South,1500) [1500]
        };
        \end{axis}
        \begin{axis}[
            axis lines = none,
            symbolic x coords={North,South},
            xtick=data,
            scaled y ticks = false,
            enlargelimits=0.5,
            bar width=50pt,
            bar shift=100pt,
            nodes near coords,
            nodes near coords style={
                anchor=south,
            },
            every node near coord/.append style={
                yshift=-20pt,
                font=\scriptsize
            },
            point meta=explicit symbolic,
            ybar % make bar chart
        ]
        \addplot+[draw=black,fill=green]
            coordinates {
                (North,1750) [300]
                (South,1600) [250]
        };
        \end{axis}
    \end{tikzpicture}
\end{center}

任何帮助都将不胜感激,谢谢!

答案1

这是实现这个目标的方法。我并不是说这是最好的可能性,可以想象有人可能会想出类似这样的东西这个很好的答案。不过,我可以在这里提供以下内容:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{center}
    \begin{tikzpicture}
    \pgfplotstableread[header=true]{
        dir   L    R
        North 1300 1600
        South 1750 1500
        East  1100 1500
        West  2050 2250
    }\honeydat
    \pgfplotstablegetrowsof{\honeydat}
    \pgfmathtruncatemacro{\numrows}{\pgfplotsretval-1}
        \begin{axis}[width=12cm,ybar,
            axis x line*=bottom, % show bottom x-axis and dont remove tick marks
            axis y line=none,
            scaled y ticks = false,
            enlarge x limits={abs=1.5cm},
            bar width=30pt,
            xtick={0,...,3},
            xticklabels={North,South,East,West},
            nodes near coords,
            nodes near coords style={
                anchor=south,
            },
            every node near coord/.append style={
                yshift=-20pt,
                font=\scriptsize
            },
        ]
        \addplot[draw=black,fill=gray!30] table[x expr=\coordindex,y=L] \honeydat;
        \addplot[draw=black,fill=gray!50] table[x expr=\coordindex,y=R] \honeydat;
        \pgfplotsinvokeforeach{0,...,\numrows}{%
         \pgfplotstablegetelem{#1}{L}\of{\honeydat}%
         \edef\myL{\pgfplotsretval}%
         \pgfplotstablegetelem{#1}{R}\of{\honeydat}%
         \edef\myR{\pgfplotsretval}%
         \ifdim\myL pt<\myR pt
          \pgfmathtruncatemacro{\mydiff}{\myR-\myL}
          \edef\temp{\noexpand\path[fill=green!20] (axis cs:#1,\myR)
            rectangle ([xshift=-30pt]axis cs:#1,\myL);
            \noexpand\draw[densely dashed,green!70!black] 
            (axis cs:#1,\myR) -- ++ (-30pt,0pt) 
                node[pos=0.5,above]{\mydiff}
              -- ([xshift=-30pt]axis cs:#1,\myL);          
            }
         \else
          \pgfmathtruncatemacro{\mydiff}{\myL-\myR}
          \edef\temp{\noexpand\path[fill=orange!20] (axis cs:#1,\myL)
            rectangle ([xshift=30pt]axis cs:#1,\myR);
            \noexpand\draw[densely dashed,orange] 
            (axis cs:#1,\myL) -- ++ (30pt,0pt) 
                node[pos=0.5,above]{\mydiff}
              -- ([xshift=30pt]axis cs:#1,\myR);           
            }
         \fi
         \temp}
        \end{axis}
    \end{tikzpicture}
\end{center}
\end{document}

在此处输入图片描述

如果您还想允许更大的值,则需要使用fpu循环中完成的计算。

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.16}
\newcommand{\PgfmathtruncatemacroFPU}[2]{\begingroup%
\pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed}%
\pgfmathtruncatemacro{#1}{#2}%
\pgfmathsmuggle#1\endgroup}
\begin{document}
\begin{center}
    \begin{tikzpicture}
    \pgfplotstableread[header=true]{
        dir   L    R
        North 130000 160000
        South 175000 150000
        East  110000 150000
        West  205000 225000
    }\honeydat
    \pgfplotstablegetrowsof{\honeydat}
    \pgfmathtruncatemacro{\numrows}{\pgfplotsretval-1}
        \begin{axis}[width=12cm,ybar,
            axis x line*=bottom, % show bottom x-axis and dont remove tick marks
            axis y line=none,
            scaled y ticks = false,
            enlarge x limits={abs=1.5cm},
            bar width=30pt,
            xtick={0,...,3},
            xticklabels={North,South,East,West},
            nodes near coords,
            nodes near coords style={
                anchor=south,
            },
            every node near coord/.append style={
                yshift=-20pt,
                font=\scriptsize
            },
        ]
        \addplot[draw=black,fill=gray!30] table[x expr=\coordindex,y=L] \honeydat;
        \addplot[draw=black,fill=gray!50] table[x expr=\coordindex,y=R] \honeydat;
        \pgfplotsinvokeforeach{0,...,\numrows}{%
         \pgfplotstablegetelem{#1}{L}\of{\honeydat}%
         \edef\myL{\pgfplotsretval}%
         \pgfplotstablegetelem{#1}{R}\of{\honeydat}%
         \edef\myR{\pgfplotsretval}%
         \PgfmathtruncatemacroFPU{\mysign}{sign(\myR-\myL)}
         \ifnum\mysign>0
          \PgfmathtruncatemacroFPU{\mydiff}{\myR-\myL}
          \edef\temp{\noexpand\path[fill=green!20] (axis cs:#1,\myR)
            rectangle ([xshift=-30pt]axis cs:#1,\myL);
            \noexpand\draw[densely dashed,green!70!black] 
            (axis cs:#1,\myR) -- ++ (-30pt,0pt) 
                node[pos=0.5,above]{\noexpand\pgfmathprintnumber{\mydiff}}
              -- ([xshift=-30pt]axis cs:#1,\myL);          
            }
         \else
          \PgfmathtruncatemacroFPU{\mydiff}{\myL-\myR}
          \edef\temp{\noexpand\path[fill=orange!20] (axis cs:#1,\myL)
            rectangle ([xshift=30pt]axis cs:#1,\myR);
            \noexpand\draw[densely dashed,orange] 
            (axis cs:#1,\myL) -- ++ (30pt,0pt) 
                node[pos=0.5,above]{\noexpand\pgfmathprintnumber{\mydiff}}
              -- ([xshift=30pt]axis cs:#1,\myR);           
            }
         \fi
         \temp}
        \end{axis}
    \end{tikzpicture}
\end{center}
\end{document}

在此处输入图片描述

相关内容