1

I'm trying to plot the time complexity of some sorting algorithms using a log scale. I'm fetching the data from a text file insertion.dat which contains the following information:

x y
0 0
10 0.002
100 0.019
1000 1.636
5000 42.735
10000 167.950

and I'm using the following code to try implement it:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=15cm,
height=7cm,
axis x line=center,
axis y line=center,
ytick={0,10,100,1000},
xtick={0,10, 1000, 5000,10000},
ymode=log, xmode=log,
xlabel={\texttt{L}},
ylabel={Time (ms)},
xlabel style={right},
ylabel style={above},
xmin=0,
xmax=15000,
ymin=0,
ymax=2000]
\addplot[smooth,red,thick] table[y expr=\thisrow{y}/0.95] {insertion.dat};      
\end{axis}         
\end{tikzpicture}   
\end{document}

But the result I get is:

enter image description here

What's up with the axes? How can I fix this?

  • 1
    0,0 is a bit outside the range of a logscale plot... ;-) – Rmano Feb 4 '16 at 19:32
  • @Rmano Oh yeah, of course! But when I set xmin and ymin equal to 10 and remove (0,0) I still get the space between the axes. – Luke Collins Feb 4 '16 at 19:34
  • The axis [xy] line probably get confused by the log scale. Why don't you use \begin{logaxis} to start with? – Rmano Feb 4 '16 at 19:37
  • I played around with it and I managed to smarten it up. All I need to do is to position the x-axis more to the left so that it touches the y-axis line. Do you know how I can achieve this? – Luke Collins Feb 4 '16 at 19:40
2

The main mistake is that you use axis y line=center which implies that the axis should go through 0, or in the case of a log axis through 1. When you change this to axis y line=left and at least the xmin value, you should get the desired result. But then the ylabel is positioned left in the middle (again). This can be modified using the ylabel style key.

\begin{filecontents}{insertion.dat}
    x y
    10 0.002
    100 0.019
    1000 1.636
    5000 42.735
    10000 167.950
\end{filecontents}
\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
    \pgfplotsset{compat=1.13}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            xmode=log,
            ymode=log,
            %
            width=15cm,
            height=7cm,
            axis x line=center,
            axis y line=left,   % <-- `center' confuses PGFPlots on a log axis
            xmin=5,
            xmax=15000,
            ymin=1e-3,
            ymax=2000,
            xlabel={\texttt{L}},
            ylabel={Time (ms)},
            xlabel style={right},
            ylabel style={
                at={(rel axis cs:0,1)},
                rotate=-90,
                anchor=south,
            },
        ]
            \addplot [smooth,red,thick,mark=*]
                table [y expr=\thisrow{y}/0.95] {insertion.dat};
        \end{axis}
    \end{tikzpicture}
\end{document}

image showing the result of above code

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.