更新答案

更新答案

我有以下简单的图片:

在此处输入图片描述

有没有办法更好地说明 LS 和 LD 之间的小重叠?我知道我可以用另一条线来定义这个区域,并使用不同版本的红色和绿色虚线。但是,对于所有重叠线的情况,是否有最佳实践,例如移动线条或将其自动分成红色和绿色部分?或者至少有一个颜色混合选项?也许可以为重叠区域使用透明线?但这仍然留下了自动检测重叠问题的问题。

谢谢

\begin{center}
\begin{tikzpicture}[ultra thick]
    % Axis and coordinates
    \coordinate (y) at (0,5);
    \coordinate (o) at (0,0);
    \coordinate (x) at (7,0);

    % Axis labels, basic line and curve
    \draw[<->,line width=1.5pt] (y) node[left, label={[align=left] $w$}] -- (o) -- (x) node[below]{$L^S, L^D$};

        \node[left] at (0,0) {0};
        \node[left] at (0,1) {z};
        \node[left] at (0,4) {$\overline{w}$};

        % dashed line and curve
        \draw[red] (0,1) -- (1.5,1) node[right, red] {$L^S$};
        \draw[red] (1.5,1) -- (3,4);
        \draw[red] (3,4) -- (6,4);
        \draw[gray, dashed, thin]  (5.5,0) node[below, black] {$\overline{L}$} -- (5.5,4) ;

        \draw[ForestGreen] (0,4) -- (3.5,4);
        \draw[ForestGreen] (3.5,4) -- (5,1) node[left, ForestGreen] {$L^D$};

\end{tikzpicture}
\end{center}

答案1

更新答案

灵感来自TikZ 中的“Z 级别”

