Graphics, Figures & Tables ⇒ 'Clipped' sine curve in PGFPlots
-
- Posts: 6
- Joined: Mon Dec 10, 2012 4:50 pm
'Clipped' sine curve in PGFPlots
I'm trying to produce a sine curve in pgfplots where the y-values do not exceed a certain number like this:
Any ideas, or is this impossible?
Thanks!
NEW: TikZ book now 40% off at Amazon.com for a short time.
And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p
- Stefan Kottwitz
- Site Admin
- Posts: 10335
- Joined: Mon Mar 10, 2008 9:44 pm
'Clipped' sine curve in PGFPlots
welcome to the board!
I understand, that your question is specifically about the clipping. Could you post the code for the curve which you already have, i.e. a

Stefan
-
- Posts: 6
- Joined: Mon Dec 10, 2012 4:50 pm
'Clipped' sine curve in PGFPlots
Code: Select all
\documentclass{report}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
domain=-6.283:6.283,
grid=both,minor tick num=2,
title={\underline{Title goes here}},
xlabel=Bacon,
ylabel=Eggs,
legend pos=outer north east]
\addplot+[no marks, samples=100] {sin(deg(x))};
\legend{Test}
\end{axis}
\end{tikzpicture}
\end{document}
- svend_tveskaeg
- Posts: 478
- Joined: Sun Jul 12, 2009 5:31 am
Re: 'Clipped' sine curve in PGFPlots
-- Zapp Brannigan, Futurama (season 1, episode 4)
-
- Posts: 6
- Joined: Mon Dec 10, 2012 4:50 pm
Re: 'Clipped' sine curve in PGFPlots
Thanks
- Stefan Kottwitz
- Site Admin
- Posts: 10335
- Joined: Mon Mar 10, 2008 9:44 pm
'Clipped' sine curve in PGFPlots
\clip
such as byCode: Select all
\pgfplotsextra{%
\clip (axis cs:-6.3,-0.8) rectangle (axis cs:6.3,0.8);}
Code: Select all
\documentclass{report}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
domain=-6.283:6.283,
grid=both,minor tick num=2,
title={\underline{Title goes here}},
xlabel=Bacon,
ylabel=Eggs,
legend pos=outer north east]
\pgfplotsextra{%
\clip (axis cs:-6.3,-0.8) rectangle (axis cs:6.3,0.8);}
\addplot+[no marks, samples=100] {sin(deg(x))};
\legend{Test}
\end{axis}
\end{tikzpicture}
\end{document}
\clip
by \draw[fill,opacity=0.2]
:Stefan
-
- Posts: 6
- Joined: Mon Dec 10, 2012 4:50 pm
Re: 'Clipped' sine curve in PGFPlots
Would there be any way to connect the ends by a straight line as in the example, or is the best way to manually enter four new lines (not a very elegant solution!)?
- Stefan Kottwitz
- Site Admin
- Posts: 10335
- Joined: Mon Mar 10, 2008 9:44 pm
'Clipped' sine curve in PGFPlots
asin
(arcsin) and rad
to reverse the y value for getting one x value, and using the periodicity of the sinus for all the other x values.Code: Select all
\documentclass{report}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
domain=-6.283:6.283,
grid=both,minor tick num=2,
title={\underline{Title goes here}},
xlabel=Bacon,
ylabel=Eggs,
legend pos=outer north east]
\pgfplotsextra{%
\clip (axis cs:-6.3,-0.8) rectangle (axis cs:6.3,0.8);
\pgfmathparse{rad(asin(0.8))}\let\xa\pgfmathresult
\pgfmathparse{pi-\xa}\let\xb\pgfmathresult
\draw[thick] (axis cs:\xa,0.8) -- (axis cs:\xb,0.8);
\pgfmathparse{\xa-2*pi}\let\xa\pgfmathresult
\pgfmathparse{\xb-2*pi}\let\xb\pgfmathresult
\draw[thick] (axis cs:\xa,0.8) -- (axis cs:\xb,0.8);
\pgfmathparse{\xa+pi}\let\xa\pgfmathresult
\pgfmathparse{\xb+pi}\let\xb\pgfmathresult
\draw[thick] (axis cs:\xa,-0.8) -- (axis cs:\xb,-0.8);
\pgfmathparse{\xa+2*pi}\let\xa\pgfmathresult
\pgfmathparse{\xb+2*pi}\let\xb\pgfmathresult
\draw[thick] (axis cs:\xa,-0.8) -- (axis cs:\xb,-0.8);
}
\addplot+[no marks, samples=100] {sin(deg(x))};
\legend{Test}
\end{axis}
\end{tikzpicture}
\end{document}
-
- Posts: 34
- Joined: Sun Oct 16, 2011 5:56 pm
'Clipped' sine curve in PGFPlots
Pgfplots has two different types of clipping: the first is the graphical clipping operation which is activated by \clip, the second is based on coordinate value manipulation, more precisely by
restrict y to domain*
:Code: Select all
\documentclass{report}
\usepackage{pgfplots}
\begin{document}
\thispagestyle{empty}
\begin{tikzpicture}
\begin{axis}[
domain=-6.283:6.283,
grid=both,minor tick num=2,
title={\underline{Title goes here}},
xlabel=Bacon,
ylabel=Eggs,
restrict y to domain*=-0.8:0.8,
legend pos=outer north east]
\addplot+[no marks, samples=100] {sin(deg(x))};
\legend{Test}
\end{axis}
\end{tikzpicture}
\end{document}
restrict y to domain
means that *if* some sample exceeds the limit, it will be replaced by the limit. Without the asterisk, exceeding coordinate values will be discarded.Kind regards
Christian
- Stefan Kottwitz
- Site Admin
- Posts: 10335
- Joined: Mon Mar 10, 2008 9:44 pm
Re: 'Clipped' sine curve in PGFPlots
I hoped I bridged the time well until you arrived.

Stefan