有谁知道如何通过 Lipschitz 连续函数优雅地改善以下维基百科(wikimedia)图像质量,使其在 Tikz 或 Pgfplots 中变为矢量?
可见如下:
感谢您的贡献!
答案1
因为完全模仿图像中显示的函数非常具有挑战性,所以我创建了一些其他的东西来帮助你实现同样的目标——这里有一个小脚本,它可以从随机数生成器中创建任意的 Lipschitz 连续函数。
您可以通过调整以下三个参数来定制输出内容:
\numSamples
- 用于创建函数的随机样本数量。样本越多,函数看起来就越参差不齐。\K
- 函数的 Lipschitz 常数\pgfmathsetseed
- 设置 Ti 的种子钾z 的随机数生成器
示例输出
代码
\documentclass[12pt]{article}
\usepackage[svgnames]{xcolor}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{math}
\begin{document}
\def\numSamples{50}
\def\K{1}
\pgfmathsetseed{123456789}
\def\lipschitzFunc{(0,0)}
\tikzmath{
\funcValue = 0; % the graph begins at the origin
for \n in {1,...,\numSamples} {
if \n <= \numSamples / 2 then { % first, generate the right side of the graph
\deltaX = 4 / int(\numSamples / 2);
\funcValue = \funcValue + rand * \K * \deltaX;
\xVal = \n * \deltaX;
{\global\edef\lipschitzFunc{\lipschitzFunc(\xVal,\funcValue)}};
}
else { % then, come back and generate the left side of the graph
if \n == 1 + int(\numSamples / 2) then {
\funcValue = 0;
};
\deltaX = 4 / int((\numSamples + 1)/ 2);
\funcValue = \funcValue + rand * \K * \deltaX;
\xVal = (int(\numSamples / 2) - \n) * \deltaX;
{\global\edef\lipschitzFunc{(\xVal,\funcValue)\lipschitzFunc}};
};
};
}
\begin{tikzpicture}
\begin{axis}[
xmin = -4,
xmax = 4,
ymin = -4,
ymax = 4,
xtick = \empty,
ytick = \empty,
axis on top,
%axis line style={draw=none} % uncomment this line to remove surrounding box
]
\addplot[smooth] coordinates {\lipschitzFunc};
\addplot[name path = downup, color = LimeGreen, very thick]{ \K * x};
\addplot[name path = updown, color = LimeGreen, very thick]{-\K * x};
\addplot[fill = PaleGreen]
fill between
[
of = updown and downup,
soft clip = {domain = 0:4}
];
\addplot[fill = PaleGreen]
fill between
[
of = downup and updown,
soft clip = {domain = -4:0}
];
\end{axis}
\end{tikzpicture}
\end{document}