我们怎样才能用一个 \addlegendentry 来表示两个叠加的集合(带有橙色背景的阴影图案)?

我们怎样才能用一个 \addlegendentry 来表示两个叠加的集合(带有橙色背景的阴影图案)?

我写信是想寻求您的帮助。我想在初始图案的背景中添加一种颜色(例如橙色)(见下文)。有没有办法做到这一点:使用与新图形相同的图例?(即使用两种颜色:线条和背景)

我使用以下代码为初始图案的背景着色,但是我无法修改图例的呈现方式。\addlegendentry{Interval 2}

您能告诉我如何使“间隔 2”图例与图表中使用的模式相对应吗?

非常感谢您抽出时间和帮助。此致,Steph

设置阴影但不设置图例

从此页面进行修改

添加橙色背景的代码:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{patterns}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\tikzset{
    hatch distance/.store in=\hatchdistance,
    hatch distance=10pt,
    hatch thickness/.store in=\hatchthickness,
    hatch thickness=2pt
}

\makeatletter
\pgfdeclarepatternformonly[\hatchdistance,\hatchthickness]{flexible hatch}
{\pgfqpoint{0pt}{0pt}}
{\pgfqpoint{\hatchdistance}{\hatchdistance}}
{\pgfpoint{\hatchdistance-1pt}{\hatchdistance-1pt}}%
{
    \pgfsetcolor{\tikz@pattern@color}
    \pgfsetlinewidth{\hatchthickness}
    \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
    \pgfpathlineto{\pgfqpoint{\hatchdistance}{\hatchdistance}}
    \pgfusepath{stroke}
}

\begin{axis}[
    xmin=-4,xmax=4,
    xlabel={z},
    ymin=0,ymax=1,
    axis on top,
    legend style={legend cell align=right,legend plot pos=right}] 

\addplot[name path=A,color=red,domain=-4:4,samples=100] {1/sqrt(2*pi)*exp(-x^2/2)};
\addlegendentry{z}

\path[name path=B] (axis cs:\pgfkeysvalueof{/pgfplots/xmin},0) -- (axis cs:\pgfkeysvalueof{/pgfplots/xmax},0);


\addplot+[draw,pattern=flexible hatch,pattern color=red]
fill between[
    of=A and B,
    soft clip={domain=0:1},
];
\addlegendentry{Interval 1}

%%%%%%%%%%%%%%% for orange background
\addplot+[ %forget plot,
 fill=orange,%opacity=0.3,
 ]
 fill between[
    of=A and B,
    soft clip={domain=-2:-0.5},
    ];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\addplot[area legend,pattern=flexible hatch,pattern color=cyan,draw=blue,hatch distance=5pt, hatch thickness=0.5pt]
fill between[
    of=A and B,
    soft clip={domain=-2:-0.5},
];

\addlegendentry{Interval 2}
\end{axis}
\end{tikzpicture}
\end{document}

答案1

您可以通过添加其可选参数并删除橙色区域的分隔符来绘制\addplot与图案相同的橙色区域。这应该可以修复您的图例条目。preaction = {fill = orange}\addplot

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{patterns}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\tikzset{
    hatch distance/.store in=\hatchdistance,
    hatch distance=10pt,
    hatch thickness/.store in=\hatchthickness,
    hatch thickness=2pt
}

\makeatletter
\pgfdeclarepatternformonly[\hatchdistance,\hatchthickness]{flexible hatch}
{\pgfqpoint{0pt}{0pt}}
{\pgfqpoint{\hatchdistance}{\hatchdistance}}
{\pgfpoint{\hatchdistance-1pt}{\hatchdistance-1pt}}%
{
    \pgfsetcolor{\tikz@pattern@color}
    \pgfsetlinewidth{\hatchthickness}
    \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
    \pgfpathlineto{\pgfqpoint{\hatchdistance}{\hatchdistance}}
    \pgfusepath{stroke}
}

\begin{axis}[
    xmin=-4,xmax=4,
    xlabel={z},
    ymin=0,ymax=1,
    axis on top,
    legend style={legend cell align=right,legend plot pos=right}]

\addplot[name path=A,color=red,domain=-4:4,samples=100] {1/sqrt(2*pi)*exp(-x^2/2)};
\addlegendentry{z}

\path[name path=B] (axis cs:\pgfkeysvalueof{/pgfplots/xmin},0) -- (axis cs:\pgfkeysvalueof{/pgfplots/xmax},0);


\addplot+[draw,pattern=flexible hatch,pattern color=red]
fill between[
    of=A and B,
    soft clip={domain=0:1},
];
\addlegendentry{Interval 1}

\addplot[area legend,preaction={fill=orange},pattern=flexible hatch,pattern color=cyan,draw=blue,hatch distance=5pt, hatch thickness=0.5pt]
fill between[
    of=A and B,
    soft clip={domain=-2:-0.5},
];

\addlegendentry{Interval 2}
\end{axis}
\end{tikzpicture}
\end{document}

阴谋

相关内容