Is it possible to plot with TikZ
/pgfplots
the function f(x)=sin(\pi \cdot x)?
If so, how?
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.
Sign up to join this community\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}%
[grid=both,
minor tick num=4,
grid style={line width=.1pt, draw=gray!10},
major grid style={line width=.2pt,draw=gray!50},
axis lines=middle,
enlargelimits={abs=0.2}
]
\addplot[domain=-1:3,samples=50,smooth,red] {cos(deg(pi*x))};
\end{axis}
\end{tikzpicture}
\end{document}
One possible:
\documentclass[10pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
% CORES DE GRÁFICOS
\usepackage{xcolor} % configurações de cor
\definecolor{padrao-grafico-grid}{RGB}{195,220,227}
\definecolor{padrao-grafico1}{RGB}{67,128,134}
\definecolor{padrao-grafico2}{HTML}{1ABC9C}
\definecolor{padrao-grafico3}{RGB}{243,156,18}
\definecolor{padrao-grafico4}{HTML}{c0392b}
\definecolor{padrao-grafico5}{HTML}{8e44ad}
% GRÁFICOS
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{my style/.append style={axis x line=middle, axis y line=
middle, xlabel={$x$}, ylabel={$y$}, axis equal }}
\begin{document}
\begin{tikzpicture}
\begin{axis}[my style, grid=both, grid style={padrao-grafico-grid}, width=10cm, xtick={-1,0,...,3}, ytick={-1,0,...,1},
xmin=-2, xmax=4, ymin=-1.2, ymax=1.2]
% gráficos de funções
\addplot+[mark=none,smooth,color=padrao-grafico2] (\x,{cos(pi*deg(x))});
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
% remover grid - tirar grid=both, grid style={padrao_grafico4!15}, minor tick num=4
\begin{axis}[my style, grid=both, grid style={padrao-grafico-grid}, width=10cm, xtick={-3,-2,...,3}, ytick={-3,-2,...,3},
xmin=-4, xmax=4, ymin=-4, ymax=4]
%gráfico com legenda
\addplot+[mark=none,smooth,color=padrao-grafico1] (\x,{(e)^\x});
\label{plot_one}
\addlegendimage{/pgfplots/refstyle=plot_one}\addlegendentry{$y=e^x$}
% gráficos de funções
\addplot+[mark=none,smooth,color=padrao-grafico2] (\x,{\x^2});
\addplot+[mark=none,smooth,color=padrao-grafico3] (\x,{\x});
\addplot+[thick,mark=none,smooth,color=padrao-grafico4] (\x,{2*\x});
\addplot+[ultra thick,mark=none,smooth,color=padrao-grafico5] (\x,{3*\x});
% pontos
\addplot[smooth,mark=x,color=padrao-grafico1] coordinates{(2,3)};
\addplot[smooth,mark=o,color=padrao-grafico2] coordinates{(2,2)};
\addplot[smooth,mark=*,color=padrao-grafico3] coordinates{(2,1)};
% assintota
\addplot[dashed, color=padrao-grafico4] coordinates{(-3,-4) (-3,4)};
% ponto com coordenada
\addplot[smooth,mark=*,color=padrao-grafico3] coordinates{(3,-3)};
\addplot[dashed,very thin,color=padrao-grafico4] coordinates { (3,0) (3,-3) (0,-3) };
\addplot[mark=*] coordinates {(3,0)} node[pin=-50:{$(3,0)$}]{} ;
\addplot[mark=*,color=padrao-grafico4] coordinates {(2,-2)} node[right] {$A$};
\end{axis}
\end{tikzpicture}
\end{document}
samples
(200 is usually ok) to have a more relevant sin
function.
Apr 3, 2017 at 14:31
\documentclass[border=4mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines=middle,clip=false,
xmin=-3,xmax=4,ymin=-2,ymax=2,
ytick={-1,1},
xtick={-2,-1,0,1,2},
xticklabels={$-2$,$-1$,$0$,$1$,$2$},
xticklabel style={black},
xlabel=$x$,
ylabel=$y$]
\addplot[domain=-2:2,samples=200,orange]{sin(deg(pi*x))}
node[right,pos=0.9,font=\footnotesize]{$f(x)=\sin \pi x$};
\addplot[domain=-2:2,samples=200,magenta]{cos(deg(pi*x))}
node[right,pos=1,font=\footnotesize]{$f(x)=\cos \pi x$};
\end{axis}
\end{tikzpicture}
\end{document}
tikz
commands, and I tried doing a bit of coding that didn't work, like `\\addplot [blue, line width = 1, smooth, domain=-3:3] {sin(rad(\pi \cdot x)}; and some other ones.*
character for multiplication, and accidentally discovered thatpi
is the syntax for the symbol of pi (and not\pi
, and I tried so far). Please don't downvote the post.3.14
instead of\pi
and*
instead of\cdot
. Don't mix LaTeX commands for type setting stuff with math commands for plotting stuff. Just have a look at the pgfplots manual.