填补不当行为?

填补不当行为?

大家好!我正尝试用深红色来给黑线和两条红线之间的三角形区域上色,该区域目前为浅红色(在右侧)。在阅读了手册的相应部分和此网站发布的一些问题后,我或多或少了解了“fillbetween”功能的工作原理,但我仍然无法得到我想要的确切内容。有人能告诉我我遗漏了什么或没有正确理解什么吗?这是 MWE:

\documentclass[a4paper]{article}
\usepackage{enumitem}
\usepackage{float}
\usepackage{sectsty}
\usepackage{lipsum}
\usepackage{amssymb}
\usepackage{mathtools}
\usepackage[labelfont=bf,skip=0pt,labelsep=period]{caption}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.6}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{matrix,calc,positioning}
\pgfplotsset{soldot/.style={color=black,only marks,mark=*}}
\pgfplotsset{/pgfplots/xlabel near ticks/.style={/pgfplots/every axis x label/.style={at={(ticklabel cs:0.5)},anchor=near ticklabel}},/pgfplots/ylabel near ticks/.style={/pgfplots/every axis y label/.style={at={(ticklabel cs:0.5)},rotate=90,anchor=near ticklabel}}}
\renewcommand{\thefigure}{\Roman{figure}}

\begin{document}
\begin{figure}[H]
\centering
\caption{Pay-off Area of the Infinitely Repeated Game A}
\label{Pay-off Area of the Infinitely Repeated Game A II}
\medskip
\begin{tikzpicture}
\begin{axis}[
xmax=5,
xmin=0,
ymax=5,
ymin=0,
extra tick style={grid=major},
ylabel near ticks,
xlabel near ticks,
xlabel={$P_1$'s utility (pay-offs)},
ylabel={$P_2$'s utility (pay-offs)},
xtick={0,1,2,3,4,5},
ytick={0,1,2,3,4,5},
xmajorgrids=true,
ymajorgrids=true,
legend style={at={(0.5,-0.25)},anchor=north}, 
width=7cm,
height=7cm
]
\addplot[thick, black, no markers, fill=red, fill opacity=0.30, name path=A] coordinates {
(5, 1)
(4, 4)
(1, 5)
(2, 2)
(5, 1)
};
\addlegendentry{Feasible Pay-off Area of both Players};
\addplot +[mark=none, red, thick, name path=B] coordinates {(2, 0) (2, 5)};
\addplot +[mark=none, red, thick, name path=C] coordinates {(0, 2) (5, 2)};
\addplot +[mark=none, gray, ultra thin, name path=D] coordinates {(4, 0) (4, 5)};
\addplot +[mark=none, gray, ultra thin, name path=E] coordinates {(0, 4) (5, 4)};
\addlegendentry{Min-Max Pay-off Area of both Players};
\addplot +[fill=red, fill opacity=0.40] fill between[of=B and D, soft clip={domain=4:2, domain y=2:4}];
\addplot +[fill=red, fill opacity=0.40] fill between[of=E and A, soft clip={domain=2:4}];
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

这是我得到的输出:

在此处输入图片描述

我需要的改变是改变左侧浅红色三角形的颜色,使其看起来是深红色而不是浅色。我知道工具已经存在,但我已经尝试了所有可行的组合,但还是无法获得我需要的东西。提前谢谢大家!

答案1

您几乎已经掌握了。诀窍是使用密钥,然后使用(索引从 0 开始)split填充第二段。every segment no 1/.style

以下代码现在仅包含解决方案的相关部分。

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
    \usetikzlibrary{
        pgfplots.fillbetween,
    }
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            xmax=5,
            xmin=0,
            ymax=5,
            ymin=0,
            width=7cm,
            height=7cm,
            no markers,
        ]
            \addplot [
                thick,
                black,
                fill=red,
                fill opacity=0.30,
                name path=A,
            ] coordinates {
                (5, 1)
                (4, 4)
                (1, 5)
                (2, 2)
            }
                -- cycle
            ;
            \addplot+ [
                red,
                thick,
                name path=B,
            ] coordinates {(2, 0) (2, 5)};

            \addplot+ [
                fill=none,
            ] fill between [
                of=A and B,
                split,
                every segment no 1/.style={
                    fill=green,
                },
            ];
        \end{axis}
    \end{tikzpicture}
\end{document}

该图显示了上述代码的结果


编辑:

这又是解决方案,但现在经过了修改,您可以看到交点在哪里以及这会产生多少个段。

这很可能不会像你预期的那样工作,但可以通过以下方式解释pgfplots手册 v1.13 第427 页第 5.7.8 节第 3 项

这适用于“正常”情况,您可以使用手册中给出的示例之一亲自尝试。

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
    \usetikzlibrary{
        patterns,
        pgfplots.fillbetween,
    }
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            xmin=0,
            xmax=5,
            ymin=0,
            ymax=5,
            clip=false,
        ]
            \addplot [
                thick,
                black,
                name path=A,
            ] coordinates {
                (5, 1)
                (4, 4)
                (1, 5)
                (2, 2)
            }
                node [at start,label={right:start\,=\,end}] (start) {}
                -- cycle
            ;
            \addplot [
                red,
                thick,
                name path=B,
            ] coordinates {(2, 0) (2, 5)};

            % fill all segments with the checkerboard pattern
            % from this can be seen, that there should be 3 segments (0,1,2)
            \addplot [
                pattern=checkerboard,
                pattern color=blue!50,
            ] fill between [
                of=A and B,
            ];
            % now we try to fill them separately
            \addplot [
                fill=none,
            ] fill between [
                of=A and B,
                split,
                % works
                every segment no 0/.style={
                    fill=red!50,
                    opacity=0.5,
                },
                % works
                every segment no 1/.style={
                    fill=green!50,
                    opacity=0.5,
                },
                % doesn't work
                % this is most probably because path A intersects with itself;
                % see section 5.7.8 «Pitfalls and Limitations» on page 427
                % in the manual v1.13
%                every segment no 2/.style={% <-- equivalent to next line
                every last segment/.style={
                    fill=yellow!50,
                },
            ];

            % show the intersections of the pathes which show together with
            % the start/end point(s) the beginning/end of the segments
            \draw [
                name intersections={
                    of=A and B,
                    name=i,
                    total=\t,
                },
                black,
                every node/.style={
                    above left,
                    black,
                    opacity=1,
                }
            ]
            \foreach \s in {1,...,\t}{
                (i-\s) circle (2pt) node {\footnotesize\s}
            };

        \end{axis}
            \fill [red,radius=2pt] (start) circle;
    \end{tikzpicture}
\end{document}

该图显示了上述代码的结果

相关内容