Hi,
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!
LaTeX forum ⇒ Graphics, Figures & Tables ⇒ 'Clipped' sine curve in PGFPlots Topic is solved
-
- Posts: 6
- Joined: Mon Dec 10, 2012 4:50 pm
'Clipped' sine curve in PGFPlots
Last edited by Stefan Kottwitz on Sat Mar 16, 2013 5:23 pm, edited 1 time in total.
Reason: external image uploaded to forum
Reason: external image uploaded to forum
- Stefan Kottwitz
- Site Admin
- Posts: 9617
- Joined: Mon Mar 10, 2008 9:44 pm
Hi,
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
minimal working example?
Stefan
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
LaTeX.org admin
-
- Posts: 6
- Joined: Mon Dec 10, 2012 4:50 pm
Here it is.
\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}
Last edited by europaflyer on Sun Mar 17, 2013 1:15 pm, edited 2 times in total.
- svend_tveskaeg
- Posts: 478
- Joined: Sun Jul 12, 2009 5:31 am
Read the page about a minimal working example, please. You have not produced such a thing.
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
-- Zapp Brannigan, Futurama (season 1, episode 4)
-
- Posts: 6
- Joined: Mon Dec 10, 2012 4:50 pm
Sorry! I've edited it - is that OK now?
Thanks
Thanks
- Stefan Kottwitz
- Site Admin
- Posts: 9617
- Joined: Mon Mar 10, 2008 9:44 pm
Nice that you completed your example. Here just a few lines were missing, in other cases it might be less obvious how to get it compilable. I gladly test a complete example, so here's a possible solution - add a clipping path by
Complete example:
For better viewing when adjusting the clipping area, you could simply replace
Stefan
\clip
such as by
\pgfplotsextra{% \clip (axis cs:-6.3,-0.8) rectangle (axis cs:6.3,0.8);}
Complete example:
\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}
For better viewing when adjusting the clipping area, you could simply replace
\clip
by \draw[fill,opacity=0.2]
:Stefan
LaTeX.org admin
-
- Posts: 6
- Joined: Mon Dec 10, 2012 4:50 pm
That's really nice work, big thanks Stefan.
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!)?
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: 9617
- Joined: Mon Mar 10, 2008 9:44 pm
You can draw the lines yourself doing some math, so you get it exact. The key is to use
Stefan
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.
\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}
Stefan
LaTeX.org admin
-
- Posts: 34
- Joined: Sun Oct 16, 2011 5:56 pm
Hi,
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
The Asterisk after
Kind regards
Christian
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*
:
\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}
The Asterisk after
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: 9617
- Joined: Mon Mar 10, 2008 9:44 pm
Thanks Christian!
I hoped I bridged the time well until you arrived.
Stefan
I hoped I bridged the time well until you arrived.

Stefan
LaTeX.org admin
Return to “Graphics, Figures & Tables”
Who is online
Users browsing this forum: No registered users and 7 guests