3

I have to a plot like this with Tikz and pgfplot:

enter image description here

I have never used pgfplot and I am just learning how to use it. So far I have done only the axis:

\documentclass{article}
\usepackage{tikz,pgfplots}
\pgfplotsset{%
compat=newest, %footnotesize
tick label style={font=\footnotesize},
label style={font=\small},
legend style={font=\small},
axis x line = center,
axis y line = center,
every axis/.style={pin distance=1ex},
trim axis left   
}   %%%% fin pgfplotsset


 \begin{document}

  \begin{tikzpicture}
  \begin{axis}[%
   axis x line=center,
   axis y line=none,
   xmin=-3,xmax=3,
   xticklabels={$s_{k-4}$, $s_{k-3}$, $s_{k-2}$, $s_{k-1}$, $s_k$, $s_{k+1}$}
   ]
  \end{axis}

  \end{tikzpicture} 
 \end{document}

At this point, how to draw the step u? I would be glad if someone can help understanding how to do this. Thanks!

  • Please don't edit your question significantly) when there is an answer and then ask another question. Please ask a Follow-up question for that. I will revert the question to its old state now ... – Stefan Pinnow Mar 10 '17 at 17:28
  • Sorry for that! But I haven't seen the answer when I edited my question, that's why I made some updates. – Betelgeuse Mar 10 '17 at 17:42
3

There are many ways to achieve what you want. Here I present one of these.

For more details please have a look at the comments in the code.

% used PGFPlots v1.14
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \pgfplotsset{
        % use this `compat' level or higher to use the "advanced" placing for the
        % axis labels
        compat=1.3,
    }
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        % we want to reduce the `height' of the graph without rescaling the `widht'
        width=\axisdefaultwidth,
        height=3cm,
        % (I use `bottom' to draw the line from 0 to 1, so if needed by using
        %  the value `center' I can easily put it *on* the drawn line if needed)
        axis x line=bottom,
        axis y line=none,
        % -----
%        % add (pure) "z" label (without the arrow)
%        xlabel=$z$,
        % -----
        % add the arrow with the "z" label
        % (I do it in this way, because it would also work when `clip=true'
        %  would be used. Using the `xticklabel cs' (coordinate system) has
        %  the advantage that it respects the size of the xticklabels and thus
        %  would also work, if you would change the font size to e.g. `\Huge'. The given "y" coordinate is used to add an additional yshift (in this case).)
        after end axis/.code={
            \draw [->] (xticklabel cs:0.25,1ex)
                -- node [below] {$z$}
                    (xticklabel cs:0.75,1ex)
            ;
        },
        % -----
        % (adjusted a bit)
        xmin=-2.5,
        xmax=2.5,
        % change this value to state the "offset" of the x axis to the plot
        ymin=-0.5,
        % to sync the ticks with the labels ...
        xtick={-2,...,2},
        xticklabels={$s_{k-2}$, $s_{k-1}$, $s_k$, $s_{k+1}$, $s_{k+2}$},
        % we don't want to draw markers
        no markers,
        % turn of clipping so the nodes above the plot aren't clipped away
        clip=false,
    ]
        % one of many possible ways to draw the line in question is to just
        % provide some coordinates
        \addplot coordinates { (-2.5,0) (0.5,0) (0.5,1) (2.5,1) }
                % provide options used for all following nodes
                [above]
            % add nodes to the plot at specified `pos'itions
            node [pos=0.25] {$U_{k-3}$}
            node [pos=0.80] {$U_{k-2}$}
        ;
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

  • Thanks a lot! very nice explaination! I think I can draw also the arrow tau below the axis in the same way of u, right? – Betelgeuse Mar 10 '17 at 17:15
  • 1
    You could, but there is a "better" (not necessarily easier) way to do that. See the edit of my answer – Stefan Pinnow Mar 10 '17 at 17:45
1

Well, I see you are in the process of adapting to pgfplot. I'd be glad to help you. I'll post source code as well as the result of my compiling (I'm using TeXLive). I suggest you to use plain tikz before pgfplot instead, as it is more beginner-friendly and doesn't involve a plethora of long functions. Because of this, I'll be using plain tikz. Of course, If you insist on pgfplots solution, I'll post that as well, just let me know.

Anyway, here's the tikz code:

\documentclass{article}

\usepackage{amsmath}
\usepackage{tikz}

\begin{document}

\begin{center}
\begin{tikzpicture}
\draw (0,0) -- (10,0);
\node at (-0.3,0) {$\cdots$};
\node at (10.3,0) {$\cdots$};

\node at (2,0) {$|$};
\node at (4,0) {$|$};
\node at (6,0) {$|$};
\node at (8,0) {$|$};
\node at (10,0) {$|$};
\node at (2,-2) {$|$};

\node at (2,-0.5) {$s_{k-2}$};
\node at (4,-0.5) {$s_{k-1}$};
\node at (6,-0.5) {$s_{k}$};
\node at (8,-0.5) {$s_{k+1}$};

\node at (2,-2) {$\bullet$};
\draw[->] (2,-2) to node [below] {$z$}(7.2,-2);

\draw node at (-0.3,1) {$\cdots$};
\draw (0,1) to node [above] {$u_{k-3}$}(7.2,1);

\draw[dashed] (7.2,-2.5) to (7.2,1);
\draw (7.2,1) -- (7.2,2.5);
\draw(7.2,2.5) to node [above] {$u_{k-2}$}(10.5,2.5);
\node at (10.8,2.5) {$\cdots$};
\end{tikzpicture}
\end{center}

\end{document}

enter image description here

I know it's a little primitive, especially the | spam part (if I used the right commands I would get the same result with only one line of code), but I can't avoid such examples, as I want to show you simplicity sometimes is the right decision, especially for newcomers. Please don't feel offended by the fact that I posted tikz and not pgfplot solution. From experience I sensed my decision of presenting a less complex solution would be of benefit to you.

Have a good day.

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.