2

I'm trying to generate a plot where I want to shade the region enclosed by the x-axis, the right-hand branch of the equilateral hyperbola x^2 − y^2 = 1 and the line y=x as x varies between 0 and 1. This would be the region bounded by the y=x line, the x-axis and the right branch of equilateral hyperbola (blue curve) in the example.

The example LaTeX code is listed below. When I use the "fill between" command made available in pgfplots, I do not get the desired result. For some reason the last command to fill "white" between the right branch of equilateral hyperbola (blue curve) and x-axis does not work as expected. What am I doing wrong?

Best regards, Ozan

\documentclass{article}
\usepackage{tikz,pgfplots}
\usepgfplotslibrary{fillbetween}
\begin{document}

\begin{center}
\begin{tikzpicture}
% Point on equilateral hyperbola
\pgfmathsetmacro\XCoord{cosh(1.2)}
\pgfmathsetmacro\YCoord{sinh(1.2)}
\begin{axis}[%
  % Set the same unit vectors
  x=1cm, y=1cm,   
  % Number of samples
  samples=150,
  % Ensure axis cross at origin
  axis lines=middle,
]
% The x-axis
\path[name path=axis] (axis cs:0,0) -- (axis cs:\XCoord,0);
% Equilateral hyperbola
\addplot [domain=-1.75:1.75,blue,very thick,%
  name path global = branchRight]({cosh(x)}, {sinh(x)});
\addplot [domain=-1.75:1.75,name path global = branchLeft]%
  ({-cosh(x)}, {sinh(x)});
% Line 
\addplot[gray,domain=0:\XCoord,name path global = LineToPoint]%
  {(\YCoord/\XCoord)*x};
% Filling between line and x-axis
\addplot[gray] fill between[of=axis and LineToPoint,%
  soft clip={domain=0:\XCoord}];
% Filling between right branch of equilateral hyperbola and x-axis
\addplot[white] fill between[of=axis and branchRight,%
   soft clip={domain=1:\XCoord}];
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}

The output of the above code is shown below: Output of LaTeX code for filling between curves

1
  • So in short you don't need that white strip in the shaded area. Do I get you right? BTW, Welcome to TeX.SX!.
    – AboAmmar
    Sep 20, 2015 at 14:00

1 Answer 1

2

enter image description here

\documentclass{standalone}
\usepackage{tikz,pgfplots}
\usepgfplotslibrary{fillbetween}
 \pgfplotsset{compat=1.3}
\begin{document}

\begin{tikzpicture}
% Point on equilateral hyperbola
\pgfmathsetmacro\XCoord{cosh(1.2)}
\pgfmathsetmacro\YCoord{sinh(1.2)}
\begin{axis}[%
  % Set the same unit vectors
  x=1cm, y=1cm,   
  % Number of samples
  samples=150,
  % Ensure axis cross at origin
  axis lines=middle,
]
% The x-axis
\path[name path=axis] (axis cs:0,0) -- (axis cs:\XCoord,0);

% Equilateral hyperbola
\addplot [domain=-1.75:1.75,blue,very thick,%
  name path global = branchRight]({cosh(x)}, {sinh(x)});
\addplot [domain=-1.75:1.75,name path global = branchLeft]({-cosh(x)}, {sinh(x)});

% Line 
\addplot[gray,domain=0:\XCoord,name path global = LineToPoint]{(\YCoord/\XCoord)*x};
\path[name path=yline] (axis cs:2,-5) -- (axis cs:2,5);


% Filling between line and x-axis
\addplot[gray]fill between[of=axis and LineToPoint,soft clip={domain=0:\XCoord}];

% Filling between right branch of equilateral hyperbola and x-axis
\addplot[white]fill between[of=branchRight and yline,soft clip={domain=1:2}];
\end{axis}
\end{tikzpicture}
\end{document}

Just created a vertical line with the name yline as this :

\path[name path=yline] (axis cs:2,-5) -- (axis cs:2,5);

and performed the fillbetween as this:

\addplot[white]fill between[of=branchRight and yline,soft clip={domain=1:2}];
1
  • @OzanÖktem You are most welcome. Remember that you can accept the answer if you feel it solved the problem by clicking that green arrow on the left. This removes the question from the unanswered list.
    – AboAmmar
    Sep 21, 2015 at 19:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .