将线的名称添加到 pgfplots 中的图例中

将线的名称添加到 pgfplots 中的图例中

我用实际数据制作了一个图表并画了一条虚线。我无法将虚线包含在图例中。它显示一条直线。有什么想法吗?这是我使用的代码

\documentclass[a4paper]{article}

\usepackage{pgfplots}
\usepackage{pgfplotstable}%fitting functions
\usepackage{tikz}
\usetikzlibrary{tikzmark}

\pgfkeys{/pgf/number format/.cd,1000 sep={}}


\newcommand{\myfont}{\fontfamily{cmss}\selectfont}% used with mathpazo 


\pgfplotsset{compat=1.15}
\pgfplotsset{label style={font=\Large},
            tick label style={font=\Large}}

\pgfplotsset{error bars/.cd,
    x dir=both, x explicit,
    y dir=both, y explicit,
    }
\pgfplotsset{/pgfplots/error bars/error bar style={semithick,black}}
%\pgfplotsset{/pgfplots/label shift={0pt}}


\begin{document}


\begin{figure}
\centering

\begin{tikzpicture}
\begin{axis}[
    enable tick line clipping=false, % makes all ticks same width
    axis line style=semithick,
    width=9cm,
    height=7cm,
    legend style={at={(0.95,0.95)},draw=none},
    legend cell align={left},
    x tick style={black,semithick},
    x label style=
        {at={(ticklabel cs:0.5)},anchor=near ticklabel},
    xlabel={{$t$} [{\myfont min}]},
    xmin=0,xmax=3,
    xtick={0,1,...,3},
    xtick pos=bottom,
    xtick align=outside,
    y tick style={black,semithick},
    y label style=
        {at={(ticklabel cs:0.5)},anchor=near ticklabel},
    ylabel={{$F$} [{\myfont N}]},
    ymin=0,ymax=150,
    ytick={0,50,...,150},
    ytick pos=left,
    ytick align=outside,
]
    %\addplot [black,thick] file {force1.txt};

    %\addplot [black,thick] file {force2.txt};

    \addplot [black,thick] file {force3.txt};
    \addlegendentry{\large{\myfont experimental}}

    \draw [black,dashed,thick] (0,84) -- (3,84);
    \addlegendentry{\large{\myfont model prediction}}


\end{axis}
\end{tikzpicture}
\end{figure}


\end{document}

在此处输入图片描述

答案1

我想在这种情况下最简单的解决方法是(我没有你的数据文件,所以我不得不做点什么)

\documentclass[a4paper]{article}

\usepackage{pgfplots}
\usepackage{pgfplotstable}%fitting functions
\usepackage{tikz}
\usetikzlibrary{tikzmark}

\pgfkeys{/pgf/number format/.cd,1000 sep={}}


\newcommand{\myfont}{\fontfamily{cmss}\selectfont}% used with mathpazo 


\pgfplotsset{compat=1.15}
\pgfplotsset{label style={font=\Large},
            tick label style={font=\Large}}

\pgfplotsset{error bars/.cd,
    x dir=both, x explicit,
    y dir=both, y explicit,
    }
\pgfplotsset{/pgfplots/error bars/error bar style={semithick,black}}
%\pgfplotsset{/pgfplots/label shift={0pt}}


\begin{document}


\begin{figure}
\centering

\begin{tikzpicture}
\begin{axis}[
    enable tick line clipping=false, % makes all ticks same width
    axis line style=semithick,
    width=9cm,
    height=7cm,
    legend style={at={(0.95,0.95)},draw=none},
    legend cell align={left},
    x tick style={black,semithick},
    x label style=
        {at={(ticklabel cs:0.5)},anchor=near ticklabel},
    xlabel={{$t$} [{\myfont min}]},
    xmin=0,xmax=3,
    xtick={0,1,...,3},
    xtick pos=bottom,
    xtick align=outside,
    y tick style={black,semithick},
    y label style=
        {at={(ticklabel cs:0.5)},anchor=near ticklabel},
    ylabel={{$F$} [{\myfont N}]},
    ymin=0,ymax=150,
    ytick={0,50,...,150},
    ytick pos=left,
    ytick align=outside,
]
    %\addplot [black,thick] file {force1.txt};

    %\addplot [black,thick] file {force2.txt};

    \addplot [black,thick,smooth] coordinates {(0,0) (0.1,90) (0.6,70) (3,73)};
    \addlegendentry{\large{\myfont experimental}}

    \addplot[color=black,dashed,thick] coordinates {(0,84) (3,84)};
    \addlegendentry{\large{\myfont model prediction}}


\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

在此处输入图片描述

您也可以玩等等\addlegendimage,但这样的话就需要付出更多努力。

相关内容