f = 1 と g = |x| の間の領域の陰影

f = 1 と g = |x| の間の領域の陰影

以下のものがあります:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{mathtools}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    axis lines = middle,
    xlabel = $x$,
    ylabel = {$\begin{aligned}
        \color{blue} f(x)& \color{blue}=1\\
        \color {red} g(x)& \color{red}=|x|
    \end{aligned}$}
]

\addplot [
    name path = A,
    domain=-2:2, 
    color=blue,
]
{1};

\addplot [
    name path = B,
    domain=-2:2, 
    color=red,
]
{abs(x))};

\addplot [
    pattern=north west lines,
]
fill between [
    of=A and B,
    soft clip={domain=-1:1}
];

\end{axis}
\end{tikzpicture}

\end{document}

私は、限られた領域に陰影をつけることを目指しています

f(x) = 1

そして

g(x) = |x|

ただし、代わりに表示されるのは次のようになります。

グラフは描画されますが、領域が網掛けされていません。2 つの関数を別の関数に切り替えて試してみたところ、うまくいきました。そのため、ここで何が問題なのかよくわかりません。(ShareLaTex を使用しています)

答え1

(記録のために: PGFPlots v1.16 のリリースにより、あなたの例では期待どおりの結果が得られます。ただし、あなたが望む結果を達成するためのより良い/簡単な方法があります。)

domain使用する代わりにsplit個別に処理できる 3 つのセグメントを提供する を使用します。したがって、何も塗りつぶさない一般的なオプション ( fill=none) を指定してから、必要な塗りつぶしパターンを " " スタイルに指定しますsegment no 1。(セグメントのカウントは 0 から始まるため、ここで必要なのは 1 番です。)

% used PGFPlots v.1.14
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepackage{amsmath}
    \usetikzlibrary{
        patterns,
        pgfplots.fillbetween,
    }
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            axis lines=middle,
            xlabel=$x$,
            ylabel={
                $\begin{aligned}
                    \color{blue} f(x)& \color{blue}=1\\
                    \color {red} g(x)& \color{red}=|x|
                \end{aligned}$
            },
        ]
            \addplot [
                name path=A,
                domain=-2:2,
                color=blue,
            ] {1};

            \addplot [
                name path = B,
                domain=-2:2,
                color=red,
            ] {abs(x)};

            \addplot [
                fill=none,
            ] fill between [
                of=A and B,
                % --------------------------------------
                % the below code is what I have changed
                split,
                % draw only selected ones:
                % every segment no 0/.style: invisible
                every segment no 1/.style={
                    pattern=north west lines,
                },
                % every segment no 2/.style: invisible
            ];

        \end{axis}
    \end{tikzpicture}
\end{document}

上記コードの結果を示す画像

答え2

fillbetweenこれはのライブラリのバグ (または少なくとも予期しない動作) であると考えられますpgfplots。ただし、これはユーザーとしての私の個人的な見解に過ぎず、そのように動作する特定の理由がある可能性があります。

以下の例を考えてみましょう。この動作を再現するのに必要のない部分をすべて取り除いています。あなたの例のように、領域は塗りつぶされていません。しかし、axis lines=middle、領域は期待どおりに塗りつぶされます。

\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    axis lines=middle,
    domain=-2:2,
]

\addplot[name path=A,color=blue]{1};
\addplot[name path=B,color=red]{abs(x))};
\addplot[black] fill between[of=A and B,soft clip={domain=-1:1}];

\end{axis}
\end{tikzpicture}
\end{document}

悪い結果 良い結果

0有効にした場合、ポイントの処理に問題があるようですaxis lines=middle。簡単な回避策はfill between-1:01つと の別のを使用することです0:1。または、splitStefan Pinnow が提案した方法を使用します。

\addplot[pattern=north west lines] fill between[of=A and B,soft clip={domain=-1:0}];
\addplot[pattern=north west lines] fill between[of=A and B,soft clip={domain=0:1}];

回避策による結果

関連情報