\pgfdeclarelayer{front}
\pgfdeclarelayer{back}
\pgfsetlayers{back,main,front}
\tikzset{% https://tex.stackexchange.com/questions/20425/z-level-in-tikz
    on layer/.code={\pgfonlayer{#1}\tikzset{every picture}\begingroup\aftergroup\endpgfonlayer\aftergroup\endgroup},
    also in front/.style 2 args={#1,postaction={on layer=front,draw,#1,#2}},
    also behind/.style 2 args={#1,postaction={on layer=back,draw,#1,#2}},
    every picture/.style={line width=6},
}

\tikz{
    \draw[also in front={red}{opacity=.5}](-2,0)--(1,0)--(2,1);
    \draw[green](-2,-1)--(-1,0)--(2,0);
}

\tikz{
    \draw[also in front={red}{dashed}](-2,0)--(1,0)--(2,1);
    \draw[green](-2,-1)--(-1,0)--(2,0);
}

\tikz{
    \draw[also in front={red}{line width=.5\pgflinewidth}](-2,0)--(1,0)--(2,1);
    \draw[green](-2,-1)--(-1,0)--(2,0);
}

\tikz{
    \draw[also in front={red}{line width=.5\pgflinewidth}](-2,0)--(1,0)--(2,1);
    \draw[also behind={}{green},also in front={green,dashed}{line width=.5\pgflinewidth,dash phase=3pt}](-2,-1)--(-1,0)--(2,0);
}

\tikz{
    \let\tikzerror\relax
    \draw[also in front={red}{clip,draw=none,postaction={draw}}](-2,0)--(1,0)--(2,1);
    \draw[green](-2,-1)--(-1,0)--(2,0);
}

\tikz{
    \let\tikzerror\relax
    \path(-2,-1)(2,1);
    \draw[also behind={}{red},preaction={draw=red,dashed},also in front={clip,draw=none}{postaction={draw=red,dashed,dash phase=3pt}}](-2,0)--(1,0)--(2,1);
    \draw[also behind={green}{}](-2,-1)--(-1,0)--(2,0);
}

还有一个疯狂的。(基本上复制了的定义snake

\pgfdeclaredecoration{DNA}{initial}
{
  \state{initial}[switch if less than=+.625\pgfdecorationsegmentlength to final,
                  width=+.3125\pgfdecorationsegmentlength,
                  next state=down]
  { 
    \pgfpathcurveto
    {\pgfqpoint{.125\pgfdecorationsegmentlength}{0pt}}
    {\pgfqpoint{.1875\pgfdecorationsegmentlength}{\pgfdecorationsegmentamplitude}}
    {\pgfqpoint{.3125\pgfdecorationsegmentlength}{\pgfdecorationsegmentamplitude}}
  }
  \state{down}[switch if less than=+.8125\pgfdecorationsegmentlength to end down,
               width=+.5\pgfdecorationsegmentlength,
               next state=up]
  {
    \def\pgfpathcurveto##1##2##3{\pgfpathmoveto{##3}}
    \pgfpathcosine{\pgfqpoint{.25\pgfdecorationsegmentlength}{-1\pgfdecorationsegmentamplitude}}
    \pgfpathsine{\pgfqpoint{.25\pgfdecorationsegmentlength}{-1\pgfdecorationsegmentamplitude}}
  }               
  \state{up}[switch if less than=+.8125\pgfdecorationsegmentlength to end up,
             width=+.5\pgfdecorationsegmentlength,
             next state=down]
  {
    \pgfpathcosine{\pgfqpoint{.25\pgfdecorationsegmentlength}{\pgfdecorationsegmentamplitude}}
    \pgfpathsine{\pgfqpoint{.25\pgfdecorationsegmentlength}{\pgfdecorationsegmentamplitude}}
  }               
  \state{end down}[width=+.3125\pgfdecorationsegmentlength,
                   next state=final]
  {
    \pgfpathcurveto
    {\pgfqpoint{.125\pgfdecorationsegmentlength}{\pgfdecorationsegmentamplitude}}
    {\pgfqpoint{.1875\pgfdecorationsegmentlength}{0pt}}
    {\pgfqpoint{.3125\pgfdecorationsegmentlength}{0pt}}
  }  
  \state{end up}[width=+.3125\pgfdecorationsegmentlength,
                 next state=final]
  {
    \pgfpathcurveto
    {\pgfqpoint{.125\pgfdecorationsegmentlength}{-\pgfdecorationsegmentamplitude}}
    {\pgfqpoint{.1875\pgfdecorationsegmentlength}{0pt}}
    {\pgfqpoint{.3125\pgfdecorationsegmentlength}{0pt}}
  }  
  \state{final}
  {
    \pgfpathlineto{\pgfpointdecoratedpathlast}
  }
}
\tikz[line width=2]{
    \draw[red,decorate,decoration={snake}](-2,0)--(1,0)--(2,1);
    \draw[green,decorate,decoration={snake}](-2,-1)--(-1,0)--(2,0);
    \draw[red,decorate,decoration={DNA}](-2,0)--(1,0)--(2,1);
}

旧答案

就在这个问题被关闭之前,这里有一个 TiZ 特定方法的灵感来自

\documentclass[tikz]{standalone}
\usetikzlibrary{decorations.pathmorphing}
\begin{document}
\pgfdeclaredecoration{penciline}{initial}{
    \state{initial}[width=+\pgfdecoratedinputsegmentremainingdistance,auto corner on length=1mm,]{
        \pgfpathcurveto%
        {% From
            \pgfqpoint{\pgfdecoratedinputsegmentremainingdistance}
                            {\pgfdecorationsegmentamplitude}
        }
        {%  Control 1
        \pgfmathrand
        \pgfpointadd{\pgfqpoint{\pgfdecoratedinputsegmentremainingdistance}{0pt}}
                        {\pgfqpoint{-\pgfdecorationsegmentaspect\pgfdecoratedinputsegmentremainingdistance}%
                                        {\pgfmathresult\pgfdecorationsegmentamplitude}
                        }
        }
        {%TO 
        \pgfpointadd{\pgfpointdecoratedinputsegmentlast}{\pgfpoint{1pt}{1pt}}
        }
    }
    \state{final}{}
}
\begin{tikzpicture}[ultra thick,decoration=penciline]
    % Axis and coordinates
    \coordinate (y) at (0,5);
    \coordinate (o) at (0,0);
    \coordinate (x) at (7,0);

    % Axis labels, basic line and curve
    \draw[<->,line width=1.5pt] (y) node[left, label={[align=left] $w$}]{} -- (o) -- (x) node[below]{$L^S, L^D$};
        \node[left] at (0,0) {0};
        \node[left] at (0,1) {z};
        \node[left] at (0,4) {$\overline{w}$};
        % dashed line and curve
        \draw[red,decorate] (0,1) -- (1.5,1)node[right, red] {$L^S$}(1.5,1) -- (3,4) -- (6,4);
        \draw[gray, dashed, thin]  (5.5,0) node[below, black] {$\overline{L}$} -- (5.5,4) ;
        \draw[green,decorate] (0,4) -- (3.5,4) -- (5,1) node[left, green] {$L^D$};
\end{tikzpicture}
\end{document}

答案2

对于重叠线,我建议使用不同的样式,dotted例如dashed

在此处输入图片描述

笔记:

  • 因为这是一个图表,所以您确实应该考虑使用pgfplots而不是直线tikz

代码:

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[ultra thick]
    % Axis and coordinates
    \coordinate (y) at (0,5);
    \coordinate (o) at (0,0);
    \coordinate (x) at (7,0);

    % Axis labels, basic line and curve
    \draw[<->,line width=1.5pt] (y) 
        node[left, label={[align=left] $w$}] {} -- (o) -- (x) 
        node[below]{$L^S, L^D$};

        \node[left] at (0,0) {0};
        \node[left] at (0,1) {z};
        \node[left] at (0,4) {$\overline{w}$};

        % dashed line and curve
        \draw[red] (0,1) -- (1.5,1) node[right, red] {$L^S$};
        \draw[red] (1.5,1) -- (3,4);
        \draw[red] (3,4) -- (6,4);
        \draw[gray, dashed, thin]  (5.5,0) node[below, black] {$\overline{L}$} -- (5.5,4) ;

        \draw[blue, dotted] (0,4) -- (3.5,4);
        \draw[blue, dotted] (3.5,4) -- (5,1) node[left, blue] {$L^D$};

\end{tikzpicture}
\end{document}

相关内容