Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am providing the code for graphing three trigonometric functions. (In the preamble, I had to put a % before the command \usetikzlibrary{calc,angles,quotes} because LaTeX could not compile it with the command. It gave me the error can't find file 'tikzlibraryangles.code.tex'. MikTeX2.9 is compiling the code.) How do I add the labels "$\scriptstyle{y = \sin(x)}," "$\scriptstyle{y = \cos(x)}," and "$\scriptstyle{y = \tan(x)}" directly under the y-axis to the corresponding graphs? Arrowheads should be at both ends of the axes. They are currently at one end. Is there a code for putting labels (in nodes) for the tick marks over other features of the graphs? How does the code instruct TikZ to marks the tick marks in half-unit increments? How do I get the code to instruct TikZ to mark the tick marks in increments of integral multiples of \pi/2? In the graphs of the tangent function, the asymptotes are solid lines. How do I get dotted lines? In the code for graphing the tangent function, there is "scaled x ticks={real:3.1415}, xtick scale label code/.code={}". What does "{real:3.1415}" and "code/.code={}" instruct TikZ to draw on the x-axis?

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
%\usetikzlibrary{calc,angles,quotes}
\usepackage{pgfplots}


\pgfkeys{/pgfplots/Axis Style/.style={
    width=13.5cm, height=5cm,
    axis x line=center, 
    axis y line=middle, 
    samples=100,
    ymin=-1.5, ymax=1.5,
    xmin=-7.0, xmax=7.0,
    domain=-2*pi:2*pi
}}


\begin{document}

\begin{tikzpicture}
\begin{axis}[
    Axis Style,
    xtick={
        -6.28318, -4.7123889, -3.14159, -1.5708,
        1.5708, 3.14159, 4.7123889, 6.28318
    },
    xticklabels={
        $-2\pi$, $-3\pi/2$, $-\pi$, $\pi/2$,
        $\pi/2$, $\pi$, $3\pi/2$, $2\pi$
    }
]
\addplot [mark=none, ultra thick, blue] {sin(deg(x))};
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[
    Axis Style,
    xtick={
        -6.28318, -4.7123889, -3.14159, -1.5708,
        1.5708, 3.14159, 4.7123889, 6.28318
    },
    xticklabels={
        $-2\pi$, $-3\pi/2$, $-\pi\hspace{0.30cm}$, $\pi/2$,
        $\pi/2$, $\pi\hspace{0.10cm}$, $3\pi/2$, $\hspace{0.25cm} 2\pi$
    }
]
\addplot [mark=none, ultra thick, red] {cos(deg(x))};
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[
    Axis Style,
    xtick={-6.28318, -4.7123889, ..., 6.28318},
    scaled x ticks={real:3.1415},
    xtick scale label code/.code={},
]
\addplot [mark=none, thin, brown] {tan(deg(x))};
\end{axis}
\end{tikzpicture}


\end{document}
share|improve this question
    
Welcome to TeX.SX! The pgfplots manual has answers to your questions and much, much more. –  Paul Gessler Aug 5 at 18:00

2 Answers 2

up vote 6 down vote accepted

You've asked quite a few questions! It's generally best to ask one, focused question on this site ;)

But, here's an attempt to get through your questions....

Labels, such as $y = \sin(x), would make the graphs more presentable. I guess directly under the y-axis.

You could use, for example,

\begin{axis}[
    Axis Style,
    xtick={
        -6.28318, -4.7123889, -3.14159, -1.5708,
        1.5708, 3.14159, 4.7123889, 6.28318
    },
    xticklabels={
        $-2\pi$, $-3\pi/2$, $-\pi$, $\pi/2$,
        $\pi/2$, $\pi$, $3\pi/2$, $2\pi$
    },
    ylabel={$y=\sin(x)$}, %<---- new bit
]

or else, for example,

...
\addplot [mark=none, ultra thick, blue] {sin(deg(x))};
\addlegendentry{$y=\sin(x)$} %<---- new bit
...

You might also want to adjust the legend position using legend pos=...

