PGFPLOTS 包图例空条目向左移动

PGFPLOTS 包图例空条目向左移动

我制作了一个在同一个 x 轴上有 2 个 y 轴的图。一个在左边,一个在右边。在此处输入图片描述

这就是我使用的代码:

\documentclass{standalone}

\usepackage{siunitx}    
\usepackage{pgfplots}
\usetikzlibrary{pgfplots.groupplots}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes}
\pgfplotsset{width=10cm,compat=1.3}
    \begin{document}
        \begin{tikzpicture}
        \begin{axis}[
            /pgf/number format/1000 sep={},
            axis y line*=left,
            width = \textwidth,
            height =  10 cm,
            xlabel={Something},
            ylabel={Something},         
            grid=none,
            major grid style={gray!95},
            ]%
            %
            \addplot [line width=1pt, solid,color=black, mark=none]
            table[x=x,y=y, col sep=comma] {Data1.csv};
            \label{Plot1}
    
            \addplot [color=black, mark=*, only marks,mark size=2pt]
            table[x=x,y=y, col sep=comma] {Data2.csv};
            \label{Plot2}
            
            \addplot [color=black, mark=diamond*, only marks,mark size=2pt]
            table[x=x,y=y, col sep=comma] {Data3.csv};
            \label{Plot3}
            
        \end{axis}
        \begin{axis}[
            %   /pgf/number format/000 sep={},
            axis y line*=right,
            axis x line = none,
            width = \textwidth,
            height =  10 cm,
            ylabel={Something else},            
            ymin = 0,
            ymax = 18,
            ytick align = outside,
            ytick pos = right,
            ylabel near ticks,
            grid=none,
            major grid style={gray!95},
            legend style={font=\small},
            legend columns=1,
            legend cell align={left},
            ]%
            \addlegendimage{empty legend}\addlegendentryexpanded{Headline 1:}
            \addplot[line width=1pt,dashed,color=black, mark=none]
            table[x=x,y=y, col sep=comma] {Data4.csv};
            \addlegendentry{Model prediction}
            
            \addplot[color=black, mark=square*, only marks,mark size=2pt]
            table[x=x,y=y, col sep=comma] {Data5.csv};
            \addlegendentry{Experiment 1}
            
            \addplot[color=black, mark=triangle*, only marks,mark size=2pt]
            table[x=x,y=ý, col sep=comma] {Data6.csv};
            \addlegendentry{Experiment 2}
            
            \addlegendimage{empty legend}\addlegendentryexpanded{Headline 2:}
            \addlegendimage{/pgfplots/refstyle=Plot1}\addlegendentryexpanded{Model prediction}
            \addlegendimage{/pgfplots/refstyle=Plot2}\addlegendentryexpanded{Experiment 1}
            \addlegendimage{/pgfplots/refstyle=Plot3}\addlegendentryexpanded{Experiment 2}
        \end{axis}
        \end{tikzpicture}
    \end{document}

让我困扰的是图例中标题的对齐方式。我知道“空条目”占用了文本的空间。但是有没有办法将图例中的标题条目向左移动?

\addlegendimage{empty entry}仅供参考:在之前不使用\addlegendentryexpanded会导致顺序混淆,并且标题会根据实际数据分配一个标记。

答案1

你可以使用https://tex.stackexchange.com/a/204519/36296添加标题而不是图例图像:

\documentclass{standalone}

\usepackage{siunitx}    
\usepackage{pgfplots}
\usetikzlibrary{pgfplots.groupplots}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes}
\pgfplotsset{width=10cm,compat=1.3}

\pgfplotsset{
    legend image with text/.style={
        legend image code/.code={%
            \node[anchor=center] at (0.0cm,0cm) {#1};
        }
    },
}

    \begin{document}
        \begin{tikzpicture}
        \begin{axis}[
            /pgf/number format/1000 sep={},
            axis y line*=left,
            width = \textwidth,
            height =  10 cm,
            xlabel={Something},
            ylabel={Something},         
            grid=none,
            major grid style={gray!95},
            ]%
            %
            \addplot [line width=1pt, solid,color=black, mark=none]
            {x};
            \label{Plot1}
    
            \addplot [color=black, mark=*, only marks,mark size=2pt]
            {x};
            \label{Plot2}
            
            \addplot [color=black, mark=diamond*, only marks,mark size=2pt]
            {x};
            \label{Plot3}
            
        \end{axis}
        \begin{axis}[
            %   /pgf/number format/000 sep={},
            axis y line*=right,
            axis x line = none,
            width = \textwidth,
            height =  10 cm,
            ylabel={Something else},            
            ymin = 0,
            ymax = 18,
            ytick align = outside,
            ytick pos = right,
            ylabel near ticks,
            grid=none,
            major grid style={gray!95},
            legend style={font=\small},
            legend columns=1,
            legend cell align={left},
            ]%
            \addlegendimage{legend image with text=\makebox[0pt][l]{Headline 1:}}
            \addlegendentry{}
            \addplot[line width=1pt,dashed,color=black, mark=none]
            {x};
            \addlegendentry{Model prediction}
            
            \addplot[color=black, mark=square*, only marks,mark size=2pt]
            {x};
            \addlegendentry{Experiment 1}
            
            \addplot[color=black, mark=triangle*, only marks,mark size=2pt]
            {x};
            \addlegendentry{Experiment 2}
            
            \addlegendimage{legend image with text=\makebox[0pt][l]{Headline 2:}}
            \addlegendentry{}
            \addlegendimage{/pgfplots/refstyle=Plot1}\addlegendentryexpanded{Model prediction}
            \addlegendimage{/pgfplots/refstyle=Plot2}\addlegendentryexpanded{Experiment 1}
            \addlegendimage{/pgfplots/refstyle=Plot3}\addlegendentryexpanded{Experiment 2}
        \end{axis}
        \end{tikzpicture}
    \end{document}

在此处输入图片描述

相关内容