我想创建一个自定义颜色条,目前我有以下代码:
\documentclass[tikz,border={12pt,12pt}]{standalone}
\usepackage{tikz,pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
hide axis, scale only axis, height=0pt, width=0pt, % hide axis
colormap/jet,
colorbar sampled,
colorbar horizontal,
point meta min = 20,
point meta max = 40,
colorbar style = {
samples = 10,
height = 0.5cm,
width = 10cm,
xticklabel style = {
text width = 2.5em,
align = center,
/pgf/number format/.cd,
fixed,
fixed zerofill,
precision = 1,
/tikz/.cd
}
}
]
\addplot [draw=none] coordinates {(0,0)};
\end{axis}
\end{tikzpicture}
\end{document}
- 添加延长(上/下箭头)像本例的最后一个颜色条http://matplotlib.org/examples/api/colorbar_only.html
- 指定公正
point meta min
,point meta max
并且样品因此每个颜色区域和刻度都对齐,而无需使用xtick = {20,22.22,...,40}
- 删除垂直虚线绿色的每笔交易的线
- 使用 RGB 定义的自定义颜色图,例如:
[ [191, 191, 191],
[255, 255, 0 ],
[255, 201, 0 ],
[255, 150, 0 ],
[255, 79 , 0 ],
[255, 25 , 0 ],
[229, 0 , 25 ],
[176, 0 , 79 ],
[105, 0 , 150],
[54 , 0 , 201],
[0 , 0 , 255],
[102, 102, 102] ]
非常感谢。
答案1
以下代码应该能满足您的所有需求,包括已给出的评论和答案。有关更多详细信息,请查看代码中的评论。
% (used PGFPlots v1.14)
\documentclass[border={2pt}]{standalone}
\usepackage{pgfplots}
\pgfplotsset{
% define the custom colormap
colormap={my colormap}{
rgb255=(191, 191, 191),
rgb255=(255, 255, 0 ),
rgb255=(255, 201, 0 ),
rgb255=(255, 150, 0 ),
rgb255=(255, 79 , 0 ),
rgb255=(255, 25 , 0 ),
rgb255=(229, 0 , 25 ),
rgb255=(176, 0 , 79 ),
rgb255=(105, 0 , 150),
rgb255=(54 , 0 , 201),
rgb255=(0 , 0 , 255),
rgb255=(102, 102, 102),
},
}
\begin{document}
\begin{tikzpicture}
% define `point meta min' and `point meta max' values ...
\pgfmathsetmacro{\PointMetaMin}{20}
\pgfmathsetmacro{\PointMetaMax}{40}
% ... and calculate from that the `tick distance' value
\pgfmathsetmacro{\XTickDistance}{
(\PointMetaMin - \PointMetaMax)
/
(\pgfplotscolormapsizeof{my colormap} - 2)
}
\begin{axis}[
hide axis,
scale only axis,
height=0pt,
width=0pt,
%
% use defined custom colormap
colormap name=my colormap,
%
colorbar sampled,
colorbar horizontal,
point meta min=\PointMetaMin,
point meta max=\PointMetaMax,
colorbar style={
samples=10,
height=0.5cm,
width=10cm,
% don't draw ticks
xtick style={
draw=none,
},
xticklabel style={
text width=2.5em,
align=center,
/pgf/number format/.cd,
fixed,
fixed zerofill,
precision=1,
/tikz/.cd,
},
% specify number ticks indirectly by calculating the distance
% between the ticks
xtick distance=\XTickDistance,
% % (in case of some numerical issues it could be that the
% % min or max value isn't drawn; then add it "by hand")
% extra x ticks={20},
},
]
\addplot [draw=none] coordinates {(0,0)};
\end{axis}
% ---------------------------------------------------------------------
% code borrowed/copied from Salim Bou
% <http://tex.stackexchange.com/a/332352/95441>
\def\len{5mm}
\definecolor{leftcolor}{RGB}{191, 191, 191}
\definecolor{rightcolor}{RGB}{102, 102, 102}
\foreach \i/\j in {
south east/a,
east/b,
north east/c,
north west/e,
west/f,
south west/g%
}{
\coordinate (\j) at (current colorbar axis.\i);
}
\filldraw [fill=leftcolor] (a) -- ([xshift=\len] b) -- (c);
\filldraw [fill=rightcolor] (e) -- ([xshift=-\len]f) -- (g);
% ---------------------------------------------------------------------
\end{tikzpicture}
\end{document}
答案2
对于第一个问题,颜色条本身是一个轴,你可以使用以下方式引用该轴current colorbar axis
您需要在axis
环境之后添加此部分
\def\len{5mm}
\definecolor{leftcolor}{RGB}{191, 191, 191}
\definecolor{rightcolor}{RGB}{102, 102, 102}
\foreach \i/\j in {south east/a,east/b,north east/c,north west/e,west/f,south west/g}
{\coordinate (\j) at (current colorbar axis.\i);}
\filldraw[fill=leftcolor] (a) -- ([xshift=\len]b) -- (c);
\filldraw[fill=rightcolor] (e) -- ([xshift=-\len]f)-- (g);
控制\len
两个三角形的长度,leftcolor
以及rightcolor
它们的颜色
\documentclass[tikz,border={12pt,12pt}]{standalone}
\usepackage{tikz,pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
hide axis, scale only axis, height=0pt, width=0pt, % hide axis
colormap/jet,
colorbar sampled,
colorbar horizontal,
point meta min = 20,
point meta max = 40,
colorbar style = {
samples = 10,
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 = 1,
/tikz/.cd
}
}
]
\addplot [draw=none] coordinates {(0,0)};
\end{axis}
\def\len{5mm}
\definecolor{leftcolor}{RGB}{191, 191, 191}
\definecolor{rightcolor}{RGB}{102, 102, 102}
\foreach \i/\j in {south east/a,east/b,north east/c,north west/e,west/f,south west/g}
{\coordinate (\j) at (current colorbar axis.\i);}
\filldraw[fill=leftcolor] (a) -- ([xshift=\len]b) -- (c);
\filldraw[fill=rightcolor] (e) -- ([xshift=-\len]f)-- (g);
\end{tikzpicture}
\end{document}