Arrowheads should be at both ends of the axes. They are currently at one end.

You can use,

\pgfkeys{/pgfplots/Axis Style/.style={
    width=.3\textwidth, %height=5cm,
    axis x line=center, 
    axis y line=middle, 
    samples=100,
    ymin=-1.5, ymax=1.5,
    xmin=-7.0, xmax=7.0,
    domain=-2*pi:2*pi,
    axis line style=<->,  %<----- new bit
}}

or otherwise use pgfplotsset (as demonstrated below).

The scale is suitable for the graphs of the sine and cosine functions but not for the tangent function. Maybe have the y-axis include -10 and 10.

Try using, for example,

\begin{axis}[
    Axis Style,
    xtick={-6.28318, -4.7123889, ..., 6.28318},
    scaled x ticks={real:3.1415},
    xtick scale label code/.code={},
    ymin=-10,ymax=10, %<---- new bit
]

Changing the scale for this graph may have the labels along the axes drawn over by the graph of the tangent function. Is there a code for putting labels (in nodes) over other features of a graph?

I believe you want the axis on top key for this.

Currently, the y-axes of the three graphs are aligned. How can I have the x-axes are aligned?

Remove the blank lines between your figures - note that you'll need to adjust the text width to get them to fit nicely - consider the geometry package for this.

What does mark=none instruct TikZ to do? What other options are there for mark?

mark=none means that you only get the curve, and not circles (or other marks) at the sample points along the way. There are a lot of other options - see Section 4.7 of the pgfplots for details.

Here's a complete bit of code to play with that implements the things I have mentioned.

% arara: pdflatex
% !arara: indent: {overwrite: yes}
\documentclass{article}
\usepackage{pgfplots}
\usepackage[margin=1cm]{geometry}
\pgfplotsset{Axis Style/.style={
    width=.3\textwidth, %height=5cm,
    axis x line=center,
    axis y line=middle,
    samples=100,
    ymin=-1.5, ymax=1.5,
    xmin=-7.0, xmax=7.0,
    domain=-2*pi:2*pi,
    axis line style=<->,
}}


\begin{document}

\begin{tikzpicture}
    \begin{axis}[
            Axis Style,
            xtick={
                -6.28318, -4.7123889, -3.14159, -1.5708,
                1.5708, 3.14159, 4.7123889, 6.28318
            },
            xticklabels={
                $-2\pi$, $-3\pi/2$, $-\pi$, $\pi/2$,
                $\pi/2$, $\pi$, $3\pi/2$, $2\pi$
            },
            ylabel={$y=\sin(x)$},
        ]
        \addplot [mark=none, ultra thick, blue] {sin(deg(x))};
        \addlegendentry{$y=\sin(x)$}
    \end{axis}
\end{tikzpicture}
\begin{tikzpicture}
    \begin{axis}[
            Axis Style,
            xtick={
                -6.28318, -4.7123889, -3.14159, -1.5708,
                1.5708, 3.14159, 4.7123889, 6.28318
            },
            xticklabels={
                $-2\pi$, $-3\pi/2$, $-\pi\hspace{0.30cm}$, $\pi/2$,
                $\pi/2$, $\pi\hspace{0.10cm}$, $3\pi/2$, $\hspace{0.25cm} 2\pi$
            }
        ]
        \addplot [mark=none, ultra thick, red] {cos(deg(x))};
    \end{axis}
\end{tikzpicture}
\begin{tikzpicture}
    \begin{axis}[
            Axis Style,
            xtick={-6.28318, -4.7123889, ..., 6.28318},
            scaled x ticks={real:3.1415},
            xtick scale label code/.code={},
            ymin=-10,ymax=10,
        ]
        \addplot [mark=none, ultra thick, brown] {tan(deg(x))};
    \end{axis}
\end{tikzpicture}


\end{document}
share|improve this answer

An easy-to-customize template with PSTricks.

