你能在 Tikz 中创建具有可变粗细(线宽)的线条吗?
答案1
这是可能的,但并不容易,而且对线宽的控制也不是很精细,但这是一个想法,我认为可以得到更好的代码。下面的代码来自 Mark Wibrow 的一个想法,用于改变线条的颜色。我修改了代码以改变宽度,但如果你只想改变颜色,这个代码是完美的:
\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{decorations}
\begin{document}
\makeatletter
\pgfkeys{/pgf/decoration/.cd,
start color/.store in =\startcolor,
end color/.store in =\endcolor
}
\pgfdeclaredecoration{width and color change}{initial}{
\state{initial}[width=0pt, next state=line, persistent precomputation={%
\pgfmathdivide{50}{\pgfdecoratedpathlength}%
\let\increment=\pgfmathresult%
\def\x{0}%
}]{}
\state{line}[width=.5pt, persistent postcomputation={%
\pgfmathadd@{\x}{\increment}%
\let\x=\pgfmathresult%
}]{%
\pgfsetlinewidth{\x/40*0.075pt+\pgflinewidth}%
\pgfsetarrows{-}%
\pgfpathmoveto{\pgfpointorigin}%
\pgfpathlineto{\pgfqpoint{.75pt}{0pt}}%
\pgfsetstrokecolor{\endcolor!\x!\startcolor}%
\pgfusepath{stroke}%
}
\state{final}{%
\pgfsetlinewidth{\pgflinewidth}%
\pgfpathmoveto{\pgfpointorigin}%
\color{\endcolor!\x!\startcolor}%
\pgfusepath{stroke}%
}
}
\makeatother
\tikz\draw[ line width=.4pt, decoration={width and color change,
start color=yellow, end color=red}, decorate] (0cm,0cm) arc
(0:120:4cm) ;
\end{document}
要修改此代码,您需要进行调整\pgfsetlinewidth{\x/40*0.075pt+\pgflinewidth}
答案2
可以使用 foreach 语句增加线宽。以下是 3D 螺旋的示例:
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[width=7cm, height=7cm, xmin=-1.05,
xmax=1.05, axis lines=none, view={0}{25}]
\foreach \x in {0,0.5,...,12.0}
{\edef\temp{\noexpand\addplot3[blue, line width=1+\x/2 pt,
domain=\x:\x+0.5,samples y=0]
( { cos( deg(x) ) }, { sin( deg(x) ) }, { x } );
} \temp }
\draw[>=latex,->] (105,100,10) -- (105,100,180);
\node at (95,90,178) { $z$ };
\end{axis}
\end{tikzpicture}
\end{document}