3

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,
 ]

\addplot [blue,smooth] table {data1.txt};
\addplot [red,smooth] table {data2.txt};
\addplot [green,smooth] table {data3.txt};
\addplot [violet,smooth] table {data4.txt};
\addplot [orange,smooth] table {data5.txt};
\addplot [magenta,smooth] table {data6.txt};
\addplot [purple,smooth] table {data7.txt};
\addplot [cyan,smooth] table {data8.txt};
\addplot [brown,smooth] table {data9.txt};
\addplot [lime,smooth] table {data10.txt};
\addplot [olive,smooth] table {data11.txt};

 \end{axis}
 \end{tikzpicture}

\end{document}

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

  • 2
    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
  • 1
    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
3

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

\addplot [blue,smooth] table {data1.txt};
\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}

enter image description here

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.