Graphics, Figures & Tables'Clipped' sine curve in PGFPlots

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

Post by europaflyer »

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 13415 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.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

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

Post by Stefan Kottwitz »

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

'Clipped' sine curve in PGFPlots

Post by europaflyer »

Here it is.

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}
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

Re: 'Clipped' sine curve in PGFPlots

Post by svend_tveskaeg »

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

Re: 'Clipped' sine curve in PGFPlots

Post by europaflyer »

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

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

'Clipped' sine curve in PGFPlots

Post by Stefan Kottwitz »

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

Code: Select all

\pgfplotsextra{%
   \clip (axis cs:-6.3,-0.8) rectangle (axis cs:6.3,0.8);}
Complete example:

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}
clipped-plot.png
clipped-plot.png (13.53 KiB) Viewed 13384 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 13383 times
Stefan
LaTeX.org admin
europaflyer
Posts: 6
Joined: Mon Dec 10, 2012 4:50 pm

Re: 'Clipped' sine curve in PGFPlots

Post by europaflyer »

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: 10335
Joined: Mon Mar 10, 2008 9:44 pm

'Clipped' sine curve in PGFPlots

Post by Stefan Kottwitz »

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.

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}
clipped-continuous-plot.png
clipped-continuous-plot.png (13.81 KiB) Viewed 13379 times
Stefan
LaTeX.org admin
feuersaenger
Posts: 34
Joined: Sun Oct 16, 2011 5:56 pm

'Clipped' sine curve in PGFPlots

Post by feuersaenger »

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*:

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}
P.png
P.png (20.9 KiB) Viewed 13373 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: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Re: 'Clipped' sine curve in PGFPlots

Post by Stefan Kottwitz »

Thanks Christian!

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

Stefan
LaTeX.org admin
Post Reply