pgfplots:使用 fillbetween 库

pgfplots:使用 fillbetween 库

我想使用fillbetween以下功能这里由 pgfplots 作者描述克里斯蒂安·费尔桑格在我的例子中,我有一个坐标列表,并且想要填充 -10 到 10 之间的区域。但是它不能正常工作:

\documentclass{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    width=150mm,
    xmin=-100,
    xmax=100,
    ymin=0,
    ymax=1.1,
    xtick={-100,-10,0,10,100},  
    grid,
    xlabel = xlabel,
    ylabel = ylabel,
    title = title,
]
\addplot[
    const plot,
    name path global = myFunction,
    ] coordinates {
(-71,0.001144165)
(-57,0.003432494)
(-54,0.004576659)
(-50,0.005720824)
(-46,0.006864989)
(-44,0.008009153)
(-43,0.010297483)
(-42,0.013729977)
(-40,0.014874142)
(-39,0.016018307)
(-38,0.018306636)
(-37,0.019450801)
(-34,0.02173913)
(-33,0.025171625)
(-32,0.026315789)
(-31,0.030892449)
(-30,0.035469108)
(-29,0.042334096)
(-28,0.050343249)
(-27,0.052631579)
(-26,0.057208238)
(-25,0.065217391)
(-24,0.073226545)
(-23,0.086956522)
(-22,0.09610984)
(-21,0.107551487)
(-20,0.113272311)
(-19,0.130434783)
(-18,0.143020595)
(-17,0.155606407)
(-16,0.177345538)
(-15,0.195652174)
(-14,0.207093822)
(-13,0.226544622)
(-12,0.255148741)
(-11,0.279176201)
(-10,0.302059497)
(-9,0.327231121)
(-8,0.352402746)
(-7,0.383295195)
(-6,0.424485126)
(-5,0.450800915)
(-4,0.487414188)
(-3,0.536613272)
(-2,0.586956522)
(-1,0.637299771)
(0,0.700228833)
(1,0.747139588)
(2,0.780320366)
(3,0.814645309)
(4,0.83180778)
(5,0.85583524)
(6,0.869565217)
(7,0.8798627)
(8,0.894736842)
(9,0.908466819)
(10,0.916475973)
(11,0.922196796)
(12,0.92791762)
(13,0.935926773)
(14,0.940503432)
(15,0.946224256)
(16,0.949656751)
(17,0.953089245)
(18,0.957665904)
(19,0.958810069)
(20,0.962242563)
(21,0.965675057)
(22,0.970251716)
(23,0.973684211)
(24,0.97597254)
(25,0.977116705)
(26,0.97826087)
(29,0.981693364)
(31,0.982837529)
(34,0.983981693)
(35,0.986270023)
(36,0.987414188)
(37,0.988558352)
(41,0.989702517)
(42,0.990846682)
(45,0.991990847)
(47,0.993135011)
(49,0.994279176)
(71,0.995423341)
(76,0.996567506)
(79,0.99771167)
(90,0.998855835)
(95,1)
};
\path[name path=myAxes] (axis cs:-10,0) -- (axis cs:10,0);
\addplot [
        thick,
        color=blue,
        fill=blue, 
        %fill opacity=0.05,
        draw=red,
        line width=2pt,
    ]
    fill between[
        of=myAxes and myFunction,
        soft clip={domain=-10:10},
    ];
\end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

我添加了一些评论来说明问题:

在此处输入图片描述

更新:也许有人知道如何将 居中x tick labels-10由于减号,它看起来没有居中。

答案1

无法填充该区域似乎是由于交叉点过多造成的:导致的垂直线const plot与 -- 相交不止一次soft clip,而软剪辑无法检测到它。这解释了虚假的红线和无法将其可视化的原因。

