如果在 pgfplots 中使用标记索引,如何获取带有图例的标记详细信息?

如果在 pgfplots 中使用标记索引,如何获取带有图例的标记详细信息?

我有以下 MWE,其中第二个图仅显示选定的标记。结果,标记在图例条目中丢失,如图所示(案例 2)。如何在使用标记索引时将标记保留在图例条目中?

在此处输入图片描述

平均能量损失

\documentclass{standalone}
\usepackage{filecontents}
\usepackage{pgfplots}
\begin{filecontents*}{pgfplots.dat}
T   A  B  C
1.42857     6.616   6.697   6.782
1.40845     5.413   5.487   5.564
1.38889     4.544   4.612   4.684
1.36986     3.916   3.982   4.052
1.35135     3.469   3.534   3.603
1.33333     3.158   3.224   3.294
1.31579     2.954   3.022   3.094
1.29870     2.838   2.909   2.983
1.28205     2.795   2.870   2.949
1.26582     2.818   2.898   2.984
1.25000     2.905   2.992   3.085
1.23457     3.057   3.153   3.255
1.21951     3.278   3.385   3.499
1.20482     3.571   3.69    3.816
1.19048     3.922   4.052   4.188
1.17647     4.296   4.433   4.576
1.16279     4.645   4.783   4.927
1.14943     4.921   5.054   5.193
1.13636     5.085   5.208   5.335
1.12360     5.114   5.222   5.333
1.11111     5.007   5.097   5.19
1.09890     4.784   4.858   4.933
1.08696     4.477   4.536   4.596
1.07527     4.121   4.167   4.214
1.06383     3.742   3.778   3.815
1.05263     3.364   3.392   3.42
1.04167     3.001   3.022   3.043
1.03093     2.661   2.677   2.694
1.02041     2.35    2.362   2.375
1.01010     2.069   2.079   2.089
1.00000     1.819   1.827   1.834
\end{filecontents*}

\begin{document}
\begin{tikzpicture}
    \begin{semilogyaxis}[
        xlabel=x,
        ylabel=y,
        xmin=1.10,
        xmax=1.4,
        ymin=2.0,
        ymax=7.0,
        ]
        \addplot [solid,red,line width = 2.0] coordinates {(1.25,10) (1.25,0.01)};
        \addlegendentry{Case 1}        
        \addplot [smooth,line width = 1.0,mark=*,mark indices={4,6}] table[x=T,y=A] {pgfplots.dat};
        \addlegendentry{Case 2}        
        \addplot [smooth,line width = 1.0,mark=] table[x=T,y=B] {pgfplots.dat};
        \addplot [smooth,line width = 1.0,mark=] table[x=T,y=C] {pgfplots.dat};

    \end{semilogyaxis}
\end{tikzpicture}
\end{document}

答案1

使用

 legend image post style={mark indices={}}

作为单个图或轴环境的选项。

\documentclass{standalone}
\usepackage{filecontents}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}% <- set a compat! current version is 1.15
\begin{filecontents*}{pgfplots.dat}
T   A  B  C
1.42857     6.616   6.697   6.782
1.40845     5.413   5.487   5.564
1.38889     4.544   4.612   4.684
1.36986     3.916   3.982   4.052
1.35135     3.469   3.534   3.603
1.33333     3.158   3.224   3.294
1.31579     2.954   3.022   3.094
1.29870     2.838   2.909   2.983
1.28205     2.795   2.870   2.949
1.26582     2.818   2.898   2.984
1.25000     2.905   2.992   3.085
1.23457     3.057   3.153   3.255
1.21951     3.278   3.385   3.499
1.20482     3.571   3.69    3.816
1.19048     3.922   4.052   4.188
1.17647     4.296   4.433   4.576
1.16279     4.645   4.783   4.927
1.14943     4.921   5.054   5.193
1.13636     5.085   5.208   5.335
1.12360     5.114   5.222   5.333
1.11111     5.007   5.097   5.19
1.09890     4.784   4.858   4.933
1.08696     4.477   4.536   4.596
1.07527     4.121   4.167   4.214
1.06383     3.742   3.778   3.815
1.05263     3.364   3.392   3.42
1.04167     3.001   3.022   3.043
1.03093     2.661   2.677   2.694
1.02041     2.35    2.362   2.375
1.01010     2.069   2.079   2.089
1.00000     1.819   1.827   1.834
\end{filecontents*}

