Graphics, Figures & TablesCombine pgfplots' axis environment and tikz graphics

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
tush
Posts: 11
Joined: Fri Feb 22, 2019 2:54 pm

Combine pgfplots' axis environment and tikz graphics

Post by tush »

I want to reproduce the following plot:
graph.png
graph.png (42.4 KiB) Viewed 4989 times
It is made of the following points (red in image):
\fill[red] (0,{sin(2*pi*1.2*0 r)}) circle (2pt);
\fill[red] (1,{sin(2*pi*1.2*1 r)}) circle (2pt);
\fill[red] (2,{sin(2*pi*1.2*2 r)}) circle (2pt);
% and so on up until the 21st point:
\fill[red] (20,{sin(2*pi*1.2*20 r)}) circle (2pt);

Now I want to add the curve
sine eqt.png
sine eqt.png (1.57 KiB) Viewed 4989 times
So as I learned from googling it up, the way to that is use pgfplots:
\begin{axis}
[clip=false,xmin=0,xmax=20]
\addplot[samples=500,domain=0:20]{sin(2*pi*1.2*deg(x))};
\end{axis}


But the output I get is this:
Screen Shot 2021-04-10 at 16.36.40.png
Screen Shot 2021-04-10 at 16.36.40.png (121.81 KiB) Viewed 4989 times
What I understand is that pfgplots axis environment and tikz graphics are not sitting on the same place on the canvas.

My question is how to properly reproduce the plot at the top of the thread?

I am not new to LaTeX but I am new to tikz and pgfplots.

Still I couldn't find an answer to this kind of issue. Only answers I found across the web are how to plot a single function.

The full code is given here:

Code: Select all

\documentclass{standalone} 
\usepackage{tikz,pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis} [xmin=0,xmax=20]
\addplot[samples=500,domain=0:20]{sin(2*pi*1.2*deg(x))};
\end{axis}

\draw[help lines] (0,-1) grid (20,1) [step=0.5cm];
% List of points:
\fill[red] (0,{sin(2*pi*1.2*0 r)}) circle (2pt);
\fill[red] (1,{sin(2*pi*1.2*1 r)}) circle (2pt);
\fill[red] (2,{sin(2*pi*1.2*2 r)}) circle (2pt);
\fill[red] (3,{sin(2*pi*1.2*3 r)}) circle (2pt);
\fill[red] (4,{sin(2*pi*1.2*4 r)}) circle (2pt);
\fill[red] (5,{sin(2*pi*1.2*5 r)}) circle (2pt);
\fill[red] (6,{sin(2*pi*1.2*6 r)}) circle (2pt);
\fill[red] (7,{sin(2*pi*1.2*7 r)}) circle (2pt);
\fill[red] (8,{sin(2*pi*1.2*8 r)}) circle (2pt);
\fill[red] (9,{sin(2*pi*1.2*9 r)}) circle (2pt);
\fill[red] (10,{sin(2*pi*1.2*10 r)}) circle (2pt);
\fill[red] (11,{sin(2*pi*1.2*11 r)}) circle (2pt);
\fill[red] (12,{sin(2*pi*1.2*12 r)}) circle (2pt);
\fill[red] (13,{sin(2*pi*1.2*13 r)}) circle (2pt);
\fill[red] (14,{sin(2*pi*1.2*14 r)}) circle (2pt);
\fill[red] (15,{sin(2*pi*1.2*15 r)}) circle (2pt);
\fill[red] (16,{sin(2*pi*1.2*16 r)}) circle (2pt);
\fill[red] (17,{sin(2*pi*1.2*17 r)}) circle (2pt);
\fill[red] (18,{sin(2*pi*1.2*18 r)}) circle (2pt);
\fill[red] (19,{sin(2*pi*1.2*19 r)}) circle (2pt);
\fill[red] (20,{sin(2*pi*1.2*20 r)}) circle (2pt);

\end{tikzpicture}
\end{document}
Any kind of help would be much appreciated.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
Bartman
Posts: 366
Joined: Fri Jan 03, 2020 2:39 pm

Combine pgfplots' axis environment and tikz graphics

Post by Bartman »

The grid and the red dots must be drawn within the axis environment so that the coordinates are correct.

My result-oriented adaptation of the mathematical expression is based on trial and error, it is not a mathematically desirable approach.

Code: Select all

\documentclass{standalone} 
\usepackage{pgfplots}

\pgfplotsset{compat=1.17}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    width=12cm,
    height=\pgfkeysvalueof{/pgfplots/width}/2,
    axis x line=center,
    axis y line=left,
    xmax=20.5,
    ymin=-1.2, ymax=1.2,
    xtick={0,5,...,20},
    ytick={-1,0,1},
    clip=false
  ]
  \addplot[samples=500,domain=0:20]{sin(2/5*pi*deg(x))};

  % List of points:
  \pgfplotsinvokeforeach{0,...,20}{
    \fill[red] (#1,{sin(2*pi*1.2*#1 r)}) circle (2pt);
  }
  \end{axis}
\end{tikzpicture}
\end{document}
tush
Posts: 11
Joined: Fri Feb 22, 2019 2:54 pm

Combine pgfplots' axis environment and tikz graphics

Post by tush »

@Bartman Thanks a lot!
Post Reply