如何平滑 pgfplots 图的角落

如何平滑 pgfplots 图的角落

如何使下图中的角变得平滑?

\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}

在此处输入图片描述

答案1

使用分段定义函数是否作弊?

截屏

% 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}

绘制分段函数图像最清晰的方法是什么?以及其中的链接以供参考。

答案2

line cap=round另一种方法是使用 addplot[option]

在此处输入图片描述

代码

\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}

相关内容