一种解决方案是将软剪辑区域稍微移离垂直线(或使用sharp plot而不是const plot)。我将其移动到-10.15:10.1。在这种情况下, 应该soft clip只应用于 函数 - 否则它不会与您的 x 轴片段相交。我使用soft clip second以便仅将其应用于 函数。

刻度标签的“居中”按照设计工作 - 居中应尊重刻度标签的整个宽度,包括任何减号。也就是说,我们仍然可以检查数字是否为负数并妨碍边界框。在最简单的情况下,负水平空间就可以完成这项工作。

两个建议均已实施如下:

\documentclass{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    width=150mm,
    xmin=-100,
    xmax=100,
    ymin=0,
    ymax=1.1,
    xtick={-100,-10,0,10,100},  
    xticklabel={%
        \pgfmathfloatparsenumber\tick
        \pgfmathfloatgetflagstomacro\pgfmathresult\pgfretval
        \if2\pgfretval
            % ah -- it is negative.
            \hskip -1.7ex %
        \fi
        \pgfmathprintnumber\pgfmathresult
    },%
    %xticklabel style={draw=black},
    grid,
    xlabel = xlabel,
    ylabel = ylabel,
    title = title,
]
\addplot[
    const plot,
    name path global = myFunction,
    ] coordinates {
(-71,0.001144165)
(-57,0.003432494)
(-54,0.004576659)
(-50,0.005720824)
(-46,0.006864989)
(-44,0.008009153)
(-43,0.010297483)
(-42,0.013729977)
(-40,0.014874142)
(-39,0.016018307)
(-38,0.018306636)
(-37,0.019450801)
(-34,0.02173913)
(-33,0.025171625)
(-32,0.026315789)
(-31,0.030892449)
(-30,0.035469108)
(-29,0.042334096)
(-28,0.050343249)
(-27,0.052631579)
(-26,0.057208238)
(-25,0.065217391)
(-24,0.073226545)
(-23,0.086956522)
(-22,0.09610984)
(-21,0.107551487)
(-20,0.113272311)
(-19,0.130434783)
(-18,0.143020595)
(-17,0.155606407)
(-16,0.177345538)
(-15,0.195652174)
(-14,0.207093822)
(-13,0.226544622)
(-12,0.255148741)
(-11,0.279176201)
(-10,0.302059497)
(-9,0.327231121)
(-8,0.352402746)
(-7,0.383295195)
(-6,0.424485126)
(-5,0.450800915)
(-4,0.487414188)
(-3,0.536613272)
(-2,0.586956522)
(-1,0.637299771)
(0,0.700228833)
(1,0.747139588)
(2,0.780320366)
(3,0.814645309)
(4,0.83180778)
(5,0.85583524)
(6,0.869565217)
(7,0.8798627)
(8,0.894736842)
(9,0.908466819)
(10,0.916475973)
(11,0.922196796)
(12,0.92791762)
(13,0.935926773)
(14,0.940503432)
(15,0.946224256)
(16,0.949656751)
(17,0.953089245)
(18,0.957665904)
(19,0.958810069)
(20,0.962242563)
(21,0.965675057)
(22,0.970251716)
(23,0.973684211)
(24,0.97597254)
(25,0.977116705)
(26,0.97826087)
(29,0.981693364)
(31,0.982837529)
(34,0.983981693)
(35,0.986270023)
(36,0.987414188)
(37,0.988558352)
(41,0.989702517)
(42,0.990846682)
(45,0.991990847)
(47,0.993135011)
(49,0.994279176)
(71,0.995423341)
(76,0.996567506)
(79,0.99771167)
(90,0.998855835)
(95,1)
};
\path[name path=myAxes] (axis cs:-10,0) -- (axis cs:10,0);
\addplot [
        thick,
        color=blue,
        fill=blue, 
        %fill opacity=0.05,
        draw=red,
        line width=2pt,
    ]
    fill between[
        of=myAxes and myFunction,
        soft clip second={domain=-10.15:10.1},
    ];
\end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容