Graphics, Figures & TablesDifferent Curves for the same Data

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

Different Curves for the same Data

Post by mas »

My query is somewhat related to my earlier question.

My tex file is given as this.

Code: Select all

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

\usepackage{filecontents}
\begin{filecontents*}{lopass.dat}
x y
100       10
200       10
300       10
400       10
500       9
1000      6.5
2000      4.2
4000      2.4
6000      1.6
8000      1.2
10000     0.75
\end{filecontents*}

\begin{document}
  \begin{tikzpicture}
    \begin{semilogxaxis}[
      xlabel={Frequency (Hz)},
      ylabel={Gain}
    ]
      \addplot[only marks,blue!50] table[y expr=\thisrow{y}/10.0] {lopass.dat};
      \addplot[raw gnuplot,smooth] gnuplot {
        plot "lopass.dat" using ($1):($2/10.0) smooth sbezier
      };
    \end{semilogxaxis}
  \end{tikzpicture}
\end{document}
This results in the graph
lopass.jpg
lopass.jpg (14.75 KiB) Viewed 5719 times
When I try plotting the same data directly in gnuplot with the code

Code: Select all

unset key
set logscale x
set yrange [0:1.2]
set xrange [80:11.5e3]
set xlabel "Frequency (Hz)"
set ylabel "Gain"
set title "Frequency Response of a Low Pass Filter"
plot "lowpass.dat" using ($1):($2/10.) with points, \
     "lowpass.dat" using ($1):($2/10.) smooth sbezier
and the data in lowpass.data as

Code: Select all

#f        A
100       10
200       10
300       10
400       10
500       9
1000      6.5
2000      4.2
4000      2.4
6000      1.6
8000      1.2
10000     0.75
the resulting graph looks like
lp.png
lp.png (4.8 KiB) Viewed 5719 times
Am I wrong to assume that both graphs should be identical as Gnuplot is the one doing the computation in both the cases?

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

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

Different Curves for the same Data

Post by mas »

Excuse me for trying to answer my own question. :oops:

I took a look at the table generated by gnuplot, lopass.pgf-plot.table. Plotting it in gnuplot gave me a curve as gien by pgfplot. This ruled out the possibility of any problem with pgfplots as gnuplot was the one generating the table.

I tried creating the table and discovered that using the code

Code: Select all

set samples 500
set logscale x
set table "lp0.dat" ; set format "%.5f"
plot "lopass.dat" using ($1):($2/10.) smooth sbezier
gave me the proper curve. Not using the "set logscale x" instruction, gives a different curve. This is the case even in my earlier hipass curve. But, the difference there is not that obvious.

Now, my question is, how to make sure that pgfplots includes the instruction "set logscale x" in the gnuplot file it creates?

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

Different Curves for the same Data

Post by mas »

Posting my solution for whoever might need it. The following code works cleanly:

Code: Select all

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

\usepackage{filecontents}
\begin{filecontents*}{lopass.dat}
x y
100       10
200       10
300       10
400       10
500       9
1000      6.5
2000      4.2
4000      2.4
6000      1.6
8000      1.2
10000     0.75
\end{filecontents*}

\begin{document}
  \begin{tikzpicture}
    \begin{semilogxaxis}[
      xlabel={Frequency (Hz)},
      ylabel={Gain}
    ]
      \addplot[only marks,blue!50] table[y expr=\thisrow{y}/10.0] {lopass.dat};
      \addplot[raw gnuplot,smooth] gnuplot {
        set logscale x; plot "lopass.dat" using ($1):($2/10.0) smooth sbezier
      };
    \end{semilogxaxis}
  \end{tikzpicture}
\end{document}
Just to show the difference, here is the LaTeX code:

Code: Select all

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

\usepackage{filecontents}
\begin{filecontents*}{lopass.dat}
x y
100       10
200       10
300       10
400       10
500       9
1000      6.5
2000      4.2
4000      2.4
6000      1.6
8000      1.2
10000     0.75
\end{filecontents*}

\begin{document}
  \begin{tikzpicture}
    \begin{semilogxaxis}[
      xlabel={Frequency (Hz)},
      ylabel={Gain}
    ]
      \addplot[only marks,blue!50] table[y expr=\thisrow{y}/10.0] {lopass.dat};
      \addplot[raw gnuplot,smooth] gnuplot {
        set logscale x; plot "lopass.dat" using ($1):($2/10.0) smooth sbezier
      };
      \addplot[raw gnuplot,smooth,red,dashed] gnuplot {
        plot "lopass.dat" using ($1):($2/10.0) smooth sbezier
      };
    \end{semilogxaxis}
  \end{tikzpicture}
\end{document}
The graph looks as shown below. The red, dashed line is what we get if "set logscale x" is not used.
lopass.jpg
lopass.jpg (16.28 KiB) Viewed 5693 times

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

Different Curves for the same Data

Post by localghost »

mas wrote:Posting my solution for whoever might need it.[…]
Thanks for sharing. I'm quite sure that I will need this anytime since I quite often have to plot data sets. Seems as if this is also applicable to the "hipass" version.


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

Different Curves for the same Data

Post by mas »

Yes, I made a mention of it and forgot to link to the post :-(
[...]
This is the case even in my earlier hipass curve. But, the difference there is not that obvious.

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