TransWikia.com

Fill area under curve in the right order

TeX - LaTeX Asked by TimSqua on July 20, 2021

Good evening. I am building a plot for a data which was recorded over a year. For this frequency analysis I want to add thresholds to show at which time these have been passed. The area under these thresholds should be colored in red, orange and green but without to overlay the next area above. I thought to achieve this via the fill-command at the line of addplot command but this doesn’t have any effect.

documentclass[12pt,twoside]{report}
usepackage[utf8]{inputenc}
usepackage{etoolbox}
makeatletter
usepackage{graphicx}
usepackage{tikz}
usepackage{pgf-pie}
usepackage{pgfplots}
usepgfplotslibrary{dateplot}
usepgfplotslibrary{external}
usepackage{pdfpages}
pgfplotsset{compat=1.8}
pgfplotsset{compat=newest,compat/show suggested version=false}
usepackage{luatex85,shellesc}
usepackage{wrapfig}
usepackage{xcolor}
begin{document}
    begin{tikzpicture}
    begin{axis}[area style,
                width=16.5cm,
                height=7cm,
                date coordinates in=x,
                date ZERO=2013-01-01,
                xmin=2013-01-01,
                xmax=2013-12-29,
     
            ]
       addplot[color=black!70, thick] coordinates {
    (2013-01-01,10) %Data for the year
    (2013-01-02,13)
    ...
    (2013-12-28,14)
    (2013-12-29,18)
       };
    addplot[fill=green!30,color=green, thick] coordinates
            {(2013-01-01,10) (2013-12-29,10)}; %Threshold 1
    addplot[fill=orange!30,,thick] coordinates
            {(2013-01-01,15) (2013-12-29,15)}; %Threshold 2
    addplot[fill=red!60,color=red,thick] coordinates
            {(2013-01-01,20) (2013-12-29,20)}; %Threshold 3
      end{axis}
    end{tikzpicture}
end{document}

The current above result into the following outcome without any filled area under a curve:Current outcome

Instead, I want to achieve the following outcome: Target output

Is there a more promising approach to get the outcome as shown in the second picture by filling the area under the curve and in right order so that the colors are not overlaying?

2 Answers

Unfortunately your MWE doesn't work. Meanwhile I write the following MWE, which can serve you as starting point. Note, that for filling of area is used pgfplots library fillbetween:

documentclass[border=3.141592]{standalone}
usepackage{pgfplots}
usepgfplotslibrary{fillbetween,
                    dateplot}
usetikzlibrary{intersections}

begin{document}
    begin{tikzpicture}[
lbl/.style = {text=#1,font=hugebfseries,midway,below}
                       ]
begin{axis}[%area style,
    width=16.5cm,
    height=7cm,
%    date coordinates in=x,
%    date ZERO=2013-01-01,
%    xmin=2013-01-01,
%    xmax=2013-12-29,
        ]
addplot [color=red,   thick, name path=A]  
            coordinates { (0,1 ) (1,1 )} node[lbl=red] {Red};
addplot [color=orange,thick, name path=B]  
            coordinates { (0,.6) (1,.6)} node[lbl=orange] {Orange};
addplot [color=green, thick, name path=C] 
            coordinates { (0,.4) (1,.4)} node[lbl=green] {Green};
addplot [draw=none, name path=Axis]   coordinates { (0,0) (1,0)};
% fill
addplot [red!20]     fill between [of=A and Axis];
addplot [orange!20]  fill between [of=B and Axis];
addplot [green!20]   fill between [of=C and Axis];
% your function
% addplot <your function>
%
end{axis}
    end{tikzpicture}
end{document}

enter image description here

Addendum: With some simplifications of diagram code and consideration of the OP comment:

documentclass[border=3.141592]{standalone}
usepackage{pgfplots}
pgfplotsset{compat=1.17}
% abbreviations for use of axis minimal and maximal values
     newcommandXmin{pgfkeysvalueof{/pgfplots/xmin}}
     newcommandXmax{pgfkeysvalueof{/pgfplots/xmax}}
usepgfplotslibrary{fillbetween,
                    dateplot}

begin{document}
    begin{tikzpicture}[
lbl/.style = {font=hugebfseries,midway,below},
                      ]
 begin{axis}[%area style,
    width=16.5cm,
    height=7cm,
    axis on top,
    enlargelimits=false,
%    date coordinates in=x,
%    date ZERO=2013-01-01,
%    xmin=2013-01-01,
%    xmax=2013-12-29,
    xmin=0, xmax=10,
    domain=0:10, samples=101,
    no marks,
    every axis plot post/.append style={ultra thick, color=blue, semitransparent},
        ]
draw[red,      name path=A] (Xmin,2.0) -- node[lbl] {Red}     (Xmax,2.0);
draw [orange,  name path=B] (Xmin,1.4) -- node[lbl] {Orange}  (Xmax,1.4);
draw [green,   name path=C] (Xmin,0.8) -- node[lbl] {Green}   (Xmax,0.8);
path [name path=Axis] (Xmin,0) -- node[lbl] {Red} (Xmax,0);
% fill
%addplot [red!20]     fill between [of=A and Axis];
addplot [orange!20]  fill between [of=B and Axis];
addplot [green!20]   fill between [of=C and Axis];
% function (dummy example)
addplot    {1+rand};
end{axis}
    end{tikzpicture}
end{document}

enter image description here

Answered by Zarko on July 20, 2021

Another solution with filled rectangles.

documentclass{standalone}
usepackage{pgfplots}
usepgfplotslibrary{dateplot}
usepgfplotslibrary{external}
pgfplotsset{compat=newest,compat/show suggested version=false}
usepackage{xcolor}
begin{document}
    begin{tikzpicture}
    begin{axis}[
    width=16.5cm,
    height=7cm,
    date coordinates in=x,
    date ZERO=2013-01-01,
    xmin=2013-01-01,
    xmax=2013-12-29,
    no markers,
    solid
    ]
    fill [green!30, opacity=.5] (axis cs:2013-01-01, 0) rectangle (2013-12-29,10);
    fill [orange!30, opacity=.5] (axis cs:2013-01-01, 10) rectangle (2013-12-29,15);
    fill [red!30, opacity=.5] (axis cs:2013-01-01, 15) rectangle (2013-12-29,20);
    node at (2013-06-15, 5) [green] {Green};
    node at (2013-06-15, 12.5) [orange] {Orange};
    node at (2013-06-15, 17.5) [red] {Red};
%   addplot table [header=false, col sep=comma] {jobname.csv};
    addplot[color=black!70, thick] coordinates {
        (2013-01-01,10) %Data for the year
        (2013-01-02,13)
        (2013-01-03,1)
        (2013-12-28,14)
        (2013-12-29,18)
    };
    end{axis}
    end{tikzpicture}
end{document}

enter image description here

Answered by NBur on July 20, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP