Smoothing a pgfplot curve

pgfplotsplotsmooth

I am trying to generate plots with smooth curves using the pgfplots package.
Setting the smooth option in the \addplot command does not seem to make any change.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usepackage{filecontents}
\begin{filecontents*}{data.csv}
a,b
1,4
2,5
3,1
4,3
\end{filecontents*}


\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot table [smooth, x=a, y=b, col sep=comma] {data.csv};
\end{axis}
\end{tikzpicture}
\end{document}

Plot of a jagged curve

Best Answer

The smooth parameter should be given to the \addplot macro:

You may also try to change the tension parameter to a maybe higher value than the default of 0.55:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usepackage{filecontents}
\begin{filecontents*}{data.csv}
a,b
1,4
2,5
3,1
4,3
\end{filecontents*}


\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot[smooth,tension=0.8] table [x=a, y=b, col sep=comma] {data.csv};
\end{axis}
\end{tikzpicture}
\end{document}

Output:

Output

Or if you want to keep the color and markers:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usepackage{filecontents}
\begin{filecontents*}{data.csv}
a,b
1,4
2,5
3,1
4,3
\end{filecontents*}


\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot[smooth,blue,mark=*] table [x=a, y=b, col sep=comma] {data.csv};
\end{axis}
\end{tikzpicture}
\end{document}

Output

Edit For your actual data you could try something like this:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\usepackage[gobble=auto]{pythontex}
  
\begin{document}
\begin{pycode}
from numpy import *
from scipy.signal import savgol_filter

data = pd.read_csv('data.csv',sep=',')

x = data.x
y = data.y
## See https://stackoverflow.com/q/20618804/406686
yhat = savgol_filter(y, 51, 3) # window size 51, polynomial order 3

xyhat = list(zip(x,yhat))
savetxt('dataSmooth.csv',xyhat,fmt='%0.5f')
\end{pycode}
 
\begin{tikzpicture}
\begin{axis}
\addplot[smooth] table {dataSmooth.csv};
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}
\addplot[smooth] table [col sep=comma] {data.csv};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here