\begin{document}
\begin{tikzpicture}
    \begin{semilogyaxis}[
        xlabel=x,
        ylabel=y,
        xmin=1.10,
        xmax=1.4,
        ymin=2.0,
        ymax=7.0,
        legend image post style={mark indices={}}% <- added
        ]
        \addplot [solid,red,line width = 2.0] coordinates {(1.25,10) (1.25,0.01)};
        \addlegendentry{Case 1}        
        \addplot [smooth,line width = 1.0,mark=*,mark indices={4,6}] table[x=T,y=A] {pgfplots.dat};
        \addlegendentry{Case 2}        
        \addplot [smooth,line width = 1.0,mark=] table[x=T,y=B] {pgfplots.dat};
        \addplot [smooth,line width = 1.0,mark=] table[x=T,y=C] {pgfplots.dat};

    \end{semilogyaxis}
\end{tikzpicture}
\end{document}

结果:

在此处输入图片描述

答案2

一种可能的解决方法是添加到forget plot\addplot然后使用\addlegendimage{line width=1,mark=*}来填充图例。

代码输出

\documentclass{standalone}
\usepackage{filecontents}
\usepackage{pgfplots}
\begin{filecontents*}{pgfplots.dat}
T   A  B  C
1.42857     6.616   6.697   6.782
1.40845     5.413   5.487   5.564
1.38889     4.544   4.612   4.684
1.36986     3.916   3.982   4.052
1.35135     3.469   3.534   3.603
1.33333     3.158   3.224   3.294
1.31579     2.954   3.022   3.094
1.29870     2.838   2.909   2.983
1.28205     2.795   2.870   2.949
1.26582     2.818   2.898   2.984
1.25000     2.905   2.992   3.085
1.23457     3.057   3.153   3.255
1.21951     3.278   3.385   3.499
1.20482     3.571   3.69    3.816
1.19048     3.922   4.052   4.188
1.17647     4.296   4.433   4.576
1.16279     4.645   4.783   4.927
1.14943     4.921   5.054   5.193
1.13636     5.085   5.208   5.335
1.12360     5.114   5.222   5.333
1.11111     5.007   5.097   5.19
1.09890     4.784   4.858   4.933
1.08696     4.477   4.536   4.596
1.07527     4.121   4.167   4.214
1.06383     3.742   3.778   3.815
1.05263     3.364   3.392   3.42
1.04167     3.001   3.022   3.043
1.03093     2.661   2.677   2.694
1.02041     2.35    2.362   2.375
1.01010     2.069   2.079   2.089
1.00000     1.819   1.827   1.834
\end{filecontents*}

\begin{document}
\begin{tikzpicture}
    \begin{semilogyaxis}[
        xlabel=x,
        ylabel=y,
        xmin=1.10,
        xmax=1.4,
        ymin=2.0,
        ymax=7.0,
        ]
        \addplot [solid,red,line width = 2.0] coordinates {(1.25,10) (1.25,0.01)};
        \addlegendentry{Case 1}        
        \addplot [smooth,line width = 1.0,mark=*,mark indices={4,6},forget plot] table[x=T,y=A] {pgfplots.dat};
        \addlegendimage{line width=1,mark=*}
        \addlegendentry{Case 2}        
        \addplot [smooth,line width = 1.0,mark=] table[x=T,y=B] {pgfplots.dat};
        \addplot [smooth,line width = 1.0,mark=] table[x=T,y=C] {pgfplots.dat};

    \end{semilogyaxis}
\end{tikzpicture}
\end{document}

相关内容