【问题标题】:Remove white space on right side of pgfplot删除 pgfplot 右侧的空白
【发布时间】:2022-01-16 18:15:19
【问题描述】:

目前我有以下数字(图):

我已将 pgfplots 放置在两个子图中,以便我可以添加标题。然而,问题是在图 3b 的右侧添加了一些空白。我正在使用 \hfill 选项,但仍然删除了这个空白。有人知道如何摆脱这个空白,使图 3b 的右侧与我的论文的边界对齐(由 text text text ...表示)

我的代码如下:

\documentclass[a4paper, 11pt]{article}
\usepackage[utf8x]{inputenc}
\usepackage[margin=2.5cm]{geometry}
\usepackage{subcaption}
\usepackage{float}
\usepackage{graphicx}
\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzset{font=\footnotesize}

\begin{document}

\begin{figure}[H]
\centering

\begin{subfigure}{0.49\textwidth}

\begin{tikzpicture}
    \begin{axis}[
        title={},
        xlabel={Epochs},
        ylabel={Rewards},
        ylabel near ticks,
        legend pos=south east,
        ymajorgrids=true,
        grid style=densely dotted,
        width=7cm,
        height=6cm,
        legend cell align={left},
        legend style={font=\footnotesize}
    ]
    \addplot[color = black, line width = 0.25mm, mark = *, mark size = 1] coordinates {
        (1, 1.5)(2, 1.3)
        };
    \addplot[color = black, line width = 0.25mm, mark = none, samples = 2, dashed] {1.4}; % Constant line
    \legend{Plot1,}
    \end{axis}
    \begin{axis}[
        ylabel={Percentage (\%)},
        axis y line*=right,
        ylabel near ticks,
        ylabel style={rotate=-180},
        hide x axis,
        width=7cm,
        height=6cm,
        legend style={font=\footnotesize}
    ]
    \addplot[color = black, mark = *, mark size = 1] coordinates {
    (1, 0.5)(2, 0.6)
    }; 
    \addplot[color = black, line width = 0.25mm, mark = none, samples = 2, dashed] {0.53}; % Constant line
    \legend{Plot2,}
    \end{axis}
\end{tikzpicture}

\caption{Performance}
\label{Plot:}
\end{subfigure}\hfill
\begin{subfigure}{0.49\textwidth}

\begin{tikzpicture}
    \begin{axis}[
        title={},
        xlabel={Epochs},
        ylabel={Rewards},
        ylabel near ticks,
        legend pos=south east,
        ymajorgrids=true,
        grid style=densely dotted,
        width=7cm,
        height=6cm,
        legend cell align={left},
        legend style={font=\footnotesize}
    ]
    \addplot[color = black, line width = 0.25mm, mark = *, mark size = 1] coordinates {
        (1, 1.5)(2, 1.3)
        };
    \addplot[color = black, line width = 0.25mm, mark = none, samples = 2, dashed] {1.4}; % Constant line
    \legend{Plot1,}
    \end{axis}
\end{tikzpicture}

\caption{Action distribution}
\label{Plot:}
\end{subfigure}

\caption{Results}
\label{Plot:}
\end{figure}

\end{document}

【问题讨论】:

  • 你能制作一个可编译的minimal reproducible example吗?
  • 我怀疑你的7cm和.49\textwidth不一样,但是没有documentclass和packages,那只是猜测
  • 当然@samcarter_is_at_topanswers.xyz,我已经添加了文档类和包
  • 您测试过文档吗?它不编译
  • @samcarter_is_at_topanswers.xyz 很奇怪,当我在背面测试它时它确实编译了,我改变了几个包,你能再试一次吗?

标签: latex margin pgfplots


【解决方案1】:

问题是你的地块大约 7 厘米宽,你把它们放在 .49\textwidthsubfigure 中,所以你放在 subfigures 之间的 \hfill 只能看到 .49\textwidth 和根据这个添加填充空间。如果你想把地块移到右边的边界,你可以

a) 将\hfill 放入子图中(标题将不再居中)

b) 调整 .49\textwidth 以匹配绘图的实际宽度(大约 7 厘米)

(下面代码中的\fbox{...}只是为了可视化地块的大小)

\documentclass[a4paper, 11pt]{article}
\usepackage[utf8x]{inputenc}
\usepackage[margin=2.5cm,showframe]{geometry}
\usepackage{subcaption}
\usepackage{float}
\usepackage{graphicx}
\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzset{font=\footnotesize}

\begin{document}

\begin{figure}[H]
\centering

\begin{subfigure}{0.49\textwidth}

\fbox{\begin{tikzpicture}
    \begin{axis}[
        title={},
        xlabel={Epochs},
        ylabel={Rewards},
        ylabel near ticks,
        legend pos=south east,
        ymajorgrids=true,
        grid style=densely dotted,
        width=7cm,
        height=6cm,
        legend cell align={left},
        legend style={font=\footnotesize}
    ]
    \addplot[color = black, line width = 0.25mm, mark = *, mark size = 1] coordinates {
        (1, 1.5)(2, 1.3)
        };
    \addplot[color = black, line width = 0.25mm, mark = none, samples = 2, dashed] {1.4}; % Constant line
    \legend{Plot1,}
    \end{axis}
    \begin{axis}[
        ylabel={Percentage (\%)},
        axis y line*=right,
        ylabel near ticks,
        ylabel style={rotate=-180},
        hide x axis,
        width=7cm,
        height=6cm,
        legend style={font=\footnotesize}
    ]
    \addplot[color = black, mark = *, mark size = 1] coordinates {
    (1, 0.5)(2, 0.6)
    }; 
    \addplot[color = black, line width = 0.25mm, mark = none, samples = 2, dashed] {0.53}; % Constant line
    \legend{Plot2,}
    \end{axis}
\end{tikzpicture}}

\caption{Performance}
\label{Plot:}
\end{subfigure}
\begin{subfigure}{0.49\textwidth}
\hfill%
\fbox{\begin{tikzpicture}
    \begin{axis}[
        title={},
        xlabel={Epochs},
        ylabel={Rewards},
        ylabel near ticks,
        legend pos=south east,
        ymajorgrids=true,
        grid style=densely dotted,
        width=7cm,
        height=6cm,
        legend cell align={left},
        legend style={font=\footnotesize}
    ]
    \addplot[color = black, line width = 0.25mm, mark = *, mark size = 1] coordinates {
        (1, 1.5)(2, 1.3)
        };
    \addplot[color = black, line width = 0.25mm, mark = none, samples = 2, dashed] {1.4}; % Constant line
    \legend{Plot1,}
    \end{axis}
\end{tikzpicture}}%
\caption{Action distribution}
\label{Plot:}
\end{subfigure}
\caption{Results}
\label{Plot:}
\end{figure}

\end{document}

【讨论】:

    猜你喜欢
    • 2012-02-22
    • 2021-02-24
    • 2021-04-13
    • 1970-01-01
    • 1970-01-01
    • 2021-05-11
    • 2023-03-26
    • 2011-12-11
    • 1970-01-01
    相关资源
    最近更新 更多