LaTeX forum ⇒ Graphics, Figures & Tables'Clipped' sine curve in PGFPlots Topic is solved

Information and discussion about graphics, figures & tables in LaTeX documents.
europaflyer
Posts: 6
Joined: Mon Dec 10, 2012 4:50 pm

'Clipped' sine curve in PGFPlots

Postby europaflyer » Sat Mar 16, 2013 3:57 pm

Hi,

I'm trying to produce a sine curve in pgfplots where the y-values do not exceed a certain number like this:

sinus-wave-signal.jpg
sinus-wave-signal.jpg (51.58 KiB) Viewed 9412 times


Any ideas, or is this impossible?

Thanks!
Last edited by Stefan Kottwitz on Sat Mar 16, 2013 5:23 pm, edited 1 time in total.
Reason: external image uploaded to forum

User avatar
Stefan Kottwitz
Site Admin
Posts: 9617
Joined: Mon Mar 10, 2008 9:44 pm

Postby Stefan Kottwitz » Sat Mar 16, 2013 5:21 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 Infominimal working example?

Stefan
LaTeX.org admin

europaflyer
Posts: 6
Joined: Mon Dec 10, 2012 4:50 pm

Postby europaflyer » Sat Mar 16, 2013 6:35 pm

Here it is.

  1. \documentclass{report}
  2. \usepackage{pgfplots}
  3. \begin{document}
  4. \begin{tikzpicture}
  5. \begin{axis}[
  6. domain=-6.283:6.283,
  7. grid=both,minor tick num=2,
  8. title={\underline{Title goes here}},
  9. xlabel=Bacon,
  10. ylabel=Eggs,
  11. legend pos=outer north east]
  12. \addplot+[no marks, samples=100] {sin(deg(x))};
  13. \legend{Test}
  14. \end{axis}
  15. \end{tikzpicture}
  16. \end{document}
Last edited by europaflyer on Sun Mar 17, 2013 1:15 pm, edited 2 times in total.

User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Postby svend_tveskaeg » Sun Mar 17, 2013 6:27 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)

europaflyer
Posts: 6
Joined: Mon Dec 10, 2012 4:50 pm

Postby europaflyer » Sun Mar 17, 2013 1:16 pm

Sorry! I've edited it - is that OK now?

Thanks

User avatar
Stefan Kottwitz
Site Admin
Posts: 9617
Joined: Mon Mar 10, 2008 9:44 pm

Postby Stefan Kottwitz » Sun Mar 17, 2013 1:54 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 \clip such as by

  1. \pgfplotsextra{%
  2. \clip (axis cs:-6.3,-0.8) rectangle (axis cs:6.3,0.8);}


Complete example:

  1. \documentclass{report}
  2. \usepackage{pgfplots}
  3. \begin{document}
  4. \begin{tikzpicture}
  5. \begin{axis}[
  6. domain=-6.283:6.283,
  7. grid=both,minor tick num=2,
  8. title={\underline{Title goes here}},
  9. xlabel=Bacon,
  10. ylabel=Eggs,
  11. legend pos=outer north east]
  12. \pgfplotsextra{%
  13. \clip (axis cs:-6.3,-0.8) rectangle (axis cs:6.3,0.8);}
  14. \addplot+[no marks, samples=100] {sin(deg(x))};
  15. \legend{Test}
  16. \end{axis}
  17. \end{tikzpicture}
  18. \end{document}


clipped-plot.png
clipped-plot.png (13.53 KiB) Viewed 9381 times


For better viewing when adjusting the clipping area, you could simply replace \clip by \draw[fill,opacity=0.2]:

opacity-plot.png
opacity-plot.png (14.89 KiB) Viewed 9380 times


Stefan
LaTeX.org admin

europaflyer
Posts: 6
Joined: Mon Dec 10, 2012 4:50 pm

Postby europaflyer » Sun Mar 17, 2013 2:02 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!)?

User avatar
Stefan Kottwitz
Site Admin
Posts: 9617
Joined: Mon Mar 10, 2008 9:44 pm

Postby Stefan Kottwitz » Sun Mar 17, 2013 2:38 pm

You can draw the lines yourself doing some math, so you get it exact. The key is to use 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.

  1. \documentclass{report}
  2. \usepackage{pgfplots}
  3. \begin{document}
  4. \begin{tikzpicture}
  5. \begin{axis}[
  6. domain=-6.283:6.283,
  7. grid=both,minor tick num=2,
  8. title={\underline{Title goes here}},
  9. xlabel=Bacon,
  10. ylabel=Eggs,
  11. legend pos=outer north east]
  12. \pgfplotsextra{%
  13. \clip (axis cs:-6.3,-0.8) rectangle (axis cs:6.3,0.8);
  14. \pgfmathparse{rad(asin(0.8))}\let\xa\pgfmathresult
  15. \pgfmathparse{pi-\xa}\let\xb\pgfmathresult
  16. \draw[thick] (axis cs:\xa,0.8) -- (axis cs:\xb,0.8);
  17. \pgfmathparse{\xa-2*pi}\let\xa\pgfmathresult
  18. \pgfmathparse{\xb-2*pi}\let\xb\pgfmathresult
  19. \draw[thick] (axis cs:\xa,0.8) -- (axis cs:\xb,0.8);
  20. \pgfmathparse{\xa+pi}\let\xa\pgfmathresult
  21. \pgfmathparse{\xb+pi}\let\xb\pgfmathresult
  22. \draw[thick] (axis cs:\xa,-0.8) -- (axis cs:\xb,-0.8);
  23. \pgfmathparse{\xa+2*pi}\let\xa\pgfmathresult
  24. \pgfmathparse{\xb+2*pi}\let\xb\pgfmathresult
  25. \draw[thick] (axis cs:\xa,-0.8) -- (axis cs:\xb,-0.8);
  26. }
  27. \addplot+[no marks, samples=100] {sin(deg(x))};
  28. \legend{Test}
  29. \end{axis}
  30. \end{tikzpicture}
  31. \end{document}


clipped-continuous-plot.png
clipped-continuous-plot.png (13.81 KiB) Viewed 9376 times


Stefan
LaTeX.org admin

feuersaenger
Posts: 34
Joined: Sun Oct 16, 2011 5:56 pm

Postby feuersaenger » Sun Mar 17, 2013 6:21 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 restrict y to domain*:

  1. \documentclass{report}
  2. \usepackage{pgfplots}
  3. \begin{document}
  4. \thispagestyle{empty}
  5. \begin{tikzpicture}
  6. \begin{axis}[
  7. domain=-6.283:6.283,
  8. grid=both,minor tick num=2,
  9. title={\underline{Title goes here}},
  10. xlabel=Bacon,
  11. ylabel=Eggs,
  12. restrict y to domain*=-0.8:0.8,
  13. legend pos=outer north east]
  14. \addplot+[no marks, samples=100] {sin(deg(x))};
  15. \legend{Test}
  16. \end{axis}
  17. \end{tikzpicture}
  18. \end{document}


P.png
P.png (20.9 KiB) Viewed 9370 times


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

User avatar
Stefan Kottwitz
Site Admin
Posts: 9617
Joined: Mon Mar 10, 2008 9:44 pm

Postby Stefan Kottwitz » Sun Mar 17, 2013 6:31 pm

Thanks Christian!

I hoped I bridged the time well until you arrived. :D

Stefan
LaTeX.org admin


Return to “Graphics, Figures & Tables”

Who is online

Users browsing this forum: No registered users and 7 guests