在条形图中移动条形

在条形图中移动条形

如何使右侧的条形图出现在左侧条形图的旁边,以使其看起来不那么愚蠢?

在此处输入图片描述

图像的 LateX 代码:

\documentclass{report}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}
    % Data
    %----------------------------------------
    \pgfplotstableread[row sep=\\,col sep=&]{
        descr & DCPT & RPT            \\
        Description1     & 0.664  & 0.583 \\
        Description2     & 0.471  & 0.10  \\
        }\mydata

    % Plot
    %----------------------------------------
    \begin{figure}[!htb]
    \centering
    \begin{tikzpicture}
        \begin{axis}[
                ybar = 7pt,
                symbolic x coords={Description1, Description2},
                xticklabel style={rotate=45},
                xtick=data,
                nodes near coords,
                nodes near coords align={vertical},
                ymin=0, ymax=0.8,
            ]
            \addplot table[x=descr,y=DCPT]{\mydata};
            \addplot table[x=descr,y=RPT]{\mydata};
            \legend{Data1, Data2}
        \end{axis}
        \end{tikzpicture}
        \caption{Caption.}
        \label{fig:statistics}
    \end{figure}
\end{document}

答案1

您可以使用 键将 x 轴的范围扩大一定倍数enlarge x limits条形图

\documentclass{report}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}
    % Data
    %----------------------------------------
    \pgfplotstableread[row sep=\\,col sep=&]{
        descr & DCPT & RPT            \\
        Description1     & 0.664  & 0.583 \\
        Description2     & 0.471  & 0.10  \\
        }\mydata

    % Plot
    %----------------------------------------
    \begin{figure}[!htb]
    \centering
    \begin{tikzpicture}
        \begin{axis}[
                ybar = 7pt,
                symbolic x coords={Description1, Description2},
                xticklabel style={rotate=45},
                xtick=data,
                enlarge x limits = 0.5,
                nodes near coords,
                nodes near coords align={vertical},
                ymin=0, ymax=0.8,
            ]
            \addplot table[x=descr,y=DCPT]{\mydata};
            \addplot table[x=descr,y=RPT]{\mydata};
            \legend{Data1, Data2}
        \end{axis}
        \end{tikzpicture}
        \caption{Caption.}
        \label{fig:statistics}
    \end{figure}
\end{document}

相关内容