我有以下简单的 PGFPlots 示例
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=0, xmax=1,
ymin=0, ymax=1,
width=7.5cm,
colorbar,
colormap={mymap}{[1pt]
rgb(0pt)=(0,0,0.5);
rgb(22pt)=(0,0,1);
rgb(25pt)=(0,0,1);
rgb(68pt)=(0,0.86,1);
rgb(70pt)=(0,0.9,0.967741935483871);
rgb(75pt)=(0.0806451612903226,1,0.887096774193548);
rgb(128pt)=(0.935483870967742,1,0.0322580645161291);
rgb(130pt)=(0.967741935483871,0.962962962962963,0);
rgb(132pt)=(1,0.925925925925926,0);
rgb(178pt)=(1,0.0740740740740741,0);
rgb(182pt)=(0.909090909090909,0,0);
rgb(200pt)=(0.5,0,0)
},
point meta min=12.0628665990324,
point meta max=98.5559785610705,
colorbar style={
ytick={20,30,40,50,60,70,80,90},
yticklabels={20,30,40,50,60,70,80,90}
}
]
\path [draw=black, fill=blue, opacity=0.4]
(axis cs:0.722443382570222,0.322958913853178)--
(axis cs:0.361788655622314,0.228263230878956)--
(axis cs:0.293714046388829,0.630976123854488)--
cycle;
\end{axis}
\end{tikzpicture}
\end{document}
并希望用颜色图的值填充路径,即point meta
。如何做?
答案1
这需要直接访问颜色图函数pgfplots
:
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=0, xmax=1,
ymin=0, ymax=1,
width=7.5cm,
colorbar,
colormap={mymap}{[1pt]
rgb(0pt)=(0,0,0.5);
rgb(22pt)=(0,0,1);
rgb(25pt)=(0,0,1);
rgb(68pt)=(0,0.86,1);
rgb(70pt)=(0,0.9,0.967741935483871);
rgb(75pt)=(0.0806451612903226,1,0.887096774193548);
rgb(128pt)=(0.935483870967742,1,0.0322580645161291);
rgb(130pt)=(0.967741935483871,0.962962962962963,0);
rgb(132pt)=(1,0.925925925925926,0);
rgb(178pt)=(1,0.0740740740740741,0);
rgb(182pt)=(0.909090909090909,0,0);
rgb(200pt)=(0.5,0,0)
},
point meta min=12.0628665990324,
point meta max=98.5559785610705,
colorbar style={
ytick={20,30,40,50,60,70,80,90},
yticklabels={20,30,40,50,60,70,80,90}
}
]
\path [
/utils/exec={
% map linearly from [0:1000] into the colormap. 500 is in the
% middle:
\pgfplotscolormapdefinemappedcolor{500}
},
draw=black, fill=mapped color]
(0,0)-- (0.5,0.2) -- (0,0.4) -- cycle;
\path [
/utils/exec={
\pgfplotsset{colormap access=direct}
% direct access using an index. Yours has 201 elements:
\pgfplotscolormapdefinemappedcolor{127}
},
draw=black, fill=mapped color]
(0.722443382570222,0.322958913853178)--
(0.361788655622314,0.228263230878956)--
(0.293714046388829,0.630976123854488)--
cycle;
\end{axis}
\end{tikzpicture}
\end{document}
由于目前没有“使用颜色映射值”之类的键,因此您必须求助于mapped color
如上所述定义的宏。我使用了/utils/exec
,这是一种 PGF 方式,它允许您在需要键值对的上下文中调用宏。
答案2
与 PGFPlots v1.13 版本一样,您可以使用新键color of colormap
或index of colormap
轻松访问颜色图的颜色。请参阅手册中的第 4.7.6 节第 192f 页。
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=0, xmax=1,
ymin=0, ymax=1,
width=7.5cm,
colorbar,
colormap={mymap}{[1pt]
rgb(0pt)=(0,0,0.5);
rgb(22pt)=(0,0,1);
rgb(25pt)=(0,0,1);
rgb(68pt)=(0,0.86,1);
rgb(70pt)=(0,0.9,0.967741935483871);
rgb(75pt)=(0.0806451612903226,1,0.887096774193548);
rgb(128pt)=(0.935483870967742,1,0.0322580645161291);
rgb(130pt)=(0.967741935483871,0.962962962962963,0);
rgb(132pt)=(1,0.925925925925926,0);
rgb(178pt)=(1,0.0740740740740741,0);
rgb(182pt)=(0.909090909090909,0,0);
rgb(200pt)=(0.5,0,0)
},
point meta min=12.0628665990324,
point meta max=98.5559785610705,
colorbar style={
ytick={20,30,40,50,60,70,80,90},
yticklabels={20,30,40,50,60,70,80,90},
}
]
\path [color of colormap=500,draw=.!80!black, fill=., opacity=0.4]
(0,0)-- (0.5,0.2) -- (0,0.4) -- cycle;
\path [index of colormap=128,draw=.!80!black, fill=., opacity=0.4]
(0.722443382570222,0.322958913853178) --
(0.361788655622314,0.228263230878956) --
(0.293714046388829,0.630976123854488) --
cycle;
\end{axis}
\end{tikzpicture}
\end{document}