# How to smooth spectra plotted with Tikz?

I want to smooth these spectra which were plotted from lab experimental data, with more than 1000 point for every spectrum. I tried to control smoothing using tension=1, or "each nth point", niether worked for me, as shown in picture below. This is MWC:

\documentclass{article}
\usepackage{pgfplots}

\pgfplotsset{width=7cm,compat=1.16}
\usepackage{pgfplotstable}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
name=plot,width=0.9\textwidth, xmin=230, xmax=400,
line width=1.2,
smooth,
label style={font=\bfseries\Large},
xlabel={conc},
ylabel={absorbance},
legend pos=north east,
]

\end{axis}
\end{tikzpicture}

\end{document}


Sample file data1.txt https://gofile.io/?c=AVXV71

• Welcome! Could you please provide a data file that you are using? (It probably suffices to cook down the example to one plot.) – Schrödinger's cat Oct 24 at 16:38
• Without concrete data it is impossible to provide a concrete answer. You may try, though, to plot only every 10th (say) point by installing a filter, see e.g. tex.stackexchange.com/a/485779. – Schrödinger's cat Oct 24 at 17:51
• Not sure if this is what you expect, but smooth doesn't smooth plots in the sense that a filter or moving average does smoothing. What it does is change how the connections between data points are drawn. Instead of drawing straight lines, with smooth you get curves. But when the data points are very close together, this doesn't have much effect. – Torbjørn T. Oct 24 at 18:07
• Dear: @Schrödinger's cat, I have incorporated a sample file in the link above. Thank you in advance. here gofile.io/?c=AVXV71 – kosofo Oct 24 at 18:30

It appears to me that your data was obtained from some sort of truncation. That is, the values have been rounded in some way. It is, of course, impossible to undo the rounding. Can one produce a smooth plot? Yes. A very blunt approach is to only consider every nth point. This can be achieve by installing a filter.

\documentclass{article}
\usepackage{pgfplots}

\pgfplotsset{width=7cm,compat=1.16}
\usepackage{pgfplotstable}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
name=plot,width=0.9\textwidth, xmin=230, xmax=400,
line width=1.2,
smooth,
label style={font=\bfseries\Large},
xlabel={conc},
ylabel={absorbance},
legend pos=north east,
x filter/.expression={(mod(\coordindex,25) >0)? nan : x}
]

\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[
name=plot,width=0.9\textwidth, xmin=230, xmax=400,
line width=1.2,
smooth,
label style={font=\bfseries\Large},
xlabel={conc},
ylabel={absorbance},
legend pos=north east,
]

\addplot [blue,no markers, raw gnuplot] gnuplot {
plot 'data1.txt' smooth sbezier;
};

\end{axis}
\end{tikzpicture}
\end{document}


This plot is smooth but does not do real justice to the data. An arguably much better way to proceed is to do as discussed here: employ gnuplot. This requires you to compile with -shell-escape. The result is the second example which results in the lower plot.