4

I am trying to draw a logarithmically scaled timeline with TikZ. I tried starting from a snippet from chronlogy.sty:

\begin{tikzpicture}[baseline={(current bounding box.north)}]%
   \draw [|->] (0,0) -- (\thedeltayears*\unit+\unit, 0);%
   \foreach \x in {1,...,\thedeltayears}%
    \draw[xshift=\x*\unit] (0,-.1\unit) -- (0,.1\unit);%
  \addtocounter{deltayears}{1}%
  \foreach \x in {\thestepstart,\thestep,...,\thestepstop}{%
    \pgfmathsetlength\xstop{(\x-\theyearstart)*\unit}%
    \draw[xshift=\xstop] (0,-.3\unit) -- (0,.3\unit);%
    \node at (\xstop,0) [below=.2\unit] {\x};}}{%
\end{tikzpicture}%

The horizontal axis should look like the x-axis of a logarithmic plot with main ticks labeled ...,10^-2,10^-1,10^0,10^1,... and minor ticks without lables at a position corresponding to log10 of 2*10^-2, 3*10^-2, ... etc. Since the lower and upper limits should be customizable I need to implement an appropriate scaling function in TikZ. However, I already fail to alter the simple computation xshift=\x*\unit to something like xshift=log10(pow(10,\x))*\unit. How can I realize non-elementary computations in this place? Note that I consciously avoid the builtin \begin{axis}, because eventually I want to add labels and a second axis with different units etc. I also tried using \pgfmathparse and \pgfmathresult with equal success.

4

One can put the labels he wants.

enter image description here

\documentclass[tikz,margin=5pt]{standalone}
\usepackage{siunitx}
\usetikzlibrary{}

\newcommand{\Exp}[1]{%
    \ifnum#1=0
        1%
    \else
        \num{e#1}%
    \fi
    }

\begin{document}
\begin{tikzpicture}

\draw (0,0)--(9,0) ;

\xdef\L{1}
\foreach \n [count=\m from -2] in {0,...,8} {%
    \draw[thick] (\n,4pt)
        node[anchor=south,text depth=.5em]
        {\scriptsize\Exp{\m}}
        -- (\n,-4pt) 
        node[anchor=north,text height=.9em]
        {\scriptsize\pgfmathprintnumber\L}  ;
    \foreach \i [evaluate={\x=log10(\i)}] in {1,2,...,10}
        {\draw (\n+\x,-2pt) -- (\n+\x,+2pt) ; }
    \xdef\L{\L 0}   
}
    \draw[thick] (9,4pt)
        node[anchor=south,text depth=.5em]
        {\scriptsize\num{E7}}
        -- (9,-4pt) 
        node[anchor=north,text height=1em]
        {\scriptsize\pgfmathprintnumber\L}  ;
\end{tikzpicture}


\end{document}
  • Your example works, but any minor modification (in the computations) I try fails. It seems I am affected by this bug tex.stackexchange.com/questions/33082/… . – highsciguy May 12 '14 at 20:32
  • @highsciguy Could you give me an example, then I try to adapt my code. – Tarass May 13 '14 at 17:23
  • Since this looks pretty advanced now, I decided to use it as a basis instead of the chronology package. I would like to implement the \unit which is used by chronology (it is passed as an argument to the environment defined by this package and necessary to be able to scale the graph to the desired size). When I add \newlength{\unit}\setlength{\unit}{1pt}, I can e.g. change (\n,4pt) to (\n,4*\unit) and it works. But changing {\draw (\n+\x,-2pt) -- (\n+\x,+2pt) ; } in the same way (I.e. multiplying all length by \unit) fails. Why is that? – highsciguy May 16 '14 at 13:55
  • Try \unit*2 and or -\unit*4. – Tarass May 16 '14 at 14:09
3

You can teach PGFPlots to just draw a single axis (instead of a box) without any plots:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    hide y axis,
    xmode=log,
    xmin=1e-2,
    xmax=1e2,
    ymin=0, ymax=1,
    y=1cm,
    xtick align=center,
    axis x line*=bottom,
    enlarge x limits={abs=\pgflinewidth, upper}
]
\end{axis}
\end{tikzpicture}
\end{document}

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.