删除区域图例矩形周围的边框

删除区域图例矩形周围的边框

我有一个图例,可以选择area legend在图例中绘制一个小矩形而不是线段。矩形周围有一个边框,我想删除它或将其设置为与实际矩形相同的颜色。如何做到这一点?

答案1

要隐藏绘图和图例中的边框,您应该draw opacity=0\addplot选项中使用而不是draw=none

\documentclass[border=5mm] {standalone}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}
\addplot [fill=yellow,draw opacity=0, area legend] coordinates {(0,0) (2,1) (4,1)} \closedcycle;
\addlegendentry{Plot}
\end{axis}
\end{tikzpicture}

\end{document}

答案2

另一种可能性是改变area legend样式以使其draw=none真正生效。(示例取自pgfplots手册第 173 页。)

代码

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{
    /pgfplots/area legend/.style={%
        /pgfplots/legend image code/.code={%
            \fill[##1] (0cm,-0.1cm) rectangle (0.6cm,0.1cm);
        }%
    },
}
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    area legend,
    axis x line=bottom,
    axis y line=left,
    domain=0:1,
    legend style={at={(0.03,0.97)},anchor=north west},
    axis on top,xmin=0]
\addplot[
    pattern=crosshatch dots,
    pattern color=blue,
    draw=none,
    samples=500] {sqrt(x)} \closedcycle;
\addplot[
    pattern=crosshatch,
    pattern color=blue!30!white,
    draw=blue!30!white] {x^2} \closedcycle;
\addplot[red,line legend] coordinates {(0,0) (1,1)};
\legend{$\sqrt x$,$x^2$,$x$}
\end{axis}
\end{tikzpicture}
\end{document}

输出

在此处输入图片描述

相关内容