更新

更新

请考虑这个MWE:

\documentclass{article}

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{pgfplots}
\usetikzlibrary{arrows,overlay-beamer-styles,babel,arrows.meta, patterns}
\pgfplotsset{compat=1.8}

% ========================
% From https://tex.stackexchange.com/a/76014/152550
\newcommand{\drawge}{-- (rel axis cs:1,0) -- (rel axis cs:1,1) -- (rel axis cs:0,1) \closedcycle}
\newcommand{\drawle}{-- (rel axis cs:1,1) -- (rel axis cs:1,0) -- (rel axis cs:0,0) \closedcycle}
% ========================

\begin{document}

\begin{tikzpicture}
    \begin{axis}[
            legend pos=outer north east,
            axis lines = center,
            xticklabel style = {font=\tiny},
            yticklabel style = {font=\tiny},
            xlabel = $x$,
            ylabel = $y$,
            legend style={cells={align=left}},
            legend cell align={left},
            xticklabel=\empty,
            yticklabel=\empty,
            ]
            \addplot[very thick,red] {0.5*x+1};
            \addplot[very thick,red] {-2*x+3};
            %\addplot[draw=none,pattern=vertical lines,green,opacity=0.3] {0.5*x+1}\drawge;
            %\addplot[draw=none,pattern=vertical lines,blue,opacity=0.3] {0.5*x+1}\drawle;
            %\addplot[draw=none,pattern=vertical lines,orange,opacity=0.3] {-2*x+3}\drawge;
            %\addplot[draw=none,pattern=vertical lines,violet,opacity=0.3] {-2*x+3}\drawle;
    \end{axis}
\end{tikzpicture}

\end{document}

MWE 输出

我想填充平面上划分的 4 个区域,不重叠的纯色(包括红线)。如果我们取消最后四个的注释,\addplot我们得到:

我做了什么

如你看到的:

  • 四种\addplot颜色重叠,
  • 红线与四种颜色重叠。

如何避免区域颜色和线条颜色重叠?

PS 我应该能够在上面添加第三条红线,并用总共 7 种不同的颜色绘制区域。

答案1

更新

axis background我找到了一种更好的为区域着色的方法:找到区域的顶点并根据规范填充图层上的路径set layers=standard在此处输入图片描述

\documentclass{article}

\usepackage{pgfplots}
\usetikzlibrary{intersections}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=1.17}

\begin{document}
\begin{tikzpicture}
[
  pin distance=2mm,
  every pin edge/.style={thick,-},
]
\begin{axis}[
  set layers=standard,
  legend pos=outer north east,
  axis lines = center,
  xticklabel style = {font=\tiny},
  yticklabel style = {font=\tiny},
  xlabel = $x$,
  ylabel = $y$,
  legend style={cells={align=left}},
  legend cell align={left},
  xticklabel=\empty,
  yticklabel=\empty,
]
  \addplot[name path=l1, very thick,red] {0.5*x+1}
    coordinate [pos=0] (l1-0)
    coordinate [pos=1] (l1-1)
    node [pos=0.1, pin=below:{$l_1$}] {};
  \addplot[name path=l2, very thick,teal] {-2*x+3}
    coordinate [pos=0] (l2-0)
    coordinate [pos=1] (l2-1)
    node [pos=0.1, pin=below:{$l_2$}] {};
  \addplot[name path=l3, very thick,brown] {3*x-4.3}
    coordinate [pos=0] (l3-0)
    coordinate [pos=1] (l3-1)
    node [pos=0.2, pin=right:{$l_3$}] {};

  % intersection point
  \path[name intersections={of=l1 and l2, name=i}];
  \path[name intersections={of=l1 and l3, name=j}];
  \path[name intersections={of=l2 and l3, name=k}];
  \node[above] at (i-1) {$i_1$};
  \node[above] at (j-1) {$j_1$};
  \node[below] at (k-1) {$k_1$};

  \tikzaliascoordinatesystem{r}{rel axis}
  \pgfonlayer{axis background}
  \fill[red!10] (l2-0) -- (i-1) -- (l1-0);
  \fill[green!10] (l2-0) -- (i-1) -- (j-1) -- (l3-1) -- (r cs:1, 1);
  \fill[blue!10] (j-1) -- (l1-1) -- (l3-1);
  \fill[yellow!10] (l3-0) -- (k-1) -- (i-1) -- (l1-0);
  \fill[cyan!20] (l3-0) -- (k-1) -- (l2-1) -- (r cs:1, 0);
  \fill[teal!20] (l2-1) -- (k-1) -- (j-1) -- (l1-1);
  \fill[violet!50] (i-1) -- (j-1) -- (k-1);
  \endpgfonlayer

