填充:如何覆盖穿过填充区域的线?

填充:如何覆盖穿过填充区域的线?

我正在尝试绘制一个图形,其中一条红线穿过一个蓝色填充区域。无论我如何调整填充不透明度。似乎红线总是比阴影区域高出“一层”。我能做些什么让阴影区域比红线高出一层?谢谢。

编辑:

我先画红线(代码在底部),然后填充蓝色区域。右侧的图表是实心填充(填充不透明度=1),但红线仍然很突出。所以我想找到一个解决方案,让红线位于蓝色填充区域下方。 在此处输入图片描述

\begin{figure}[ht]
\centering
  \begin{subfigure}[b]{0.48\textwidth}
    \centering
    \begin{tikzpicture}
    \begin{axis}[
          % height=207pt, % default height
          height=200pt,          
          % height=160pt,
          % width=\axisdefaultwidth,
          font=\footnotesize,
          axis lines=middle,
          xtick={0.2,0.4,0.6,0.8,1},
          ytick={0.2,0.4,0.6,0.8,1},
          xmin=0, xmax=1,
          ymin=0, ymax=1.2,
          clip=false,
          axis equal
    ]
    % shaded regions for a
    \addplot[red, very thick, name path = a] coordinates{(0,0.2) (0,0.8) (1,0.8)};
    \addplot[red, very thick, name path = b] coordinates{(0,0.2) (1,0.2) (1,0.8)};
    \addplot[fill = red, fill opacity=0.5] fill between[of = a and b, soft clip={domain=0:1}];
    % shaded regions for b
    \addplot[blue, very thick, name path = c] coordinates{(0.2,0) (0.2,1) (0.8,1)};
    \addplot[blue, very thick, name path = d] coordinates{(0.2,0) (0.8,0) (0.8,1)}; 
    \addplot[fill = blue, fill opacity=0.5] fill between[of = c and d, soft clip={domain=0:1}];
    \end{axis}
    \end{tikzpicture}
  \end{subfigure}
  \begin{subfigure}[b]{0.48\textwidth}
    \centering
     \begin{tikzpicture}
    \begin{axis}[
          % height=207pt, % default height
          height=200pt,          
          % height=160pt,
          % width=\axisdefaultwidth,
          font=\footnotesize,
          axis lines=middle,
          xtick={0.2,0.4,0.6,0.8,1},
          ytick={0.2,0.4,0.6,0.8,1},
          xmin=0, xmax=1,
          ymin=0, ymax=1.2,
          clip=false,
          axis equal
    ]
    % shaded regions for a
    \addplot[red, very thick, name path = a] coordinates{(0,0.2) (0,0.8) (1,0.8)};
    \addplot[red, very thick, name path = b] coordinates{(0,0.2) (1,0.2) (1,0.8)};
    \addplot[fill = red, fill opacity=0.5] fill between[of = a and b, soft clip={domain=0:1}];
    % shaded regions for b
    \addplot[blue, very thick, name path = c] coordinates{(0.2,0) (0.2,1) (0.8,1)};
    \addplot[blue, very thick, name path = d] coordinates{(0.2,0) (0.8,0) (0.8,1)}; 
    \addplot[fill = blue, fill opacity=1] fill between[of = c and d, soft clip={domain=0:1}];
    \end{axis}
    \end{tikzpicture}
    \end{subfigure}
\end{figure}

答案1

这是我的建议!

\documentclass[]{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.17}

\begin{document}

    \begin{tikzpicture}
    \begin{axis}[
          % height=207pt, % default height
          height=200pt,          
          % height=160pt,
          % width=\axisdefaultwidth,
          font=\footnotesize,
          axis lines=middle,
          xtick={0.2,0.4,0.6,0.8,1},
          ytick={0.2,0.4,0.6,0.8,1},
          xmin=0, xmax=1,
          ymin=0, ymax=1.2,
          clip=false,
          axis equal
    ]
    % shaded regions for a
    \filldraw[red,very thick](0,0.2)--(0,0.8)--(1,0.8)--(1,0.2)--cycle;
   
    % shaded regions for b
    \filldraw[blue, very thick] 
    (0.2,0)--(0.2,1)--(0.8,1)--(0.8,0)--cycle;
    \end{axis}
    \end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容