Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I learned recently how to draw in pgfplots and is really enjoying the output. I generate my data in matlab and then write it to a file. One line of the file will contain two entries, first x-axis data and then corresponding y-axis data. Now I realize, I actually want the y-axis data to be in dB , i.e. whatever current values are in that file, I want 'x' against '10*log(y)' where logarithm is base 10. But, now I can't go back to matlab and generate all this data again. So I have to do it within pgfplots. Is this possible?

For a MWE, this is the data I have in a file by name fig1-line1.dat

0   0.095529
1   0.12026
2   0.1514
3   0.19061
4   0.23996
5   0.30209
6   0.38031
7   0.47878
8   0.60275
9   0.75882
10  0.95529

This is my current code which plots it.

\documentclass[a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds,shapes,shadows,fit,calc}
\usetikzlibrary[arrows,decorations.pathmorphing,backgrounds,positioning,fit,petri]
\usepackage{pgfplots}
\pgfplotsset{compat=1.3}% <-- moves axis labels near ticklabels (respects tick label widths)
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}



\pgfplotsset{every axis legend/.append style={legend pos = north west},legend cell align = {left}}

\pgfplotsset{%
tick label style={font=\normalsize},
title style={font=\normalsize,align=center},
label style={font=\normalsize,align=center},
legend style={font=\normalsize}
            }

\begin{axis}[%
xlabel = {Iterations},
ylabel = {Relative Error in Convergence $\epsilon_i$},
title  = {Convergence Rate v/s Iterations},
enlargelimits = true,
cycle list name = {color},
grid = major,
smooth,
scale = 1]

\addlegendentry{$K=2$, $M=3$}

\addplot [ color = {blue}, 
           mark  = {o},
           style = {solid},
           line width = 2pt] table {fig1-line1.dat};



\end{axis}
\end{tikzpicture}
\caption{A larger example}
\end{figure}
\end{document}

So the current plot is 'x' against 'y'. Now I want to make it 'x' against '10*log(y)'? How do I do this?

share|improve this question
    
Set ymode=log in the axis options –  Jake Oct 10 '13 at 9:46
    
hello jake, will it make it $10*log()$? –  dineshdileep Oct 10 '13 at 9:47
    
Sorry, I didn't read your question carefully enough. ymode=log will not work in this case. You'll have to transform your data. I added an answer. –  Jake Oct 10 '13 at 9:57

2 Answers 2

up vote 6 down vote accepted

You can transform your data using y filter/.code={\pgfmathparse{10*log10(\pgfmathresult))}:

\documentclass[a4paper]{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}

\begin{axis}[
y filter/.code={\pgfmathparse{10*log10(\pgfmathresult)}}
]
\addplot table {
0   0.095529
1   0.12026
2   0.1514
3   0.19061
4   0.23996
5   0.30209
6   0.38031
7   0.47878
8   0.60275
9   0.75882
10  0.95529           
};
\end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer
    
+1, nice! ______________ –  dineshdileep Oct 10 '13 at 10:04

I don't know why you have 10 but decibels have 20 as a factor but you can modify it as you wish.

\documentclass[border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{every axis legend/.append style={legend pos = north west},legend cell align = {left}}

\begin{document}
\begin{tikzpicture}
\begin{semilogyaxis}[
log basis y=10,
yticklabel={\pgfmathparse{20*(\tick)}\pgfmathprintnumber[fixed]{\pgfmathresult}},
xlabel = {Iterations},
ylabel = {Relative Error in Convergence $\epsilon_i$ [dB]},
title  = {Convergence Rate v/s Iterations},
enlargelimits = true,
cycle list name = {color},
grid = major,
smooth,
scale = 1]
\addlegendentry{$K=2$, $M=3$}

\addplot [ color = {blue}, 
           mark  = {o},
           style = {solid},
           line width = 2pt] table {
0   0.095529
1   0.12026
2   0.1514
3   0.19061
4   0.23996
5   0.30209
6   0.38031
7   0.47878
8   0.60275
9   0.75882
10  0.95529
};
\end{semilogyaxis}
\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer
    
where do I change $20$ to $10$? –  dineshdileep Oct 10 '13 at 10:03
    
@dineshdileep in yticklabel options but then remove the [dB] from the ylabel too. –  percusse Oct 10 '13 at 10:04
1  
en.wikipedia.org/wiki/Decibel . For me, its a ratio of powers. –  dineshdileep Oct 10 '13 at 10:15
    
+1,________________ –  dineshdileep Oct 10 '13 at 10:15
    
@dineshdileep Well, well I didn't know they have different versions of dB. It's strange I have never come across those conventions though I'm using db scale daily with 20log10 for 10+ years because I work mainly of power ratios. I guess I should never take things for granted. –  percusse Oct 10 '13 at 10:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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