Graphics, Figures & TablesSmooth Plot of Data

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
mas
Posts: 226
Joined: Thu Dec 04, 2008 4:39 am

Smooth Plot of Data

Post by mas »

I am trying to use pgfplots. Using Gnuplot, I can get a smooth curve as shown in figure below:
hipass-gp2.png
hipass-gp2.png (14.06 KiB) Viewed 16169 times
Wanted to get a similar output with pgfplot. My code is

Code: Select all

\documentclass{standalone}
%
\usepackage{tikz}
\usetikzlibrary{arrows}
\usetikzlibrary{calc,through,backgrounds}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
%\usetikzlibrary{pgfplots.patchplots}

\begin{document}

\begin{tikzpicture}
\begin{semilogxaxis}[xlabel=Frequency (Hz), ylabel=Gain]
\addplot[no marks, smooth] coordinates {
    (100 , 0.05)
    (200 , 0.12)
    (300 , 0.22)
    (400 , 0.32)
    (500 , 0.44)
    (700 , 0.7)
    (900 , 0.8)
    (1000, 0.85)
    (1500, 0.9)
    (2000, 0.95)
    (2500, 0.95)
    (3000, 0.95)
    (3500, 0.95)
  } ;
\addplot[only marks,red!50!black] coordinates {
    (100 , 0.05)
    (200 , 0.12)
    (300 , 0.22)
    (400 , 0.32)
    (500 , 0.44)
    (700 , 0.7)
    (900 , 0.8)
    (1000, 0.85)
    (1500, 0.9)
    (2000, 0.95)
    (2500, 0.95)
    (3000, 0.95)
    (3500, 0.95)
  } ;
\end{semilogxaxis}
\end{tikzpicture}

\end{document}
I get a graph like
hipass-pgf2.png
hipass-pgf2.png (17.34 KiB) Viewed 16169 times
What options should I give to get a plot similar to what I get from Gnuplot?

OS: Debian/GNU Linux; LaTeX System : TeXLive; Editor : Vim

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Smooth Plot of Data

Post by localghost »

The green curve in the Gnuplot output seems to be the result of a fit operation, perhaps a sin²(a·x). Please post the corresponding Gnuplot script. But you could also read about the raw gnuplot key in the pgfplots manual to learn how to work with Gnuplot scripts inside pgfplots. A similar example has already been dicussed here some time ago [1]. Perhaps that can help you on.

[1] pgfplots | Logarithmic Trend line


Thorsten
mas
Posts: 226
Joined: Thu Dec 04, 2008 4:39 am

Smooth Plot of Data

Post by mas »

localghost wrote:The green curve in the Gnuplot output seems to be the result of a fit operation, perhaps a sin²(a·x). Please post the corresponding Gnuplot script.
The Gnuplot script I used is this one.

Code: Select all

unset key
set logscale x
set xrange [90:4e3]
set yrange [0:1.1]
set xlabel "Frequency (Hz)"
set ylabel "Gain"
set title "Frequency Response of a High Pass Filter"
set terminal png 
set output "hipass.png"
plot "hipass.dat" using ($1):($2/0.95) with points, \
     "hipass.dat" using ($1):($2/0.95) smooth sbezier
But you could also read about the raw gnuplot key ...
Will do that and see if it helps.

OS: Debian/GNU Linux; LaTeX System : TeXLive; Editor : Vim
mas
Posts: 226
Joined: Thu Dec 04, 2008 4:39 am

Smooth Plot of Data

Post by mas »

localghost wrote:A similar example has already been dicussed here some time ago [1]. Perhaps that can help you on.

[1] pgfplots | Logarithmic Trend line
Took a look at that. As you can see from the gnuplot script, it is a bezier curve. Hence, there is no single function for the entire range.

Alternately, I looked at the patchplots of pgfplots. That also did not come out as I expected. My understanding about that is still very fuzzy. The output was not what I expected :-(

OS: Debian/GNU Linux; LaTeX System : TeXLive; Editor : Vim
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Smooth Plot of Data

Post by localghost »

As I already presumed the raw gnuplot key is one key to solve the problem. The other one is y expr to manipulate values in the y column. Your code then will look like this. The output is attached.

Code: Select all

\documentclass[11pt]{standalone}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}
\pgfplotsset{
  compat=newest,
  xlabel near ticks,
  ylabel near ticks
}

\usepackage{filecontents}
\begin{filecontents*}{hipass.dat}
x y
100 0.05
200 0.12
300 0.22
400 0.32
500 0.44
700 0.7
900 0.8
1000 0.85
1500 0.9
2000 0.95
2500 0.95
3000 0.95
3500 0.95
\end{filecontents*}

\begin{document}
  \begin{tikzpicture}
    \begin{semilogxaxis}[
      xlabel={Frequency (Hz)},
      ylabel={Gain}
    ]
      \addplot[only marks,red!50!black] table[y expr=\thisrow{y}/0.95] {hipass.dat};
      \addplot[raw gnuplot,smooth] gnuplot {
        plot "hipass.dat" using ($1):($2/0.95) smooth sbezier
      };
    \end{semilogxaxis}
  \end{tikzpicture}
\end{document}
Remarks:
  • The filecontents package and the corresponding filecontents* environment are only used here to generate the data file, thus not necessary in the actual document.
Attachments
ztmp.png
ztmp.png (5.77 KiB) Viewed 16150 times
mas
Posts: 226
Joined: Thu Dec 04, 2008 4:39 am

Re: Smooth Plot of Data

Post by mas »

Thanks. That is a nice solution.

Regards.

OS: Debian/GNU Linux; LaTeX System : TeXLive; Editor : Vim
Post Reply