Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want to use the fillbetween feature which is here decsribed by the pgfplots author Christian Feuersänger. In my case I have a coordinate list and want to fill the area between -10 and 10. But it doesn't work properly:

\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}

enter image description here

I added some comments to illustrate the problems:

enter image description here

Update: And maybe someone knows how to center the x tick labels. The -10 looks not centered because of the minus sign.

share|improve this question

1 Answer 1

up vote 7 down vote accepted

The failure to fill the area is caused by too many intersections as it seems: the vertical lines caused by const plot result in more than one intersection with the soft clip -- and the soft clip does not detect it. This explains the spurious red line and the failure to visualize it.

One solution is to move the soft clip region slightly away from the vertical line (or to use sharp plot instead of const plot). I moved it to -10.15:10.1. In this case, the soft clip should be applied to the function only -- otherwise it will not intersect your x axis fragment. I used soft clip second in order to apply it only to the function.

The "centering" of the tick labels works as designed - centering should respect the entire width of the tick label, including any minus signs. That said, we can still check if the number is negative and hamper with the bounding box. In the simplest case, a negative horizontal space will do the job.

Both suggestions are implemented below:

\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}

enter image description here

share|improve this answer
    
Wow. Thanks. Allow me a last question: Why is soft clip second needed? I don't get it. –  Manuel Kuehner Aug 10 at 16:14
2  
Excellent question. I was about to explain my thoughts that myAxis has no intersection points with the clip path (correct)... but it should work nevertheless! That seems to be a bug, doesn't it!? I didn't realize it until you asked for an explanation. thanks... –  Christian Feuersänger Aug 10 at 16:28
    
Cool. Finally a good question. Right - no intersection. It's asymptotic. –  Manuel Kuehner Aug 10 at 16:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.