我想用 tikz 创建图像,只使用预定义颜色图或阴影的一部分。更准确地说,如果我有 jet-colormap
我喜欢创建几个使用 jet 颜色图的矩形,但不是全部范围,而是其中的一部分:
我尝试了使用颜色图的不同方法,例如直接通过颜色条(示例 1)和使用阴影(示例 2)。但我没有想出解决方案。
示例 1
\documentclass[]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\pgfplotsset{ticks=none}
\begin{axis}[
hide axis,
scale only axis,
height=0pt,
width=0pt,
colormap/jet,
colorbar horizontal,
colorbar style={
width=10cm,
}]
\addplot [draw=none] coordinates {(0,0)};
\end{axis}
\end{tikzpicture}
\end{document}
示例 2
\documentclass[]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\definecolor{C1}{RGB}{19,128,67}
\definecolor{C2}{RGB}{255,255,255}
\definecolor{C3}{RGB}{154,0,79}
\pgfdeclareverticalshading{someShading}{50bp}{
color(0bp)=(C1);
color(25bp)=(C2);
color(50bp)=(C3)
}
\shade [shading=someShading](1,4) +(-.5,-3.5) rectangle ++(.5,3.5);
\end{tikzpicture}
\end{document}
我想避免为各个矩形创建数据来控制颜色图的范围。“最佳情况”是能够处理实际间隔,例如选择 [0,1] 之间的值或处理百分比间隔,如 [0%,100%]。
非常感谢你的帮助,
米
答案1
\documentclass[]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\definecolor{C1}{RGB}{19,128,67}
\definecolor{C2}{RGB}{255,255,255}
\definecolor{C3}{RGB}{154,0,79}
\pgfdeclarehorizontalshading{someShading}{50bp}{
color(0bp)=(C1);
color(25bp)=(C2);
color(50bp)=(C3)
}
\clip (0,0) rectangle ++(6,2.5);
\shade [shading=someShading](0,0) rectangle ++(6,0.5);
\shade [shading=someShading](0,0.51) rectangle ++(7,0.5);
\shade [shading=someShading](0,1.01) rectangle ++(8,0.5);
\shade [shading=someShading](0,1.51) rectangle ++(9,0.5);
\shade [shading=someShading](0,2.01) rectangle ++(10,0.5);
\end{tikzpicture}
\end{document}
同样的剪辑技术pgfplots
\documentclass[]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\pgfplotsset{ticks=none,
hide axis,
scale only axis,
height=0pt,
width=0pt,
colormap/jet,
colorbar horizontal}
\clip (0,-0.75) rectangle ++(9.9,2.45);
\begin{scope}[yshift=2.01cm]
\begin{axis}[
colorbar style={
width=15cm,
}]
\addplot [draw=none] coordinates {(0,0)};
\end{axis}
\end{scope}
\begin{scope}[yshift=1.51cm]
\begin{axis}[
colorbar style={
width=14cm,
}]
\addplot [draw=none] coordinates {(0,0)};
\end{axis}
\end{scope}
\begin{scope}[yshift=1.01cm]
\begin{axis}[
colorbar style={
width=13cm,
}]
\addplot [draw=none] coordinates {(0,0)};
\end{axis}
\end{scope}
\begin{scope}[yshift=0.51cm]
\begin{axis}[
colorbar style={
width=12cm,
}]
\addplot [draw=none] coordinates {(0,0)};
\end{axis}
\end{scope}
\begin{scope}
\begin{axis}[
colorbar style={
width=10cm,
}]
\addplot [draw=none] coordinates {(0,0)};
\end{axis}
\end{scope}
\end{tikzpicture}
\end{document}
答案2
您可以通过 将颜色图转换为 PGF 阴影\pgfplotscolormaptoshadingspec
。之后,您可以使用 tikz 技术剪切生成的阴影,使其仅显示可见区域:
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\begin{document}
\thispagestyle{empty}
% #1: left offset in [0,1]
% #2: width
\newcommand\colormapclipped[2]{%
% convert colormap -> \result
\pgfplotscolormaptoshadingspec{\pgfkeysvalueof{/pgfplots/colormap name}}{#2}\result
% define and use a shading in pgf:
\def\tempb{\pgfdeclarehorizontalshading{tempshading}{1cm}}%
% where `\result' is inserted as last argument:
\expandafter\tempb\expandafter{\result}%
\begin{tikzpicture}[
% compensate for the clip by scaling the image:
xscale={1/(1-#1)}
]
% clip away #1 percent of the width:
\clip ({(#1)*#2},0cm) rectangle (#2,1cm);
\pgftext[bottom,left]{\pgfuseshading{tempshading}}%
\end{tikzpicture}%
}%
\pgfplotsset{colormap/jet}%
\colormapclipped{0}{8cm}
\colormapclipped{0.25}{8cm}
\colormapclipped{0.5}{8cm}
\colormapclipped{0.75}{8cm}
\end{document}