自定义颜色条背景

自定义颜色条背景

我想将自定义背景颜色设置为颜色条,以下是我的代码:

\documentclass[tikz,border={12pt,12pt}]{standalone}

\usepackage{pgfplots}
\usetikzlibrary{calc}

\newcommand{\MakeMeAColorbar}[2]{
        % Min/Max/Colormap
        \pgfmathsetmacro{\Min}{#1}
        \pgfmathsetmacro{\Max}{#2}

        % Tick distance
        \pgfmathsetmacro{\XTickDistance}{
            (\Min - \Max) / (10 - 1)
        }

        % Above/Below
        \definecolor{Lcolor}{RGB}{192, 192, 192}
        \definecolor{Rcolor}{RGB}{255,   0,   0}

        % Axis
        \begin{axis}[
            hide axis, scale only axis, height=0pt, width=0pt, % hide axis
            colormap/jet,
            colorbar sampled,
            colorbar horizontal,
            point meta min=\Min,
            point meta max=\Max,
            colorbar style = {
                samples = 9,
                height = 0.5cm,
                width = 8cm,
                xtick style = {draw=none},
                xticklabel style = {
                    text width = 2.5em,
                    align = center,
                    /pgf/number format/.cd,
                        fixed,
                        fixed zerofill,
                        precision = 1,
                    /tikz/.cd
                },
                xtick distance=\XTickDistance,
            }
            ]
            \addplot [draw=none] coordinates {(0,0)};
        \end{axis}

        % Above/Below  triangle
        \def\len{0.75cm}
        \foreach \i/\j in {south east/a, north east/b, north west/c, south west/d}
            {\coordinate (\j) at (current colorbar axis.\i);}
        \filldraw[fill=Lcolor] (a) -- ($(a)!0.5!(b)+(\len,0)$) -- (b);
        \filldraw[fill=Rcolor] (c) -- ($(c)!0.5!(d)+(-\len,0)$)-- (d);
}

\begin{document}
    \begin{tikzpicture}
        \MakeMeAColorbar{1.0}{2.0}
    \end{tikzpicture}
\end{document}

即使我添加fill = black背景colorbar style仍然是透明的。

谢谢

答案1

下面的代码中指出了一种可能的方法。如果您想添加一些填充,您可以更改坐标,\fill例如([shift={(-2pt,-2pt)}]cb.outer south west)

\documentclass{article}

\usepackage{pgfplots}
\usetikzlibrary{backgrounds} % <----- added
\newcommand\MakeMeAColorbar[3][0,0]{
    % Min/Max
    \pgfmathsetmacro{\Min}{#2}
    \pgfmathsetmacro{\Max}{#3}
    % Tick distance
    \pgfmathsetmacro{\XTickDistance}{
        (\Min - \Max) / (10 - 1)
    }

    \begin{axis}[
    hide axis, scale only axis, height=0pt, width=0pt, % hide axis
    colormap/jet,
    colorbar sampled,
    colorbar horizontal,
    point meta min=\Min,
    point meta max=\Max,
    colorbar style = {
        name=cb,   % <----------- addded
        at={(#1)},anchor=center,
        samples = 9,
        height = 0.5cm,
        width = 8cm,
        xtick style = {draw=none},
        xticklabel style = {
            text width = 2.5em,
            align = center,
            /pgf/number format/.cd,
                fixed,
                fixed zerofill,
                precision = 2,
            /tikz/.cd
        },
        xtick distance=\XTickDistance,
    }
    ]
    \addplot [draw=none] coordinates {(0,0)};
    \end{axis}

    % the following two lines are added
    \scoped[on background layer]
    \fill [red] (cb.outer south west) rectangle (cb.outer north east);
}

\begin{document}

\begin{tikzpicture}[remember picture]

\MakeMeAColorbar{1.5}{2}

\end{tikzpicture}

\end{document}

答案2

backgrounds图书馆的帮助下,可以用一种颜色填充background rectangle每个礼物。tikzpicture

\documentclass[tikz,border={12pt,12pt}]{standalone}

\usepackage{pgfplots}
\usetikzlibrary{calc, backgrounds}

\newcommand{\MakeMeAColorbar}[2]{
        % Min/Max/Colormap
        \pgfmathsetmacro{\Min}{#1}
        \pgfmathsetmacro{\Max}{#2}

        % Tick distance
        \pgfmathsetmacro{\XTickDistance}{
            (\Min - \Max) / (10 - 1)
        }

        % Above/Below
        \definecolor{Lcolor}{RGB}{192, 192, 192}
        \definecolor{Rcolor}{RGB}{255,   0,   0}

        % Axis
        \begin{axis}[
            hide axis, scale only axis, height=0pt, width=0pt, % hide axis
            colormap/jet,
            colorbar sampled,
            colorbar horizontal,
            point meta min=\Min,
            point meta max=\Max,
            colorbar style = {
                samples = 9,
                height = 0.5cm,
                width = 8cm,
                xtick style = {draw=none},
                xticklabel style = {
                    text width = 2.5em,
                    align = center,
                    /pgf/number format/.cd,
                        fixed,
                        fixed zerofill,
                        precision = 1,
                    /tikz/.cd
                },
                xtick distance=\XTickDistance,
            }
            ]
            \addplot [draw=none] coordinates {(0,0)};
        \end{axis}

        % Above/Below  triangle
        \def\len{0.75cm}
        \foreach \i/\j in {south east/a, north east/b, north west/c, south west/d}
            {\coordinate (\j) at (current colorbar axis.\i);}
        \filldraw[fill=Lcolor] (a) -- ($(a)!0.5!(b)+(\len,0)$) -- (b);
        \filldraw[fill=Rcolor] (c) -- ($(c)!0.5!(d)+(-\len,0)$)-- (d);
}

\begin{document}
    \begin{tikzpicture}[show background rectangle, 
         background rectangle/.style={fill=purple!20}]
        \MakeMeAColorbar{1.0}{2.0}
    \end{tikzpicture}
\end{document}

在此处输入图片描述

另一个解决方案可能是包含tikzpicture在内\colorbox。在这种情况下,不需要backgrounds库。

注意:在下面的代码中,选项tikz已被从文档选项中删除。

\documentclass[border={12pt,12pt}]{standalone}

\usepackage{pgfplots}
\usetikzlibrary{calc}

\newcommand{\MakeMeAColorbar}[2]{
        % Min/Max/Colormap
        \pgfmathsetmacro{\Min}{#1}
        \pgfmathsetmacro{\Max}{#2}

        % Tick distance
        \pgfmathsetmacro{\XTickDistance}{
            (\Min - \Max) / (10 - 1)
        }

        % Above/Below
        \definecolor{Lcolor}{RGB}{192, 192, 192}
        \definecolor{Rcolor}{RGB}{255,   0,   0}

        % Axis
        \begin{axis}[
            hide axis, scale only axis, height=0pt, width=0pt, % hide axis
            colormap/jet,
            colorbar sampled,
            colorbar horizontal,
            point meta min=\Min,
            point meta max=\Max,
            colorbar style = {
                samples = 9,
                height = 0.5cm,
                width = 8cm,
                xtick style = {draw=none},
                xticklabel style = {
                    text width = 2.5em,
                    align = center,
                    /pgf/number format/.cd,
                        fixed,
                        fixed zerofill,
                        precision = 1,
                    /tikz/.cd
                },
                xtick distance=\XTickDistance,
            }
            ]
            \addplot [draw=none] coordinates {(0,0)};
        \end{axis}

        % Above/Below  triangle
        \def\len{0.75cm}
        \foreach \i/\j in {south east/a, north east/b, north west/c, south west/d}
            {\coordinate (\j) at (current colorbar axis.\i);}
        \filldraw[fill=Lcolor] (a) -- ($(a)!0.5!(b)+(\len,0)$) -- (b);
        \filldraw[fill=Rcolor] (c) -- ($(c)!0.5!(d)+(-\len,0)$)-- (d);
}

\begin{document}
\colorbox{orange!30}{\tikz \MakeMeAColorbar{1.0}{2.0};}
\end{document}

在此处输入图片描述

相关内容