代码

代码

与问题相关标题中的 tikz 图例我想使用colorbrewer 库(只需将档案解压到工作目录中即可)。但是,它们似乎不能一起使用。

\documentclass{book}
\usepackage{lipsum}
\usepackage{caption}
\usepackage{pgfplots}
\pgfplotsset{compat=1.3}
\usepgfplotslibrary{colorbrewer}

\begin{document}
\lipsum[1]
\begin{figure}[!ht]
\centering
\captionsetup{width=9.5cm}
\pgfplotsset{every axis plot post/.append style={mark=none,line width=1.5pt}}
\begin{tikzpicture}
\begin{axis}[height=7cm,width=9cm,grid=major,xlabel=$x$,ylabel=$f(x)$,
tick label style={font=\footnotesize},label style={font=\small},max space between ticks=45, major tick length=0cm,minor tick length=0cm,enlargelimits=false,colorbrewer cycle list=Dark2]
\addplot{2*x};\label{p4}
\addplot{0.5*x*x};\label{p5}
\addplot{-0.125*x*x*x};\label{p6}
\end{axis}
\end{tikzpicture}
\caption[Caption in ToC]{This is a plot about colored curves: $f(x)=2 x$ (\ref{p4}),  $f(x)=0.5 x^2$ (\ref{p5}), and $f(x)=-0.125 x^3$ (\ref{p6})}
\end{figure}
\lipsum[1]
\end{document}

我得到了错误

! Package pgfkeys Error: I do not know the key '/tikz/colorbrewer cycle list' a

更一般地,是否可以使用自定义颜色循环? 在我注释掉 行之前,以下代码不起作用pgfplotsset

\documentclass{book}
\usepackage{lipsum}
\usepackage{caption}
\usepackage{pgfplots}
\pgfplotsset{compat=1.3}
\definecolor{cb_dark2_1}{RGB}{27,158,119}
\definecolor{cb_dark2_2}{RGB}{217,95,2}
\definecolor{cb_dark2_3}{RGB}{117,112,179}
\definecolor{cb_dark2_4}{RGB}{231,41,138}
\definecolor{cb_dark2_5}{RGB}{102,166,30}
\definecolor{cb_dark2_6}{RGB}{230,171,2}
\definecolor{cb_dark2_7}{RGB}{166,118,29}
\definecolor{cb_dark2_8}{RGB}{102,102,102}
% uncomment the next line for working example
\pgfplotsset{cycle list={cb_dark2_1\\ cb_dark2_2\\ cb_dark2_3\\ cb_dark2_4\\ cb_dark2_5\\ cb_dark2_6\\ cb_dark2_7\\ cb_dark2_8},
}
\begin{document}
\lipsum[1]
\begin{figure}[!ht]
\centering
\captionsetup{width=9.5cm}
\pgfplotsset{every axis plot post/.append style={mark=none,line width=1.5pt}}
\begin{tikzpicture}
\begin{axis}[height=7cm,width=9cm,grid=major,xlabel=$x$,ylabel=$f(x)$,
tick label style={font=\footnotesize},label style={font=\small},max space between ticks=45, major tick length=0cm,minor tick length=0cm,enlargelimits=false]
\addplot{2*x};\label{p4}
\addplot{0.5*x*x};\label{p5}
\addplot{-0.125*x*x*x};\label{p6}
\end{axis}
\end{tikzpicture}
\caption[Caption in ToC]{This is a plot about colored curves: $f(x)=2 x$ (\ref{p4}),  $f(x)=0.5 x^2$ (\ref{p5}), and $f(x)=-0.125 x^3$ (\ref{p6})}
\end{figure}
\lipsum[1]
\end{document}

答案1

出现此问题的原因是您使用了错误的循环列表名称和错误的 pgf 键。您必须使用cycle list name=Name,以及正确的循环名称,如Dark2-5,请参阅 中的列表定义pgflibrarypgfplots.colorbrewer.code.tex

代码

\documentclass{book}
\usepackage{lipsum}
\usepackage{caption}
\usepackage{pgfplots}
\pgfplotsset{compat=1.3}
\usepgfplotslibrary{colorbrewer}

\begin{document}
    \lipsum[1]
    \begin{figure}[!ht]
        \centering
        \captionsetup{width=9.5cm}
        \pgfplotsset{every axis plot post/.append style={mark=none,line width=1.5pt}}
        \begin{tikzpicture}
        \begin{axis}[height=7cm,
                     width=9cm,
                     grid=major,
                     xlabel=$x$,
                     ylabel=$f(x)$,
                     tick label style={font=\footnotesize},
                     label style={font=\small},
                     max space between ticks=45,
                     major tick length=0cm,
                     minor tick length=0cm,
                     enlargelimits=false,
                     cycle list name=Dark2-5] %Here
        \addplot{2*x};\label{p4}
        \addplot{0.5*x*x};\label{p5}
        \addplot{-0.125*x*x*x};\label{p6}
        \end{axis}
        \end{tikzpicture}
        \caption[Caption in ToC]{This is a plot about colored curves: $f(x)=2 x$ (\ref{p4}),  $f(x)=0.5 x^2$ (\ref{p5}), and $f(x)=-0.125 x^3$ (\ref{p6})}
    \end{figure}
    \lipsum[1]
\end{document}

结果

在此处输入图片描述

定义循环列表的问题

对于第二部分,您需要使用换行符来关闭循环列表定义\\\pgfplotsset{cycle list={cb_dark2_1\\ cb_dark2_2\\ cb_dark2_3\\ cb_dark2_4\\ cb_dark2_5\\ cb_dark2_6\\ cb_dark2_7\\ cb_dark2_8\\}}

相关内容