如何使用 xcolor 自定义绘图颜色?

如何使用 xcolor 自定义绘图颜色?

我正在尝试在同一张图上绘制多个数据源,并用颜色区分它们。这是一个用于复制我所得到的错误的最小环境。

\documentclass[a4paper,12pt,openany]{book}
  
\usepackage{xcolor}
\usepackage{pgfplots}

\definecolor{LB1}{RGB}{173,216,230}
\definecolor{LB2}{RGB}{135,206,250}
\definecolor{LB3}{RGB}{0,191,255}
\definecolor{LB4}{RGB}{30,144,255}
\definecolor{LB5}{RGB}{70,130,180}
\definecolor{LB6}{RGB}{123,104,238}
\definecolor{LB7}{RGB}{0,0,255}
\definecolor{LB8}{RGB}{75,0,130}
\definecolor{LB9}{RGB}{138,43,226}

\begin{document}

\begin{figure}[!h]
    \centering
    \begin{tikzpicture}[scale=0.9]
    \begin{axis}[
        legend style={at={(1,1)},anchor=north west},
        xlabel={$x$},
        ylabel={$y$},
        ymajorgrids=true,
        xmajorgrids=true,
        grid style=dashed,
    ]

    \addplot[
        color=\color{LB1},
        mark=square,
    ]
    coordinates {(2,3)(6,5)};
    \addlegendentry{$r=0.1$}

    \end{axis}
    \end{tikzpicture}
\end{figure}

\end{document}

我遇到很多错误。

! Missing \endcsname inserted.

! Extra \endcsname.
\pgfutil@colorlet ...g \csname color@#2\endcsname 

! Argument of \XC@col@rlet has an extra }.

Runaway argument?
{tikz@color}{\@undeclaredcolor }\def \reserved@b {\@declaredcolor }\futurelet \
ETC.
! Paragraph ended before \XC@col@rlet was complete.

! Package xcolor Error: Undefined color `\protect \let \reserved@d =[\def \par

! Package xcolor Error: Undefined color `tikz@color'.

我该如何修复这些错误?

答案1

Henri Menke 已经提供了解决方案:使用color=LB1。但是如果您想定义自己的颜色,您也可以定义一个cycle list,从中pgfplots自动选择颜色(和标记),而无需手动修复它。

(注:您还可以查看https://tex.stackexchange.com/a/134368/1952了解addplotaddplot+[...]和之间的区别addplot[...]。)

\documentclass[a4paper,12pt,openany]{book}
  
\usepackage{xcolor}
\usepackage{pgfplots}

\definecolor{LB1}{RGB}{173,216,230}
\definecolor{LB2}{RGB}{135,206,250}
\definecolor{LB3}{RGB}{0,191,255}
\definecolor{LB4}{RGB}{30,144,255}
\definecolor{LB5}{RGB}{70,130,180}
\definecolor{LB6}{RGB}{123,104,238}
\definecolor{LB7}{RGB}{0,0,255}
\definecolor{LB8}{RGB}{75,0,130}
\definecolor{LB9}{RGB}{138,43,226}

\pgfplotscreateplotcyclelist{mycolors}{
LB1,every mark/.append style={fill=LB1!80!black},mark=*\\
LB2,every mark/.append style={fill=LB2!80!black},mark=square*\\
LB3,every mark/.append style={fill=LB3!80!black},mark=otimes*\\
LB4,mark=star\\
LB5,every mark/.append style={fill=LB5!80!black},mark=diamond*\\
LB6,densely dashed,every mark/.append style={solid,fill=LB6!80!black},mark=*\\
LB7,densely dashed,every mark/.append style={LB7,fill=LB7!80!black},mark=square*\\
LB8,densely dashed,every mark/.append style={LB8,fill=LB8!80!black},mark=square*\\
LB9,densely dashed,every mark/.append style={solid,fill=LB9},mark=otimes*\\
}

\begin{document}

\begin{figure}[!h]
    \centering
    \begin{tikzpicture}[scale=0.9]
    \begin{axis}[
        legend style={at={(1,1)},anchor=north west},
        xlabel={$x$},
        ylabel={$y$},
        ymajorgrids=true,
        xmajorgrids=true,
        grid style=dashed,
        cycle list name=mycolors,
    ]

    \addplot
    coordinates {(0,1)(5,1)};
    \addplot
    coordinates {(0,2)(5,2)};
    \addplot
    coordinates {(0,3)(5,3)};
    \addplot
    coordinates {(0,4)(5,4)};
    \addplot
    coordinates {(0,5)(5,5)};
    \addplot
    coordinates {(0,6)(5,6)};
    \addplot
    coordinates {(0,7)(5,7)};
    \addplot
    coordinates {(0,8)(5,8)};
    \addplot
    coordinates {(0,9)(5,9)};
    \end{axis}
    \end{tikzpicture}
\end{figure}

\end{document}

在此处输入图片描述

相关内容