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.

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

share|improve this question

2 Answers 2

up vote 5 down vote accepted

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.

share|improve this answer
    
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 at 4: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 at 4: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 at 11:52
    
@A.Ellett It seems the bug is not still fully fixed. See here tex.stackexchange.com/questions/41828/using-math-in-tikz –  percusse May 29 at 10:55

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}
share|improve this answer

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.