10

I want to draw some functions with gnuplot. It works well, but the first half is missing and I don't know why.

\begin{figure}[htpb]
\begin{center}
\begin{tikzpicture}
\begin{axis}[width=0.8\textwidth, xlabel={Anzahl der Prozessoren}, ylabel={Speedup}, xmin=1, xmax=4096, ymin=0, ymax=30, xmode = log, log basis x={2},xticklabel=\pgfmathparse{2^\tick}\pgfmathprintnumber{\pgfmathresult}]
\addplot gnuplot[black, solid, raw gnuplot,id=bal,mark=none,very thick]{
set xrange [0:4096];
set yrange [0:30];
p = 0.5;
f(x)=1/((1-p) + p/x);
plot f(x)};
\addplot gnuplot[black, dashed, raw gnuplot,id=bal,mark=none,very thick]{
set xrange [0:4096];
set yrange [0:30];
p = 0.8;
f(x)=1/((1-p) + p/x);
plot f(x)};
\addplot gnuplot[black, dotted, raw gnuplot,id=bal,mark=none,very thick]{
set xrange [0:4096];
set yrange [0:30];
p = 0.9;
f(x)=1/((1-p) + p/x);
plot f(x)}; 
\addplot gnuplot[black, loosely dotted, raw gnuplot,id=bal,mark=none,very thick]{
set xrange [0:4096];
set yrange [0:30];
p = 0.95;
f(x)=1/((1-p) + p/x);
plot f(x)};     
\legend{%
P = 50\%,
P = 80\%,
P = 90\%,
P = 95\%,
}           
\end{axis}
\end{tikzpicture}
\end{center}
\caption{Speedup nach Amdahla'sches Gesetz}
\label[figure]{fig:amdahls_law_speedup}
\end{figure}

My result is this:

enter image description here

But it should be like this:

enter image description here

I am new with pgfplots. So maybe the code contains redundant data too. But why is the first part of the plots missing?

  • Just to say that gnuplot is not required for plotting your stuff... – s__C Sep 5 '14 at 12:53
12

This happens because there are no valid samples in the first half of your plot.

gnuplot by default uses 100 samples, starting with the first number in the sampling range and dividing the range equally in linear space. That means that in your case, you have samples at 0, 41.4, 82.8, etc. The first sample evaluates to NaN because of the division by zero in your equation.

A quick fix would be to set the range to 1:4096, ensuring that you get a valid sample at the start. However, this wouldn't result in an accurate representation of your equation, since almost all samples are in the upper regions of your graph. A better approach is to also use set logscale x; in the gnuplot options to evenly spread the samples in logarithmic space:


In this case, there's no need to use gnuplot at all. This plot can be generated using only the built-in PGF math engine:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    width=0.8\textwidth,
    xlabel={Anzahl der Prozessoren},
    ylabel={Speedup},
    domain = 1:4096,
    xmin=1, xmax=4096,
    ymin=0, ymax=30,
    xmode = log,
    log basis x={2},
    log ticks with fixed point
]
\addplot [black, very thick]{
1/((1-0.5) + 0.5/x)
};
\addplot [black, very thick, dashed]{
1/((1-0.8) + 0.8/x)
};
\addplot [black, very thick, dotted]{
1/((1-0.9) + 0.9/x)
};
\addplot [black, very thick, loosely dotted]{
1/((1-0.95) + 0.95/x)
};;     
\legend{%
P = 50\%,
P = 80\%,
P = 90\%,
P = 95\%,
}           
\end{axis}
\end{tikzpicture}
\end{document}
|improve this answer|||||
  • Very good explained answer! It works now. Can you tell me how you make ticks only for 2,8,32,128,512,2048 ? I have all from 1 to 4096 and it will be covered at the end. – Burak Sep 5 '14 at 13:39
  • @user2514411: PGFPlots automatically reduces the number of ticks if the labels would get too dense. If this doesn't happen for you, could you open a new question that includes a minimal example demonstrating the problem? – Jake Sep 5 '14 at 15:02
6

A pure pgfplots solution might be also efficient and faster to type

\begin{tikzpicture}
\begin{semilogxaxis}[grid,log basis x={2},xticklabel=\pgfmathparse{2^\tick}\pgfmathprintnumber{\pgfmathresult},enlarge x limits = false,width=0.8\textwidth, xlabel={Anzahl der Prozessoren}, ylabel={Speedup}, xmin=1, ymin=0, legend pos=north west]
  \addplot [black, solid,mark=none,very thick,domain=1:4096]{1/((1-0.5) + 0.5/x)};
  \addplot [black, dashed,mark=none,very thick,domain=1:4096]{1/((1-0.8) + 0.8/x)};
  \addplot [black, dotted,mark=none,very thick,domain=1:4096]{1/((1-0.9) + 0.9/x)};
  \addplot [black, loosely dotted, mark=none,very thick,domain=1:4096]{1/((1-0.95) + 0.95/x)};
  \legend{%
P = 50\%,
P = 80\%,
P = 90\%,
P = 95\%,
}
\end{semilogxaxis}
\end{tikzpicture}

Note that the important plotting parameter to avoid log(0) is domain=min:max and NOT xmin,xmax.

enter image description here

|improve this answer|||||
4

I actually get a Dimension too large error when I try to run this code. I suspect your problem is that you have a p/x term; as x->0 this term will become difficult to compute. Change the x limits in your gnuplots to [1:4096]. This works for me:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{figure}[htpb]
\begin{center}
\begin{tikzpicture}
\begin{axis}[width=0.8\textwidth, xlabel={Anzahl der Prozessoren}, ylabel={Speedup}, xmin=1, xmax=4096, ymin=0, ymax=30, xmode = log, log basis x={2},xticklabel=\pgfmathparse{2^\tick}\pgfmathprintnumber{\pgfmathresult}]
\addplot gnuplot[black, solid, raw gnuplot,id=bal1,mark=none,very thick]{
set xrange [1:4096];
set yrange [0:30];
p = 0.5;
f(x)=1/((1-p) + p/x);
plot f(x)};
\addplot gnuplot[black, dashed, raw gnuplot,id=bal2,mark=none,very thick]{
set xrange [1:4096];
set yrange [0:30];
p = 0.8;
f(x)=1/((1-p) + p/x);
plot f(x)};
\addplot gnuplot[black, dotted, raw gnuplot,id=bal3,mark=none,very thick]{
set xrange [1:4096];
set yrange [0:30];
p = 0.9;
f(x)=1/((1-p) + p/x);
plot f(x)}; 
\addplot gnuplot[black, loosely dotted, raw gnuplot,id=bal4,mark=none,very thick]{
set xrange [1:4096];
set yrange [0:30];
p = 0.95;
f(x)=1/((1-p) + p/x);
plot f(x)};     
\legend{%
P = 50\%,
P = 80\%,
P = 90\%,
P = 95\%,
}           
\end{axis}
\end{tikzpicture}
\end{center}
% \caption{Speedup nach Amdahla'sches Gesetz}
% \label[figure]{fig:amdahls_law_speedup}
\end{figure}
\end{document}

enter image description here

|improve this answer|||||

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.