我正在编写一个函数来根据以下内容在 beamer 中生成图例:
- 页面上的位置
- 最大值和最小值
- 要使用的颜色图
以下是一个例子:
\documentclass[table]{beamer}
\usepackage{tikz,pgfplots}
\usetikzlibrary{calc}
\usetikzlibrary{backgrounds}
% Colormaps
\pgfplotsset{
colormap={Legend1}{
rgb255=(255, 113, 0),
rgb255=(255, 227, 0),
rgb255=(170, 255, 0),
rgb255=( 57, 255, 0),
rgb255=( 40, 255, 185),
rgb255=( 0, 199, 221),
rgb255=( 21, 121, 255),
}
}
% Len of above/below triangle
\def\len{0.75cm}
% Horizontal Legend
\newcommand{\LegendH}[4]{
\begin{tikzpicture}[remember picture,overlay]
% Min/Max/Colormap
\pgfmathsetmacro{\Min}{#2}
\pgfmathsetmacro{\Max}{#3}
\def\colormap{#4}
% Tick distance
\pgfmathsetmacro{\XTickDistance}{
(\Min - \Max) / \pgfplotscolormapsizeof{\colormap}
}
% Above/Below
\ifnum\pdfstrcmp{\colormap}{Legend1}=0
\definecolor{Lcolor}{RGB}{192, 192, 192}
\definecolor{Rcolor}{RGB}{255, 0, 0}
\def\precision{2}
\fi
% Axis
\begin{axis}[
hide axis, scale only axis, height=0pt, width=0pt, % hide axis
colormap name = \colormap,
colorbar sampled,
colorbar horizontal,
point meta min=\Min,
point meta max=\Max,
colorbar style = {
name = cb,
at={(#1)}, anchor=center,
samples = \pgfplotscolormapsizeof{\colormap} + 1,
height = 0.5cm,
width = 10cm,
xtick style = {draw=none},
xticklabel style = {
text width = 2.5em,
align = center,
/pgf/number format/.cd,
fixed,
fixed zerofill,
precision = \precision,
/tikz/.cd
},
xtick distance=\XTickDistance,
}
]
\addplot [draw=none] coordinates {(0,0)};
\end{axis}
% Above/Below triangle
\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);
% Background
\scoped[on background layer]
\fill [white] ([shift={(-0.25cm,-0.5cm)}]cb.outer south west) rectangle ([shift={(+0.0cm,+0.5cm)}]cb.outer north east);
\end{tikzpicture}
}
\begin{document}
\begin{frame}{Legend}
\LegendH{$(current page.center)+(0cm, -2cm)$}{1.0}{2.0}{Legend1}
\LegendH{$(current page.center)+(0cm, +0cm)$}{1.0}{3.0}{Legend1}
\end{frame}
\end{document}
正如您所见,下方的图例(1 和 2 之间)是正确的。
上面的是错误的:应该从 1 开始到 3 结束,刻度应该是 1.00、1.29、1.57、1.86、2.14、2.43、2.71、3.00。
我不明白为什么会有这种奇怪的行为。
谢谢