10

How do I smooth out the corners in the following graph?

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}[>=latex]
\begin{axis}[
  every axis/.append style={font=\scriptsize},
  x=.75cm,
  y=.75cm,    
  axis x line=center,
  axis y line=center,
  xtick={-5,-4,...,5},
  ytick={-5,-4,...,5},
  xlabel={$x$},
  ylabel={$y$},
  xlabel style={below right},
  ylabel style={above left},
  xmin=-5.5,
  xmax=5.5,
  ymin=-5.5,
  ymax=5.5]
\addplot [line width=2pt,mark=none,domain=-5:-2] {3};
\addplot [line width=2pt,mark=none,domain=-2:-1] { 2*x^2-5};
\addplot [line width=2pt,mark=none,domain=-1:1]  {-3};
\addplot [line width=2pt,mark=none,domain=1:2]   {2*x^2-5};
\addplot [line width=2pt,mark=none,domain=2:5]   {3};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

A.Ellett
  • 50,533

2 Answers2

12

An alternative is use of line cap=round in the addplot[option]

enter image description here

Code

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}[>=latex]
\begin{axis}[
  every axis/.append style={font=\scriptsize},
  x=.75cm,
  y=.75cm,    
  axis x line=center,
  axis y line=center,
  xtick={-5,-4,...,5},
  ytick={-5,-4,...,5},
  xlabel={$x$},
  ylabel={$y$},
  xlabel style={below right},
  ylabel style={above left},
  xmin=-5.5,
  xmax=5.5,
  ymin=-5.5,
  ymax=5.5]
\addplot [line width=2pt,line cap=round,mark=none,domain=-5:-2] {3};
\addplot [line width=2pt,line cap=round,mark=none,domain=-2:-1] { 2*x^2-5};
\addplot [line width=2pt,line cap=round,mark=none,domain=-1:1]  {-3};
\addplot [line width=2pt,line cap=round,mark=none,domain=1:2]   {2*x^2-5};
\addplot [line width=2pt,line cap=round,mark=none,domain=2:5]   {3};
\end{axis}
\end{tikzpicture}
\end{document}
Jesse
  • 29,686
6

Is it cheating to use a piecewise defined function?

screenshot

% arara: pdflatex
% !arara: indent: {overwrite: yes}
\documentclass[tikz,border=3pt]{standalone}
\usepackage{pgfplots}

\pgfmathdeclarefunction{MyFunction}{1}{%
    \pgfmathparse{%
        (and(   1,    #1<-2)*(3)          +%
        (and(#1>=-2,  #1< -1)*(2*#1^2 - 5)+%
        (and(#1>= -1,  #1< 1)*(-3)        +%
        (and(#1>= 1,  #1< 2)*(2*#1^2 - 5) +%
        (and(#1>= 2,    1  )*(3)%
    }%
}
\pgfplotsset{every axis/.append style={
    axis x line=center,
    axis y line=center,
    xtick={-5,-4,...,5},
    ytick={-5,-4,...,5},
    xlabel={$x$},
    ylabel={$y$},
    xlabel style={below right},
    ylabel style={above left},
    xmin=-5.5, xmax=5.5,
    ymin=-5.5, ymax=5.5,
    }
}


\begin{document}
\begin{tikzpicture}
    \begin{axis}
        \addplot[domain=-5:5, blue, samples=100, ultra thick] {MyFunction(x)};
    \end{axis}
\end{tikzpicture}

\end{document}

See What is the clearest way to graph a piecewise function? and the links within for reference.

cmhughes
  • 100,947
  • Not cheating at all. I'm curious though.... you omitted my rescale of the x and y units and the font size. Perhaps you just chose to omit these, but since you reworked my MWE very nicely I'm wondering whether there's a deeper significance to this. – A.Ellett May 28 '14 at 04:42
  • @A.Ellett not really any significance, just personal preference - I like my tick labels to inherit the fontsize of the other parts of the document, but again, just personal preference :) For a standalone document, the x and y units don't seem relevant – cmhughes May 28 '14 at 04:51
  • I'm a bit confused by the syntax pgfmath syntax. It looks like there are unbalanced parentheses. Could you explain what's happening there? Or, point me to the correct place in the documentation? – A.Ellett May 28 '14 at 11:52
  • @A.Ellett It seems the bug is not still fully fixed. See here http://tex.stackexchange.com/questions/41828/using-math-in-tikz – percusse May 29 '14 at 10:55