\documentclass[pstricks,border=0pt,12pt,dvipsnames]{standalone}
\usepackage{amsmath}
\usepackage{pstricks-add}
\usepackage{pst-plot}
% \usepackage{pst-math} provides ATAN
\usepackage[nomessages]{fp}

\FPeval\XMin{0-2*pi}
\FPeval\XMax{2*pi}
\FPeval\YMin{0-pi}
\FPeval\YMax{5*pi/3}

\FPeval\XOL{0-1/3} % of DeltaX
\FPeval\XOR{1/3} % of DeltaX
\FPeval\YOB{0-1/3} % of DeltaY
\FPeval\YOT{1/3} % of DeltaY

\FPset\TrigLabelBase{3}
\FPeval\DeltaX{pi/TrigLabelBase}
\FPeval\DeltaY{pi/TrigLabelBase}

\FPeval\AxisL{XMin+DeltaX*XOL}
\FPeval\AxisR{XMax+DeltaX*XOR}
\FPeval\AxisB{YMin+DeltaY*YOB}
\FPeval\AxisT{YMax+DeltaY*YOT}

\newlength\Width\Width=12cm
\newlength\Height\Height=8cm

\newlength\llx\llx=-5pt
\newlength\urx\urx=15pt
\newlength\lly\lly=-5pt
\newlength\ury\ury=15pt


\psset
{
    llx=\llx,
    lly=\lly,
    urx=\urx,
    ury=\ury,
    xtrigLabels=true,
    ytrigLabels=true,
    trigLabelBase=\TrigLabelBase,
    labelFontSize=\scriptstyle,
    xAxisLabel=$x$,
    yAxisLabel=$y$,
    algebraic,
    plotpoints=500,
}

\def\f{Pi*sin(x)}
\def\fp{Derive(1,\f)}

\def\g{2*asin(x/(2*Pi))}

\begin{document}
\pslegend[lt]{%
    \color{NavyBlue}\rule{12pt}{1pt} & \color{NavyBlue} $y=\pi\sin x$ \\
    \color{Red}\rule{12pt}{1pt} & \color{Red} $y=\pi\cos x$ \\
    \color{ForestGreen}\rule{12pt}{1pt} & \color{ForestGreen} $y=2\sin^{-1}\left(\tfrac{x}{2\pi}\right)$
}
\begin{psgraph}
    [
        dx=\DeltaX,
        dy=\DeltaY,
        linecolor=gray,
        tickcolor=gray,
        ticksize=-3pt 3pt,
        axespos=top,
    ]{<->}(0,0)(\AxisL,\AxisB)(\AxisR,\AxisT){\dimexpr\Width-\urx+\llx}{!}%{\dimexpr\Height-\ury+\lly}
    \psaxes
    [
        dx=\DeltaX,
        dy=\DeltaY,
        labels=none,
        subticks=5,
        tickwidth=.4pt,
        subtickwidth=.2pt,
        tickcolor=Red!30,
        subtickcolor=ForestGreen!30,
        xticksize=\YMin\space \YMax,
        yticksize=\XMin\space \XMax,
        subticksize=1,
    ](0,0)(\XMin,\YMin)(\XMax,\YMax)
    \psplot[linecolor=NavyBlue]{\XMin}{\XMax}{\f}
    \psplot[linecolor=Red]{\XMin}{\XMax}{\fp}
    \psplot[linecolor=ForestGreen]{\XMin}{\XMax}{\g}
    % -------------------
    % tangent and normal
    \psplotTangent[linecolor=Magenta]{Pi 3 div 2 mul}{1.5}{\f}
    \psplotTangent[linecolor=Cyan,Derive={-1/\fp}]{Pi 3 div 2 mul}{1.5}{\f}
    %
    % sum
    \psplot[linecolor=Rhodamine]{\XMin}{\XMax}{Sum(i,0,1,7,(-1)^(i)*x^(2*i)/fact(2*i))}
\end{psgraph}
\end{document}

enter image description here

Documentation

This section explains the settings used in the template.

enter image description here

share|improve this answer
1  
+1 Great answer. –  Enthusiastic Student Aug 11 at 15:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.