1

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?

New contributor
TimSqua is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • Which TikZ libraries are you using? I can't run your code… – NBur 2 days ago
  • My plot-related preamble only consists of \usepackage{tikz} \usepackage{pgf-pie} \usepackage{pgfplots} \usepgfplotslibrary{dateplot} \usepgfplotslibrary{external} – TimSqua 2 days ago
  • Please, extend your code snippet to complete small document (MWE: Minimal Working Example), which reproduce showed diagram. So far your code fragment is not compilable. – Zarko 2 days ago
  • I extended the snippet above to an mwe. – TimSqua yesterday
3

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=\huge\bfseries,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
     \newcommand\Xmin{\pgfkeysvalueof{/pgfplots/xmin}}
     \newcommand\Xmax{\pgfkeysvalueof{/pgfplots/xmax}}
\usepgfplotslibrary{fillbetween,
                    dateplot}

\begin{document}
    \begin{tikzpicture}[
lbl/.style = {font=\huge\bfseries,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

  • Thank you, this is great. Is there a possibility to avoid the blank space under the green area? – TimSqua yesterday
  • @TimSqua, yes, it is: just use enlargelimits=false,. See addendum in my answer. – Zarko yesterday
  • Thanks, the first example works almost perfectly for me. Just one more little question: When I apply the first fill in-solution to my data, the filled area is overlapping the y axis on both sides so that the axis is not visible anymore. When I use the second and second last datapoint as coordinates there remains one small blank space. – TimSqua yesterday
  • @TimSqua, please compare axis options in the first and second solution. Between others the second differ with having axis on top,. Anyway, I suggest you to use second solution or combination axis preamble from second solution with code from the first solution and see, if the result will be what you after. – Zarko yesterday
0

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

Your Answer

TimSqua is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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