按 x 坐标值过滤 PGFPlots

按 x 坐标值过滤 PGFPlots

我一直在尝试从 percusse 那里得到的答案中找到灵感这里,但我被 PGFPlots 手册淹没了。我想在给定 x 坐标值(而不是 x 坐标索引)的情况下绘制中间的平滑区域,但

\documentclass{standalone}
\usepackage{pgfplots}
\makeatletter
\pgfplotsset{
    reduce to/.style args={every#1except between values#2and#3}{%
        /pgfplots/x filter/.append code={%
            \def\myswitch{1}%
            \ifx\pgfkeysvalueof{/data point/x}>#2%
            %also tried pgfkeysgetvalue{/data point/x} which did not work 
                \ifx\pgfkeysvalueof{/data point/x}<#3%
                    \def\myswitch{0}%
                \fi%
            \fi%
            \ifnum1=\myswitch%
                \pgfmathsetmacro\temp{int(mod(\coordindex,#1))}%
                    \ifnum0<\temp
                        \let\pgfmathresult\pgfutil@empty
                    \fi%
            \fi%
        }
    } 
}
\makeatother

\begin{document}
\begin{tikzpicture}
\begin{axis}[reduce to=every 50 except between values 0 and 2]%
\addplot[samples=1501] {sin(deg(5*x))};
\end{axis}
\end{tikzpicture}
\end{document}

我得到了意想不到的结果

意外结果

...如果它不能编译,我几乎可以更好地理解。有人知道我应该改变什么来绘制 x 值 0 和 2 之间的每个点吗?

答案1

这些应该能起作用...终于

请注意,在 中x filter有两种方法可以获取当前的 x 坐标:通过#1\pgfmathresult。并且输出应存储在 中\pgfmathresult

\documentclass[border=9]{standalone}
\usepackage{pgfplots}
\makeatletter
\pgfplotsset{
    reduce to/.style args={every#1except between values#2and#3}{%
        /pgfplots/x filter/.code={%
            \let\pgfmathreserved\pgfmathresult
            \def\myswitch{1}%
            \pgfmathparse{##1>#2}%
            \ifpgfmathfloatcomparison
                \pgfmathparse{##1<#3}%
                \ifpgfmathfloatcomparison
                    \def\myswitch{0}%
                \fi%
            \fi%
            \let\pgfmathresult\pgfmathreserved
            \ifnum1=\myswitch%
                \pgfmathsetmacro\temp{int(mod(\coordindex,#1))}%
                \ifnum0<\temp
                    \let\pgfmathresult\pgfutil@empty
                \fi%
            \fi%
        }
    } 
}
\makeatother

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[reduce to=every 50 except between values 0 and 2]%
            \addplot[samples=1501] {sin(deg(5*x))};
        \end{axis}
    \end{tikzpicture}
\end{document}

相关内容