我正在尝试获取多色平移曲线,我根据给出的解决方案编写了代码这里但似乎我错过了什么
% arara: indent: {overwrite: yes}
% arara: pdflatex: { shell : yes }
\documentclass[border=3mm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\pgfplotsset{compat=1.12}
\begin{document}
\begin{tikzpicture}[scale=1.1,>=stealth]
\colorlet{redhsb}[hsb]{red}%
\colorlet{bluehsb}[hsb]{blue}%
\begin{axis}[
width=10cm,
height=10cm,
scale=12/9.5,
disabledatascaling,
axis lines =center,
xmin= -6, xmax= 6,
ymin=-6, ymax=6,
color=cyan,
samples=100
]
% Curves
\foreach \t in {0,.2,...,5}{
\pgfmathtruncatemacro{\rat}{\t*10}
\colorlet{col}{bluehsb!\rat!redhsb}
\addplot[thin,col,smooth, domain=-2.0:2.0] {x*x*x-2*x-3+\t};
}
\end{axis}
\end{tikzpicture}
\end{document}
答案1
一年前我尝试过实现类似的目标并最终得到以下结果:
\documentclass[border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\usepackage{xparse}
% Calculate tint for values between [#1, #3] and store result in macro \tint
% Syntax: \calctint[#1]{#2}{#3}[#4][#5]
% #1: lower bound (default 1)
% #2: value at which tint should be computed
% #3: upper bound
% #4: lower tint value (default 0)
% #5: upper tint value (default 100)
\NewDocumentCommand\calctint{O{1}mmO{0}O{100}}
{\pgfmathsetmacro{\tint}{#5-(#4-#5)*(#1-#2)/(#3-#1)}}
\begin{document}
\begin{tikzpicture}
\colorlet{redhsb}[hsb]{red}%
\colorlet{bluehsb}[hsb]{blue}%
\begin{axis}
\foreach \t in {0,0.2,...,5}{
\calctint[0]{\t}{5}
\edef\tmp{\noexpand
\addplot[bluehsb!\tint!redhsb, smooth, domain=-2.0:2.0] {x*x*x-2*x-3+\t};
}\tmp
}
\end{axis}
\end{tikzpicture}
\end{document}
答案2
据我了解pgfplots
,您需要cycle list
在“addplot”之前定义如下内容:
\documentclass[border=3mm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}[scale=1.1,>=stealth]
\begin{axis}[cycle list={
red!00!blue,red!05!blue,red!10!blue,red!15!blue,red!20!blue,
red!25!blue,red!30!blue,red!45!blue,red!50!blue,red!55!blue,
red!60!blue,red!65!blue,red!70!blue,red!75!blue,red!80!blue,
red!85!blue,red!90!blue,red!95!blue,red!100!blue,
},
width=10cm,
height=10cm,
axis lines=center,
xmin= -6, xmax= 6,
ymin=-6, ymax=6,
color=cyan,
domain=-2.0:2.0, samples=50, smooth,
]
% Curves
\foreach \i in {0,0.2,...,5}
\addplot {x*x*x-2*x-3+\i};
\end{axis}
\end{tikzpicture}
\end{document}
在这种情况下,addplot
循环采用曲线颜色。如果您定义的可能颜色比曲线少,则颜色会重复。使用上述代码,我得到了以下图表:
我稍微简化了代码,但这对你的情况来说并不重要。我很乐意询问是否存在一种方法,用于在某个循环中填充列表或在循环中定义颜色,正如你(未成功)尝试实现的那样。