pgfplots,tikz:addlegendentry

pgfplots,tikz:addlegendentry

我希望图例条目不是垂直放置,而是水平放置(彼此相邻)。

有什么意见吗 :) ?非常感谢!

\documentclass{article}
\usepackage{etex}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{ternary}


\begin{document}


    \begin{tikzpicture}


    \pgfplotsset{every axis/.append style={solid},
        every tick/.append style={semithick,color=black}, 
    }


    \begin{axis}[smooth,
    scale only axis = true, width = 0.9\textwidth, height = 0.35\textwidth,
    ymin =0, ymax =10, xmin =0,  xmax=5, 
    ylabel = {y}, 
    xlabel={x},
    grid style ={dashed},
    grid = both,
    legend style={font=\footnotesize, 
        legend pos =north west},
    ]

\addplot [dotted, domain=0:5] {2*x};
\addlegendentry{Eins}; 

\addplot [dashed, domain=0:5] {1.5*x};
\addlegendentry{Zwei}; 


    \end{axis}

    \end{tikzpicture}


\end{document}

在此处输入图片描述

答案1

添加legend columns=2以便水平方向有两个图例条目。

我认为etex现在不需要加载。

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{ternary}


\begin{document}


    \begin{tikzpicture}


    \pgfplotsset{every axis/.append style={solid},
        every tick/.append style={semithick,color=black}, 
    }


    \begin{axis}[smooth,
    scale only axis = true, width = 0.9\textwidth, height = 0.35\textwidth,
    ymin =0, ymax =10, xmin =0,  xmax=5, 
    ylabel = {y}, 
    xlabel={x},
    grid style ={dashed},
    grid = both,
    legend style={font=\footnotesize, 
        legend pos =north west},
      legend columns=2
    ]

\addplot [dotted, domain=0:5] {2*x};
\addlegendentry{Eins}; 

\addplot [dashed, domain=0:5] {1.5*x};
\addlegendentry{Zwei}; 


    \end{axis}

    \end{tikzpicture}


\end{document}

在此处输入图片描述

相关内容