条形图的 x 轴标签重复两次

条形图的 x 轴标签重复两次

问题 1:我试图生成一个条形图,每个图表有 3 个条形。但从下面的输出中可以看出,我的 x 轴标签重复了两次。X 轴标签 = {3-6, 6-12, 12-24}

问题 2:当我在 overleaf 上编译代码时,我得到了位于中心的完整图片,而当我将代码粘贴到另一个文档中时,它占用了大量空间(因为整个 pdf 页面都被复制到另一个文档中)。有没有办法,当我编译项目时,我只会得到一个仅限于该图像的 pdf 下载?

问题2输出

在此处输入图片描述

问题 1 输出:

在此处输入图片描述

乳胶代码:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{pgf}
\usepackage{tikz} % Required for drawing custom shapes
\usetikzlibrary{shapes,arrows,automata}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.17}
\usetikzlibrary{patterns}

\begin{document}

    \begin{figure}
        \centering
        \begin{tikzpicture}
            \begin{groupplot}[
                    legend columns=-1,
                    %bar width=1pt, % Width of the bar
                    %, % Distance between the centers of the bars
                    symbolic x coords={3-6, 6-12, 12-24},
                     enlarge x limits={abs=0.6cm},
                     width=1.2\textwidth,
                     %xtick={3,6,12,24},
                    height=0.8\textheight,
                    ymin=0,
                    ymax=15,
                    nodes near coords,
    every node near coord/.append style={font=\scriptsize},
   nodes near coords align={vertical},
                     xlabel={x},
                     ylabel={y},
                     % The distance between the center of the first bar and the left edge
                    %enlarge y limits=false,
                    legend entries={{\color{olive}{\tiny Random}},{\color{orange}{\tiny ++Cost}},{\color{teal}{\tiny ++FTE}}},
                    legend to name=CombinedLegendBar,
                    footnotesize,
                    ybar legend,
                    legend image code/.code={%
                     \draw[#1] (0cm,-0.1cm) rectangle (0.6cm,0.1cm);
                         },
                    % area legend, % This is the alternate option
                    group style={
                    group size=3 by 1,
                    xlabels at=edge bottom,
                    ylabels at=edge left}]
                \nextgroupplot[title={\scriptsize MADDPG}]
                    \addplot[ybar, fill=olive] coordinates {  (3-6, 6.886)};
                    \addplot[ybar, fill=orange] coordinates { (6-12, 8.501)};
                    \addplot[ybar, fill=teal] coordinates {  (12-24, 10.179)};
                    %\addplot[ybar, fill=gray] coordinates {  (4, 11.14)};
                    % \addplot[ybar, pattern=north east lines] coordinates {  (5, 15.001)};

                \nextgroupplot[title={\scriptsize MATD3}]
                     \addplot[ybar, fill=olive] coordinates {  (3-6, 6.886)};
                    \addplot[ybar, fill=orange] coordinates { (6-12, 8.501)};
                    \addplot[ybar, fill=teal] coordinates {  (12-24, 10.179)};
                   % \addplot[ybar, fill=gray] coordinates {  (4, 11.14)};
                    % \addplot[ybar, pattern=north east lines] coordinates {(5, 15.001)};

                \nextgroupplot[title={\scriptsize MASAC}]
            `       \addplot[ybar, fill=olive] coordinates {  (3-6, 6.886)};
                    \addplot[ybar, fill=orange] coordinates { (6-12, 8.501)};
                    \addplot[ybar, fill=teal] coordinates {  (12-24, 10.179)};
                   % \addplot[ybar, fill=gray] coordinates {  (4, 11.14)};
                    % \addplot[ybar, pattern=north east lines] coordinates {  (5, 15.001)};

            \end{groupplot}
        \end{tikzpicture}
        \ref{CombinedLegendBar}
        \caption{Triage++ Performance}
        \label{PlusPlusCombinedBar}
    \end{figure}
\end{document}

有人能帮我解决这个问题吗?

谢谢!

答案1

  • 您的问题不完全清楚,例如,如何将该图像插入到您的真实(最终)文档中?
  • 对于页面上的图形定位,请尝试使用放置位置选项,即\begin{figure}改写\begin{figure}[ht]\begin{figure}[!ht]
  • 按照下面的 MWE 中的方式清理您的groupplot设置,并稍微重新组织您的图形代码后,在本地 MiKTeX 安装以及 Overleaf 上的编译都会产生相同的结果。
  • 此次清理工作是根据我之前对您问题(无需添加xtick选项):

在此处输入图片描述

(红线表示页面布局)

\documentclass{article}
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.18}

\begin{document}
    \begin{figure}[ht]
\begin{tikzpicture}
    \begin{groupplot}[
group style={
    group size=3 by 1,
    ylabels at=edge left,
    horizontal sep=6mm,
            },
%
    width=0.27\linewidth, %height=4cm,
    scale only axis,
    title style = {font=\small},
%
    enlarge x limits=0.3,
    xlabel={x},     ylabel={y},
    symbolic x coords={3-6, 6-12, 12-24},
    ytick={0,2,...,12},
    ticklabel style={font=\footnotesize},
    ymin=0,     ymax=12,
%
    nodes near coords,
    every node near coord/.append style={yshift=-3pt, font=\scriptsize},
%
    legend entries = {\color{olive}{Random},
                      \color{orange}{++Cost},
                      \color{teal}{++FTE}},
    legend to name=CombinedLegendBar,
    legend image code/.code={\draw[#1] (0cm,-0.1cm) rectangle (0.6cm,0.1cm);},
    legend style={at={(0.5,-0.33)},
                  below,
                  legend columns=-1,
                  font=\footnotesize,
                  /tikz/every even column/.append style={column sep=1em}
                  }
                    ]
\nextgroupplot[title=MADDPG]
    \addplot[ybar, fill=olive]  coordinates {  (3-6,   6.886) };
    \addplot[ybar, fill=orange] coordinates {  (6-12,  8.501) };
    \addplot[ybar, fill=teal]   coordinates {  (12-24,10.179) };

\nextgroupplot[title=MATD3]
    \addplot[ybar, fill=olive]  coordinates {  (3-6,   6.886) };
    \addplot[ybar, fill=orange] coordinates {  (6-12,  8.501) };
    \addplot[ybar, fill=teal]   coordinates {  (12-24,10.179) };

\nextgroupplot[title=MASAC]
    \addplot[ybar, fill=olive]  coordinates {  (3-6,   6.886) };
    \addplot[ybar, fill=orange] coordinates {  (6-12,  8.501) };
    \addplot[ybar, fill=teal]   coordinates {  (12-24,10.179) };
    \end{groupplot}
\end{tikzpicture}

    \ref{CombinedLegendBar}
\caption{Triage++ Performance}
\label{PlusPlusCombinedBar}
    \end{figure}
\end{document}

答案2

关于问题 1:添加带有符号标签的 xtick 命令(见下文)对我来说很有帮助。

关于问题 2:如果我理解正确的话,将文章类从文档替换为独立类对我来说就完成了(我必须删除图形环境+标题)

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{pgf}
\usepackage{tikz} % Required for drawing custom shapes
\usetikzlibrary{shapes,arrows,automata}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.17}
\usetikzlibrary{patterns}

\begin{document}

        \centering
        \begin{tikzpicture}
            \begin{groupplot}[
                    legend columns=-1,
                    %bar width=1pt, % Width of the bar
                    %, % Distance between the centers of the bars
                    symbolic x coords={3-6, 6-12, 12-24},
                     enlarge x limits={abs=0.6cm},
                     width=1.2\textwidth,
                    xtick={3-6, 6-12, 12-24},
                    height=0.8\textheight,
                    ymin=0,
                    ymax=15,
                    nodes near coords,
    every node near coord/.append style={font=\scriptsize},
   nodes near coords align={vertical},
                     xlabel={x},
                     ylabel={y},
                     % The distance between the center of the first bar and the left edge
                    %enlarge y limits=false,
                    legend entries={{\color{olive}{\tiny Random}},{\color{orange}{\tiny ++Cost}},{\color{teal}{\tiny ++FTE}}},
                    legend to name=CombinedLegendBar,
                    footnotesize,
                    ybar legend,
                    legend image code/.code={%
                     \draw[#1] (0cm,-0.1cm) rectangle (0.6cm,0.1cm);
                         },
                    % area legend, % This is the alternate option
                    group style={
                    group size=3 by 1,
                    xlabels at=edge bottom,
                    ylabels at=edge left}]
                \nextgroupplot[title={\scriptsize MADDPG}]
                    \addplot[ybar, fill=olive] coordinates {  (3-6, 6.886)};
                    \addplot[ybar, fill=orange] coordinates { (6-12, 8.501)};
                    \addplot[ybar, fill=teal] coordinates {  (12-24, 10.179)};
                    %\addplot[ybar, fill=gray] coordinates {  (4, 11.14)};
                    % \addplot[ybar, pattern=north east lines] coordinates {  (5, 15.001)};

                \nextgroupplot[title={\scriptsize MATD3}]
                     \addplot[ybar, fill=olive] coordinates {  (3-6, 6.886)};
                    \addplot[ybar, fill=orange] coordinates { (6-12, 8.501)};
                    \addplot[ybar, fill=teal] coordinates {  (12-24, 10.179)};
                   % \addplot[ybar, fill=gray] coordinates {  (4, 11.14)};
                    % \addplot[ybar, pattern=north east lines] coordinates {(5, 15.001)};

                \nextgroupplot[title={\scriptsize MASAC}]
            `       \addplot[ybar, fill=olive] coordinates {  (3-6, 6.886)};
                    \addplot[ybar, fill=orange] coordinates { (6-12, 8.501)};
                    \addplot[ybar, fill=teal] coordinates {  (12-24, 10.179)};
                   % \addplot[ybar, fill=gray] coordinates {  (4, 11.14)};
                    % \addplot[ybar, pattern=north east lines] coordinates {  (5, 15.001)};

            \end{groupplot}
        \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容