我有一系列pgfplots
条形图,它们都使用默认颜色图来定义条形颜色。由于空间问题,我需要使用单独的图例tikzpicture
(参见 MWE)。但是,我无法找到addlegendimage
直接从默认颜色中获取颜色的方法。我该如何修改 MWE 以使项目边框和填充与默认颜色图匹配?
平均能量损失
\documentclass[10pt]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
hide axis,
xmin=10,
xmax=50,
ymin=0,
ymax=0.4,
ybar,
legend cell align={left}, % Align text left in legend
legend style={font=\small},
legend columns=5,
% transpose legend,
]
\addlegendimage{color of colormap={1},fill}
\addlegendentry{Item 1};
\addlegendimage{color of colormap={2},fill}
\addlegendentry{Item 2};
\addlegendimage{color of colormap={3},fill}
\addlegendentry{Item 3};
\addlegendimage{color of colormap={3},fill}
\addlegendentry{Item 4};
\addlegendimage{color of colormap={3},fill}
\addlegendentry{Item 5};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
根据我对这个问题的理解,你正在寻找的默认颜色bar cycle list
,这与没有太大关系color of colormap
。在 v1.17 中,它可以在手册第 86 页找到,并由以下公式给出:
\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},
}, },
}
因此,您只需将其复制到样式中即可获得 \documentclass[10pt]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
hide axis,
xmin=10,
xmax=50,
ymin=0,
ymax=0.4,
ybar,
legend cell align={left}, % Align text left in legend
legend style={font=\small},
legend columns=5,
% transpose legend,
]
\addlegendimage{blue,fill=blue!30!white,mark=none}
\addlegendentry{Item 1};
\addlegendimage{red,fill=red!30!white,mark=none}
\addlegendentry{Item 2};
\addlegendimage{brown!60!black,fill=brown!30!white,mark=none}
\addlegendentry{Item 3};
\end{axis}
\end{tikzpicture}
\end{document}
这是一个更好的答案,完全是从这个很好的答案. 只需添加虚拟图即可。
\documentclass[10pt]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
hide axis,
xmin=10,
xmax=50,
ymin=0,
ymax=0.4,
ybar,
legend cell align={left}, % Align text left in legend
legend style={font=\small},
legend columns=5,
ybar legend
% transpose legend,
]
\pgfplotsinvokeforeach{1,...,5}{%
\addplot coordinates {(0,0)};
\addlegendentry{Item #1}} % make six dummy plots
\end{axis}
\end{tikzpicture}
\end{document}