向线段添加节点

向线段添加节点
\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage[active,float]{preview}
\usetikzlibrary{calc}
\usetikzlibrary{graphs,plotmarks,arrows,automata,positioning,fit,shapes.geometric,backgrounds}
\usepackage{graphicx}
\PreviewEnvironment{tikzpicture} 
\usetikzlibrary{plotmarks}

\begin{document}
\begin{tikzpicture}[x=0.8cm,y=0.5cm]
    %axis
    \draw (0,0) -- coordinate (x axis mid) (10,0);
        \draw (0,0) -- coordinate (y axis mid) (0,12);

        %ticks
        \foreach \x in {0,...,10}
        \draw (\x,0pt) -- (\x,-3pt)     node[anchor=north] {\x};
        \foreach \y in {0,...,12}
            \draw (0pt,\y) -- (-3pt,\y)     node[anchor=east] {\y}; 

    %labels      
    \node[below=0.8cm,xshift=0.3cm,scale=1.5] at (x axis mid) {$\sum{t_i}\;\;\longrightarrow$};

    % plot first concave function
    \draw[mark=square*,green,thick,mark options={fill=green}] (0,2) -- (1,3) -- (2,4) -- (3,5) -- (4,5) -- (5,5) -- (6,5) -- (7,5) -- (8,5) -- (9,3) -- (10,1);

    \node[mark=square*,green,thick,mark options={fill=green}] at (4,5) {};

    % plot second concave function
    \draw[mark=square*,red,thick,mark options={fill=red}] (0,1) -- (1,2) -- (2,3) -- (3,4) -- (4,5) -- (5,6) -- (6,6) -- (7,5) -- (8,4) -- (9,3) -- (10,2);

    % plot third concave function
    \draw[mark=*,blue,thick,mark options={fill=blue}] (0,3) -- (1,5) -- (2,7) -- (3,9) -- (4,10) -- (5,11) -- (6,11) -- (7,10) -- (8,9) -- (9,6) -- (10,3);

    %legend 
    \begin{scope}[shift={(0.5,10)}] 
    \draw[yshift=3\baselineskip] (0,0) -- 
        plot[mark=square*, mark options={fill=red}] (0.25,0) -- (0.5,0) node[right]{$f(t)$};
    \draw[yshift=2\baselineskip] (0,0) -- 
        plot[mark=square*, mark options={fill=green}] (0.25,0) -- (0.5,0) node[right]{$g(t)$};
    \draw[yshift=\baselineskip] (0,0) -- 
        plot[mark=square*, mark options={fill=blue}] (0.25,0) -- (0.5,0) node[right]{$f(t) + g(t)$};
    \end{scope}
\end{tikzpicture}

\end{document}

代表下图:

最终图像 我在执行以下操作时遇到麻烦:

  1. 在 xy 轴上以 2 为步长显示数字
  2. 由于某种原因,我无法想象线段之间的节点
  3. 无法在图例周围添加矩形区域。

答案1

使用pgfplots。相信我,这很容易。

\documentclass[border=4]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[width=7cm,%axis lines=left,
            xlabel={$\sum{t_i}\quad\longrightarrow$},
            legend style = {legend pos=outer north east,cells={anchor=west}},
            ymax=12,
            ytick={0,2,...,10},
            yticklabels={0,2,4,6,8,10},
            enlarge x limits={rel=0.1,upper}]
    % plot first concave function
    \addplot[mark=square*,green,thick,mark options={mark size=1pt,draw,fill=green!40}] coordinates {(0,2)  (1,3)  (2,4)  (3,5)  (4,5)  (5,5)  (6,5)  (7,5)  (8,5)  (9,3)  (10,1)};
   \addlegendentry{$g(t)$}
    % plot second concave function
    \addplot[mark=square*,red,thick,mark options={mark size=1pt,draw,fill=red!40}] coordinates {(0,1)  (1,2)  (2,3)  (3,4)  (4,5)  (5,6)  (6,6)  (7,5)  (8,4)  (9,3)  (10,2)};
     \addlegendentry{$f(t)$}
    % plot third concave function
    \addplot[mark=*,blue,thick,mark options={mark size=1pt,draw,fill=blue!40}] coordinates {(0,3)  (1,5) (2,7) (3,9) (4,10)   (5,11)   (6,11)  (7,10)  (8,9)  (9,6)  (10,3)};
     \addlegendentry{$f(t) + g(t)$}
     \end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容