Pgf 图和表格图例

Pgf 图和表格图例

我有一个图,我想在其中添加图例,该图例是线条和标记的组合。为此,我想替换(或定义新的)原始 pgf 图例类型。如前所述,在下面的 MWE 中,我想将图中的图例替换为类似于图形下方的图例,并使用与图形中相同的符号和线条。这可能吗?

\documentclass[10pt,border=0pt]{standalone}

\usepackage[T1]{fontenc}
\usepackage{lmodern}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotstableread{%
x0  y1  y2  y3  y4  y5  y6  
1   3.1 3.1 1.8 2.4 1.6 0.7 
3   3.2 2.8 1.8 2.2 1.7 0.5 
5   3.3 3.0 1.6 2.6 1.4 0.3 
7   3.1 3.0 1.2 2.7 1.5 0.2 
9   3.0 2.9 1.5 2.5 1.3 0.2
}{\testdata}

\usetikzlibrary{arrows}
\usetikzlibrary{plotmarks}

% mytick x interval=xmin:xstep:xmax
\pgfplotsset{mytick x interval/.style args={#1:#2:#3}{%
        xmin=#1,xmax=#3,xincrement={#1}{#2},
        xtick={#1,\xval,...,#3}
    },
    xincrement/.code 2 args={\pgfmathparse{#1+#2}\xdef\xval{\pgfmathresult}}
}

% mytick y interval=ymin:ystep:ymax
\pgfplotsset{mytick y interval/.style args={#1:#2:#3}{%
        ymin=#1,ymax=#3,yincrement={#1}{#2},
        ytick={#1,\yval,...,#3}
    },
    yincrement/.code 2 args={\pgfmathparse{#1+#2}\xdef\yval{\pgfmathresult}}
}

\pgfplotsset{every tick/.style={black,}}

\pgfplotsset{%
    compat=1.8,
    compat/show suggested version=false,
    mytick x interval=1:2:9,
    mytick y interval=0:0.5:4,
    tick label style={font=\footnotesize},
    label style={font=\footnotesize},
    xlabel={x-label},
    ylabel={y-label},
    %enlargelimits=false,
    legend style={%
        font=\footnotesize,
        legend cell align=left,
        legend pos=south east,
        nodes={inner xsep=2pt,inner ysep=0.4pt,text depth=0.15em},
    },
}

\begin{document}

\begin{tikzpicture}
\begin{axis}[%
    legend entries={%
        A,
        B,
        C,
        S1,
        S2,
    },
    mark options={fill=white,solid},
]
    \addlegendimage{thick,only marks,mark=square*} \label{c1}
    \addlegendimage{thick,only marks,mark=triangle*} \label{c2}
    \addlegendimage{thick,only marks,mark=*} \label{c3}
    \addlegendimage{thick} \label{l1}
    \addlegendimage{thick,dashed} \label{l2}
    %
    \addplot[thick,mark=square*] table[x=x0,y=y2] {\testdata}; \label{n1}
    \addplot[thick,dashed,mark=square*] table[x=x0,y=y1] {\testdata}; \label{n2}
    \addplot[thick,mark=triangle*] table[x=x0,y=y4] {\testdata}; \label{n3}
    \addplot[thick,dashed,mark=triangle*] table[x=x0,y=y3] {\testdata}; \label{n4}
    \addplot[thick,mark=*] table[x=x0,y=y6] {\testdata}; \label{n5}
    \addplot[thick,dashed,mark=*] table[x=x0,y=y5] {\testdata}; \label{n6}
\end{axis}
\end{tikzpicture} 

\begin{tabular}{llll}
  &          & S1       & S2 \\
  &          & \ref{l1} & \ref{l2} \\
A & \ref{c1} & \ref{n1} & \ref{n2} \\
B & \ref{c2} & \ref{n3} & \ref{n4} \\
C & \ref{c3} & \ref{n5} & \ref{n6} \\
\end{tabular}

\end{document}

在此处输入图片描述

答案1

我发现最好的方法是使用一个\usetikzlibrary{matrix}在其周围绘制边框的矩阵(...draw,)并引用轴...anchor=south east] at (rel axis cs: 0.97,0.03)

注意:要收紧边界框,您需要inner sep减少矩阵为零,但强制其为大于零的值节点,请参阅以下方括号内的代码\matrix(dict)

\documentclass[10pt,border=0pt]{standalone}

\usepackage[T1]{fontenc}
\usepackage{lmodern}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotstableread{%
x0  y1  y2  y3  y4  y5  y6  
1   3.1 3.1 1.8 2.4 1.6 0.7 
3   3.2 2.8 1.8 2.2 1.7 0.5 
5   3.3 3.0 1.6 2.6 1.4 0.3 
7   3.1 3.0 1.2 2.7 1.5 0.2 
9   3.0 2.9 1.5 2.5 1.3 0.2
}{\testdata}

\usetikzlibrary{arrows}
\usetikzlibrary{plotmarks}
\usetikzlibrary{matrix} %added

% mytick x interval=xmin:xstep:xmax
\pgfplotsset{mytick x interval/.style args={#1:#2:#3}{%
        xmin=#1,xmax=#3,xincrement={#1}{#2},
        xtick={#1,\xval,...,#3}
    },
    xincrement/.code 2 args={\pgfmathparse{#1+#2}\xdef\xval{\pgfmathresult}}
}

% mytick y interval=ymin:ystep:ymax
\pgfplotsset{mytick y interval/.style args={#1:#2:#3}{%
        ymin=#1,ymax=#3,yincrement={#1}{#2},
        ytick={#1,\yval,...,#3}
    },
    yincrement/.code 2 args={\pgfmathparse{#1+#2}\xdef\yval{\pgfmathresult}}
}

\pgfplotsset{every tick/.style={black,}}

\pgfplotsset{%
    compat=1.8,
    compat/show suggested version=false,
    mytick x interval=1:2:9,
    mytick y interval=0:0.5:4,
    tick label style={font=\footnotesize},
    label style={font=\footnotesize},
    xlabel={x-label},
    ylabel={y-label},
    %enlargelimits=false,
    legend style={%
        font=\footnotesize,
        legend cell align=left,
        legend pos=south east,
        nodes={inner xsep=2pt,inner ysep=0.4pt,text depth=0.15em},
    },
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[%
]
    %
    \addplot[thick,mark=square*] table[x=x0,y=y2] {\testdata}; \label{n1}
    \addplot[thick,dashed,mark=square*] table[x=x0,y=y1] {\testdata}; \label{n2}
    \addplot[thick,mark=triangle*] table[x=x0,y=y4] {\testdata}; \label{n3}
    \addplot[thick,dashed,mark=triangle*] table[x=x0,y=y3] {\testdata}; \label{n4}
    \addplot[thick,mark=*] table[x=x0,y=y6] {\testdata}; \label{n5}
    \addplot[thick,dashed,mark=*] table[x=x0,y=y5] {\testdata}; \label{n6}

    %legend images that aren't actually plotted
    \addlegendimage{thick, solid};\label{l1}
    \addlegendimage{thick, dashed};\label{l2}
    \addlegendimage{only marks, mark=square*};\label{c1}
    \addlegendimage{only marks, mark=triangle*};\label{c2}
    \addlegendimage{only marks, mark=*};\label{c3}
\end{axis}
\matrix(dict)[ inner sep=0pt,outer sep=0pt,draw,%necessary to move "border" in so it tightly bounds all nodes
matrix of nodes, %eliminates need to surround text with \node{<text>}
%nodes={draw},%draw boxes around all nodes (for debugging)
every node/.style={inner ysep=2pt,inner xsep=4pt},%need to set the "inner sep" of NODES to >0 so they are spaced
fill=white,
anchor=south east
] at (rel axis cs: 0.97,0.03){%places the "legend" so its southeast corner is just within the tick marks
    &          & S1       & S2 \\
    &          & \ref{l1} & \ref{l2} \\
A & \ref{c1} & \ref{n1} & \ref{n2} \\
B & \ref{c2} & \ref{n3} & \ref{n4} \\
C & \ref{c3} & \ref{n5} & \ref{n6} \\
};
\end{tikzpicture} 
\end{document}

输出: 用矩阵绘制的图例

相关内容