8

I'm trying to reproduce something like this with the function x^-1, but no matter what options I try, I can't seem to make the graph look correct.

This is my current code:

\documentclass[border=5mm]{standalone}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{pgfplots}

\pgfplotsset{compat=1.9}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    xtick={0,1,3,5,7,9,11,13,15,17},ytick={0,...,1.5},
    xmax=18,ymax=1.2,ymin=0,xmin=0,
    enlargelimits=true,
    axis lines=middle,
    clip=false,
    domain=0:17,
    axis on top
    ]

\addplot [draw=red, fill=red!10, ybar interval, samples=9, domain=1:17]
    {x^-1}\closedcycle;
\addplot [draw=green, fill=green!10, const plot mark right, samples=9, domain=1:17]
    {x^-1}\closedcycle;

\addplot[smooth, thick,domain=1:17,samples=40]{x^-1};

\end{axis}
\end{tikzpicture}
\end{document}

And this is the current output:

LaTeX output

How do I make the graph look correct, with the green outline for the green bars and without the green outline on the left of the first red bar (see output above)?

  • Probably not the solution you are looking for, but you could use foreach \i in {1,3,...,17} and draw a green line from \i to 1/\i. Not very elegant because you superimpose lines... but better than nothing :D – anderstood Aug 26 '14 at 2:33
  • For a PSTricks solution, see page 41--43 in the pstricks-add manual. – Svend Tveskæg Aug 26 '14 at 5:24
11

You can fix this problem by using the ybar interval style for both plots, but reversing the direction for the lower sum by using domain=17:1 instead of domain=1:17:

\documentclass[border=5mm]{standalone}

\usepackage{pgfplots}

\pgfplotsset{compat=1.9}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    xtick={0,1,3,5,7,9,11,13,15,17},ytick={0,...,1.5},
    xmax=18,ymax=1.2,ymin=0,xmin=0,
    enlargelimits=true,
    axis lines=middle,
    clip=false,
    domain=0:17,
    axis on top
    ]

\addplot [draw=red, fill=red!10, ybar interval, samples=9, domain=1:17]
    {x^-1}\closedcycle;
\addplot [draw=green, fill=green!10, ybar interval, samples=9, domain=17:1]
    {x^-1}\closedcycle;

\addplot[smooth, thick,domain=1:17,samples=40]{x^-1};

\end{axis}
\end{tikzpicture}
\end{document}
  • That's a neat trick but it doesn't seem to work if \addplot specifies gnuplot. More generally, is there a way to get the box heights correspond to highest and lowest function values in each interval? – Dominique Nov 7 '14 at 22:25
0

You can put the lower rectangles in a scope environment and also clip to the correct height. Try replacing

\addplot [draw=green, fill=green!10, const plot mark right, samples=9, domain=1:17]
{x^-1}\closedcycle;

with

\begin{scope}
\clip (0,0) rectangle (6.5cm,1.6cm);
\addplot [draw=green, fill=green!10, const plot mark right, samples=9, domain=1:17]
{x^-1}\closedcycle;
\end{scope}

I found the values 6.5cm and 1.6cm by trial and error. Don't know how to calculate these values automatically. Also, my version of pgfplots is 1.5, so the values may need adjusting.

Your Answer

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.