LaTeX forum ⇒ Graphics, Figures & TablesSmooth Plot of Data Topic is solved

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

Smooth Plot of Data

Postby mas » Wed Apr 10, 2013 6:03 pm

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 12276 times


Wanted to get a similar output with pgfplot. My code is
  1. \documentclass{standalone}
  2. %
  3. \usepackage{tikz}
  4. \usetikzlibrary{arrows}
  5. \usetikzlibrary{calc,through,backgrounds}
  6. \usepackage{pgfplots}
  7. \pgfplotsset{compat=1.8}
  8. %\usetikzlibrary{pgfplots.patchplots}
  9.  
  10. \begin{document}
  11.  
  12. \begin{tikzpicture}
  13. \begin{semilogxaxis}[xlabel=Frequency (Hz), ylabel=Gain]
  14. \addplot[no marks, smooth] coordinates {
  15. (100 , 0.05)
  16. (200 , 0.12)
  17. (300 , 0.22)
  18. (400 , 0.32)
  19. (500 , 0.44)
  20. (700 , 0.7)
  21. (900 , 0.8)
  22. (1000, 0.85)
  23. (1500, 0.9)
  24. (2000, 0.95)
  25. (2500, 0.95)
  26. (3000, 0.95)
  27. (3500, 0.95)
  28. } ;
  29. \addplot[only marks,red!50!black] coordinates {
  30. (100 , 0.05)
  31. (200 , 0.12)
  32. (300 , 0.22)
  33. (400 , 0.32)
  34. (500 , 0.44)
  35. (700 , 0.7)
  36. (900 , 0.8)
  37. (1000, 0.85)
  38. (1500, 0.9)
  39. (2000, 0.95)
  40. (2500, 0.95)
  41. (3000, 0.95)
  42. (3500, 0.95)
  43. } ;
  44. \end{semilogxaxis}
  45. \end{tikzpicture}
  46.  
  47. \end{document}


I get a graph like

hipass-pgf2.png
hipass-pgf2.png (17.34 KiB) Viewed 12276 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

Tags:

User avatar
localghost
Site Moderator
Posts: 9204
Joined: Fri Feb 02, 2007 12:06 pm

Postby localghost » Wed Apr 10, 2013 6:31 pm

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
LaTeX Community Moderator
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes


¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1

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

Postby mas » Wed Apr 10, 2013 6:43 pm

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.
  1. unset key
  2. set logscale x
  3. set xrange [90:4e3]
  4. set yrange [0:1.1]
  5. set xlabel "Frequency (Hz)"
  6. set ylabel "Gain"
  7. set title "Frequency Response of a High Pass Filter"
  8. set terminal png
  9. set output "hipass.png"
  10. plot "hipass.dat" using ($1):($2/0.95) with points, \
  11. "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: 223
Joined: Thu Dec 04, 2008 4:39 am

Postby mas » Wed Apr 10, 2013 6:49 pm

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: 9204
Joined: Fri Feb 02, 2007 12:06 pm

Postby localghost » Wed Apr 10, 2013 7:38 pm

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.
  1. \documentclass[11pt]{standalone}
  2. \usepackage[T1]{fontenc}
  3. \usepackage{pgfplots}
  4. \pgfplotsset{
  5. compat=newest,
  6. xlabel near ticks,
  7. ylabel near ticks
  8. }
  9.  
  10. \usepackage{filecontents}
  11. \begin{filecontents*}{hipass.dat}
  12. x y
  13. 100 0.05
  14. 200 0.12
  15. 300 0.22
  16. 400 0.32
  17. 500 0.44
  18. 700 0.7
  19. 900 0.8
  20. 1000 0.85
  21. 1500 0.9
  22. 2000 0.95
  23. 2500 0.95
  24. 3000 0.95
  25. 3500 0.95
  26. \end{filecontents*}
  27.  
  28. \begin{document}
  29. \begin{tikzpicture}
  30. \begin{semilogxaxis}[
  31. xlabel={Frequency (Hz)},
  32. ylabel={Gain}
  33. ]
  34. \addplot[only marks,red!50!black] table[y expr=\thisrow{y}/0.95] {hipass.dat};
  35. \addplot[raw gnuplot,smooth] gnuplot {
  36. plot "hipass.dat" using ($1):($2/0.95) smooth sbezier
  37. };
  38. \end{semilogxaxis}
  39. \end{tikzpicture}
  40. \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 12257 times
LaTeX Community Moderator
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes


¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1

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

Postby mas » Thu Apr 11, 2013 3:02 am

Thanks. That is a nice solution.

Regards.

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


Return to “Graphics, Figures & Tables”

Who is online

Users browsing this forum: No registered users and 7 guests