我正在尝试根据公司设计定义一些颜色循环列表。因此,它们应该可供用户稍后选择。应该为条形图、饼图和常规线图提供定义。线图一切正常,但我对条形图的定义感到困惑。这是我目前在 MWE 中的情况,我不知道为什么我只得到黑色边框的条形图:
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{/pgfplots/bar cycle list/.style={/pgfplots/cycle list={
{blue,fill=blue!30!white,mark=none},
{red,fill=red!30!white,mark=none},
{brown!60!black,fill=brown!30!white,mark=none},
{black,fill=gray,mark=none},},},}
\begin{document}
\begin{tikzpicture}
\begin{axis}[ybar,enlargelimits=0.15]
\addplot[] coordinates{(5,10) (10,15) (15,5) (20,24) (25,30)};
\addplot[] coordinates{(5,3) (10,5) (15,15) (20,20) (25,35)};
\end{axis}
\end{tikzpicture}
\end{document}
结果如下:
如何根据定义的循环列表实现条形的着色?
另外是否有类似的选项\pgfplotscreate<bar>plotcyclelist
可用于存储命名列表并稍后通过它加载\pgfplotsset{<bar> cycle list name=}
?
答案1
pgf 图手动的(v1.17 (2020/02/29),第 214 页) 指出以下内容:
\addplot+[〈keys〉] ...;
因此,如果您写入或者不使用方括号, 则将使用当前活动的循环列表, 如\addplot[〈explicit plot specification〉]...;
虽然解释不是很清楚,但这意味着您应该删除方括号,因为它们会覆盖当前活动的循环列表:
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{/pgfplots/bar cycle list/.style={/pgfplots/cycle list={
{blue,fill=blue!30!white,mark=none},
{red,fill=red!30!white,mark=none},
{brown!60!black,fill=brown!30!white,mark=none},
{black,fill=gray,mark=none},},},}
\begin{document}
\begin{tikzpicture}
\begin{axis}[ybar,enlargelimits=0.15]
\addplot coordinates{(5,10) (10,15) (15,5) (20,24) (25,30)};
\addplot coordinates{(5,3) (10,5) (15,15) (20,20) (25,35)};
\end{axis}
\end{tikzpicture}
\end{document}
也可以先存储列表,然后供条形图使用。存储列表是使用命令完成的\pgfplotscreateplotcyclelist{listname}{list specification}
,该命令可以与轴选项一起使用cycle list name
。
MWE(结果与上同):
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotscreateplotcyclelist{customlist}{
{blue,fill=blue!30!white,mark=none},
{red,fill=green!30!white,mark=none},
{brown!60!black,fill=brown!30!white,mark=none},
{black,fill=gray,mark=none}
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[ybar,enlargelimits=0.15,cycle list name=customlist]
\addplot coordinates{(5,10) (10,15) (15,5) (20,24) (25,30)};
\addplot coordinates{(5,3) (10,5) (15,15) (20,20) (25,35)};
\end{axis}
\end{tikzpicture}
\end{document}
如果要在 中使用名称自定义列表,pgfplotsset
则需要语法/pgfplots/bar cycle list/.style={/pgfplots/cycle list name={listname}}
。此设置将一直有效,直到加载新列表,而无需将其指定为各个图的选项。
梅威瑟:
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotscreateplotcyclelist{customlist}{
{blue,fill=blue!30!white,mark=none},
{orange,fill=orange!30!white,mark=none},
{brown!60!black,fill=brown!30!white,mark=none},
{black,fill=gray,mark=none}
}
\pgfplotscreateplotcyclelist{otherlist}{
{blue,fill=blue!30!white,mark=none},
{green,fill=green!30!white,mark=none},
{brown!60!black,fill=brown!30!white,mark=none},
{black,fill=gray,mark=none}
}
\pgfplotsset{/pgfplots/bar cycle list/.style={/pgfplots/cycle list name={otherlist}}}
\begin{document}
\begin{tikzpicture}
\begin{axis}[ybar,enlargelimits=0.15]
\addplot coordinates{(5,10) (10,15) (15,5) (20,24) (25,30)};
\addplot coordinates{(5,3) (10,5) (15,15) (20,20) (25,35)};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[ybar,enlargelimits=0.15]
\addplot coordinates{(5,10) (10,15) (15,5) (20,24) (25,30)};
\addplot coordinates{(5,3) (10,5) (15,15) (20,20) (25,35)};
\end{axis}
\end{tikzpicture}
\pgfplotsset{/pgfplots/bar cycle list/.style={/pgfplots/cycle list name={customlist}}}
\begin{tikzpicture}
\begin{axis}[ybar,enlargelimits=0.15]
\addplot coordinates{(5,10) (10,15) (15,5) (20,24) (25,30)};
\addplot coordinates{(5,3) (10,5) (15,15) (20,20) (25,35)};
\end{axis}
\end{tikzpicture}
\end{document}
结果: