我正在編寫一個函數來根據以下內容在投影機中生成圖例:
- 頁面上的位置
- 最大值和最小值
- 要使用的顏色圖
這是一個例子:
\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。
我不明白為什麼會有這種奇怪的行為。
謝謝