\end{axis}
\end{tikzpicture}

\end{document}

旧答案

使用 pgfplotslibrary fillbetween。将区域划分为 6 个子区域进行填充。 在此处输入图片描述

\documentclass{article}

\usepackage{pgfplots}
\usetikzlibrary{intersections}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=1.17}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
  legend pos=outer north east,
  axis lines = center,
  xticklabel style = {font=\tiny},
  yticklabel style = {font=\tiny},
  xlabel = $x$,
  ylabel = $y$,
  legend style={cells={align=left}},
  legend cell align={left},
  xticklabel=\empty,
  yticklabel=\empty,
]
  \addplot[name path=l1, very thick,red] {0.5*x+1};
  \addplot[name path=l2, very thick,red] {-2*x+3};

  % fill left and right
  \addplot[opacity=.3] fill between [
   of=l1 and l2,
   split,
   every segment no 0/.style=blue,
   every segment no 1/.style=orange,
  ];

  % intersection point
  \path[name intersections={of=l1 and l2, name=i}];

  % upper and lower bound lines
  \path[name path=upper] (rel axis cs:0, 1) -- (rel axis cs:1, 1);
  \path[name path=lower] (rel axis cs:0, 0) -- (rel axis cs:1, 0);

  % fill upper left
  \addplot [green, opacity=.3] fill between [
   of=l2 and upper,
   % rectangle must be constructed from left to right
   soft clip={(rel axis cs:0, 1) rectangle (i-1)},
  ];

  % fill upper right
  \addplot [green, opacity=.3] fill between [
   of=l1 and upper,
   soft clip={(i-1) rectangle (rel axis cs:1, 1)},
  ];

  % fill lower left
  \addplot [violet, opacity=.3] fill between [
   of=l1 and lower,
   soft clip={(rel axis cs:0, 0) rectangle (i-1)},
  ];

  % fill lower right
  \addplot [violet, opacity=.3] fill between [
   of=l2 and lower,
   soft clip={(i-1) rectangle (rel axis cs:1, 0)},
  ];
\end{axis}
\end{tikzpicture}

\end{document}

答案2

这是否满足你的要求--

在此处输入图片描述

\documentclass{article}

\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}

\begin{document}
    
    \begin{tikzpicture}
    \begin{axis}[
    legend pos=outer north east,
    axis lines = center,
    xticklabel style = {font=\tiny},
    yticklabel style = {font=\tiny},
    xlabel = $x$,
    ylabel = $y$,
    legend style={cells={align=left}},
    legend cell align={left},
    xticklabel=\empty,
    yticklabel=\empty,
    ]
    \addplot[very thick,name path=a,draw=none] {0.5*x+1};
    \addplot[very thick,draw=none,name path=b] {-2*x+3};
    \path [name intersections={of=a and b,by=c}];
    \node [fill=blue,inner sep=2pt,label=-90:$C$] at (c) {};
    
    \path[name path=lower,
    %thick,draw=red,
    intersection segments={
        of=a and b,
        sequence=B0 -- A1,
    }
    ]; 
    \path[name path=upper,
    %thick,draw=red,
    intersection segments={
        of=a and b,
        sequence=B1 -- A0,
    }
    ]; 
    
    \addplot[red!70, opacity=.5]   fill between[of=lower and a];
        \addplot[blue!70, opacity=.5]   fill between[of=lower and b];   
                \addplot[green!70, opacity=.5]   fill between[reverse=false,of=lower 
                 and a];
                        \addplot[green!70!yellow!30, opacity=.5]   fill 
                         between[of=upper and b];
    \end{axis}
    
    \end{tikzpicture}
    
\end{document}

相关内容