这是由以下极其丑陋的代码生成的:
\documentclass[tikz]{standalone}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{hobby}
\definecolor{col1}{RGB}{127,127,127}
\definecolor{col2}{RGB}{240,240,240}
\begin{document}
\begin{tikzpicture}[use Hobby shortcut]
\clip (0,0) rectangle (1,1);
\path[yshift=10,
postaction={
decorate,
decoration={
markings,
mark=between positions 0 and \pgfdecoratedpathlength step .01cm with {
\pgfmathsetmacro\myval{multiply(divide(
\pgfkeysvalueof{/pgf/decoration/mark info/distance from start}, \pgfdecoratedpathlength),100)};
\pgfsetfillcolor{col2!\myval!col1};
\pgfpathcircle{\pgfpointorigin}{1.5};
\pgfusepath{fill};}
}}]
(.075,.2)..([closed].2,.4)..(.8,0)..(.925,.2)..(.8,.4)..(.2,0);
\fill[col1] (.075,.55) circle (0.052cm);
\end{tikzpicture}
\end{document}
该代码无法编译,因为会出现许多类似这样的错误:
Dimension too large. (...2,.4)..(.8,0)..(.925,.2)..(.8,.4)..(.2,0); ...)
但是我能够获得结果 pdf,从而得到上面的图片。
我的问题是:如何正确绘制这幅图?提前感谢任何即将到来的建议〜
答案1
您pgfplots
可以使用它point meta
来改变图表的颜色。
\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\begin{document}
\begin{tikzpicture}
\begin{axis}[hide axis,width=8cm,height=4cm,clip=false]
\addplot[domain=0:360,samples=720,line cap=round,
colormap={}{
color(0cm)=(gray!10);
color(16cm)=(gray);
},
line width=5mm, variable=t,point meta=t,mesh]
({-cos(t)},{-sin(2*t)});
\end{axis}
\end{tikzpicture}
\end{document}
为了获得适当的边界框,您可以将线宽存储在宏中。
\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\begin{document}
\begin{tikzpicture}
\begin{axis}[hide axis,width=8cm,height=4cm,clip=false]
\def\mylw{5mm} %<- line width
\addplot[domain=0:360,samples=720,line cap=round,
colormap={}{
color(0cm)=(gray!10);
color(16cm)=(gray);
},
line width=\mylw, variable=t,point meta=t,mesh]
({-cos(t)},{-sin(2*t)});
% add points to have an appropriat bounding box
\path ([xshift=-\mylw/2,yshift=-\mylw/2]-1,-1)
([xshift=\mylw/2,yshift=\mylw/2]1,1);
\end{axis}
\end{tikzpicture}
\end{document}