3

I have been trying to create this graph with pgfplots and tikz. The original image.

I have most of it down but I cannot seem to get the axes centered like they are shown in the picture and have the tick labels on the sides like a boxplot as well as the labels only at intervals of $\pi$ on the x axis.

The code below gives me the pictured result below:

    \documentclass[tikz]{standalone}
\usetikzlibrary{arrows.meta}
\usepackage{pgfplots} \pgfplotsset{compat=1.12}

\begin{document}
  \begin{tikzpicture}[>=Stealth]
    \begin{axis}
      [
      grid=major,
      axis lines = center,
      xmin=-2*pi-0.5, xmax=3*pi+0.5,
      ymin=-2, ymax=3,
      xlabel=$x$, ylabel=$y$,
      domain= -2*pi:3*pi
      ]
      \plot[smooth, samples=200, thick, color=blue, <->  ]{2*sin(deg(x))+1}; %deg() used to convert radians to degrees since pgfplots wnats degrees
    \end{axis}
  \end{tikzpicture}
\end{document}

The output of code above.

6

Like this?

enter image description here

In code are marked lines which is added to your MWE:

    \documentclass[border=3mm]{standalone}% <--- changed
\usepackage{pgfplots} 
    \pgfplotsset{compat=1.12}
    \usetikzlibrary{arrows.meta}

\begin{document}
  \begin{tikzpicture}[>=Stealth]
    \begin{axis}
      [
      outer axis line style={draw=gray!50},% <-- added
      grid,
      xmin=-2*pi-0.5, xmax=3*pi+0.5,
      xtick={-6.28,-3.14,0,3.14,6.28,9.42},% <-- added
      xticklabels={$-2\pi$,$-\pi$,0,$\pi$,$2\pi$,$3\pi$},% <-- added
      ymin=-2, ymax=3,
      xlabel=$x$, ylabel=$y$,
      domain= -2*pi:3*pi
      ]
      \addplot[smooth, samples=200, thick, color=blue, <->  ]{2*sin(deg(x))+1}; %deg() used to convert radians to degrees since pgfplots wnats degrees
      \addplot[draw=none,color=blue, mark=*] coordinates {(-pi,1) (0,1) (pi,1) (2*pi,1)};% <-- added
  \draw[<->] (0,-2) -- (0,3);% <-- added
  \draw[<->] (-6.78,0) -- (9.92,0);% <-- added
      \end{axis}
  \end{tikzpicture}
\end{document}
2
  • Yeah that's great! I'm seeing that you used the \draw commands to draw the axis lines yourself correct? – 1028 Aug 4 '16 at 4:53
  • yes, because I use standard boxed axis for put ticks on graph outside. – Zarko Aug 4 '16 at 4:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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