# Graph and/or Chart

Any advice on how to draw this generic graph? I said "generic" because is not based on any data, so apparently there are no references nor coordinates.

This is as far as I can go:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{patterns,hobby}
\usepackage{pgfplots}
\pgfplotsset{compat=1.6}
\begin{document}
\begin{tikzpicture}

\begin{axis}[
xmin=-0,xmax=7,
xlabel={z},
ymin=0,ymax=4.5,
xtick={2,4.5,6},
xticklabels={$t$,$t+dt$,$n$},
ytick={3.5},
yticklabels={$V(x)$},
xlabel={},
ylabel={},
axis lines=middle]
\draw [color=black,fill=white,thick,solid] (axis cs:0,0) rectangle (axis cs:6,3.5);
\draw [color=black,fill=white,thick,solid] (axis cs:0,0) rectangle (axis cs:2,3.5);
%
\node  at (axis cs:2.5,0)    [anchor=south west] {$$}; \node at (axis cs:4,0) [anchor=south west] {$$};
\node  at (axis cs:2.8,2.2)  [anchor=north]      {$$}; \node at (axis cs:4.2,4) [anchor=north] {$$};
\node  at (axis cs:0.25,2.5) [anchor=north]      {$$}; \node at (axis cs:0.25,4) [anchor=north] {$$};
\node at (axis cs:0,0)     [anchor=south west]  {};
%
\end{axis}
\end{tikzpicture}
\end{document}


• It is very straightforward, you could start by defining all relevant coordinates on the drawing and then draw some lines and a few arrows. – daleif May 3 '16 at 16:44
• here's starting point perhaps: tex.stackexchange.com/questions/160237/… – Thruston May 3 '16 at 17:20
• compat=1.6, really? The latest version of pgfplots is 1.13. – Torbjørn T. May 3 '16 at 19:06

An alternative, pure TikZ solution:

\documentclass[tikz,border=3mm,preview]{standalone}
\usetikzlibrary{arrows.meta,positioning,quotes}

\begin{document}
\begin{tikzpicture}[
>={Triangle[]}
]
% coordinate
\draw[->] (-0.1,0) node[below left] {0}
-- (9,0);
\draw[->] (0,-0.1) -- (0,6);
% horizontal line
\draw   (-0.1,4) node[left] {$V(x,0)$} -- + (9,0);
% vertical lines
\draw   (2,4) node[above left] {$V(x,t)$}
node[below left] {$P(x,t)\mathrm{d}t$}
-- + (0,-4.1)
node[below] {$t$};
\draw   (5,4) node[above right] {$V(x,t+\mathrm{d}t)$}
-- + (0,-4.1)
node[below] {$t+\mathrm{d}t)$};
\draw   (8,4) node[above] {$V(x,n)$}
-- + (0,-4.1)
node[below] {$n$};
% vectors
\path[draw=red, fill=red, ultra thick, ->]
(2,4) edge["$\displaystyle 1-\sum_{j=1}^{k}\mu_{x+t}^{(j)}\mathrm{d}t$"]   + (3, 0)
(2,4) to [pos=0.7,sloped,"$\displaystyle \mu_{x+t}^{(j)}\mathrm{d}t$" ']                 + (3,-2);
\end{tikzpicture}
\end{document}


• Great. It's the way I need. The detail of the sloped formula is superb. Thanks a lot!! – José May 4 '16 at 6:38

Another way with TikZ. I like to separate the labels from the drawings.

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usepackage{amsmath}
\begin{document}
\begin{tikzpicture}[axis/.style={thick,-stealth},
vector/.style={ultra thick,-latex}
]
% drawing
\draw [axis] (0,0) -- (13,0);
\draw [axis] (0,0) -- (0,6);
\draw[thick] (0,4)  -- (13,4);
\foreach \i in {1,2,3}{%
\draw[thick] (4*\i,0) -- (4*\i,4);
}
\draw [vector] (4,4) --  (8,4);
\draw [vector] (4,4) --  (8,4/3) coordinate[midway](mi) coordinate(e);
% the labels
\foreach \i/\l/\a in {  0/{V(x,0)}/left,
1/{V(x,t)}/left,
2/{V(x,t+dt)}/right,
3/{V(x,n)}/left}{%
\node[above \a] at (4*\i,4) {$\l$};
}
\foreach \i/\l in { 0/0,
1/t,
2/t+dt,
3/n}{%
\node[below] at ((4*\i,0) {$\l$};)
}
\node[above=8pt] at (6,4) {%
$\displaystyle 1-\sum_{j=1}^{k}\mu_{x+t}^{(j)}\,dt$};
\node[below left] at (mi) {$\mu_{x+t}^{(j)}\,dt$};
\node[right] at (e) {$S_{x+t}^{(j)}$};
\node[below left] at (4,4) {$P(x,t)\,dt$};
\end{tikzpicture}
\end{document}


This is the kind of thing where I wouldn't use pgfplots at all.

• If you want to change the width/height of the diagram, change x=8cm,y=4cm to your liking. The axis lines are drawn from (0,0) to (0,1)/(1,0).

• If you want to raise or lower the horizontal line, modify the value in \pgfmathsetmacro\Y{0.7} to some value between 0 and 1.

• If you want to move the three vertical lines, modify the values for \Xa, \Xb and \Xc. Again, use values between 0 and 1.

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usepackage{amsmath}
\begin{document}
\begin{tikzpicture}[
axis/.style={thick,-stealth},
vector/.style={ultra thick,-latex},
x=8cm,y=4cm,
every node/.style={font=\small}
]
\draw [axis] (0,0) node[below left] {$0$} -- (1,0);
\draw [axis] (0,0) -- (0,1);

\pgfmathsetmacro\Y{0.7}
\pgfmathsetmacro\Xa{0.3}
\pgfmathsetmacro\Xb{0.65}
\pgfmathsetmacro\Xc{0.9}

\draw (0,\Y) node[left] {$V(x,0)$} -- (1,\Y);

\foreach \x/\txt/\anchor in {\Xa/t/330,\Xb/t+dt/210,\Xc/n/230}
\draw (\x,\Y) node[above,anchor=\anchor]{$V(x,\txt)$} -- (\x,0) node[below] {$\txt$};

\draw [vector] (\Xa,\Y)
-- node[above]{$\displaystyle 1-\sum_{j=1}^{k}\mu_{x+t}^{(j)}\,dt$}
(\Xb,\Y);
\draw [vector] (\Xa,\Y) node[below left] {$P(x,t)\,dt$}
-- node[below left] {$\mu_{x+t}^{(j)}\,dt$}
(\Xb,\Y/3) node[right] {$S_{x+t}^{(j)}$};

\end{tikzpicture}
\end{document}

• It worked perfectly! Interesting the point to change the width and height of the diagram. Thanks!!! – José May 4 '16 at 6:41

A PSTricks solution:

\documentclass{article}

\usepackage{mathtools}

\newcommand*\diff[1]{\mathop{}\!\mathrm{d}#1}
\def\arrow[#1](#2)(#3)#4{%
\psline[linewidth = 2.5\pslinewidth]{->}(#2)(#3)
\pcline[linestyle = none, offset = #1](#2)(#3)
\ncput{$\displaystyle #4$}}

\begin{document}

\begin{pspicture}(-1.23,-0.4)(10,6.5)
\pnodes(0,0){A}(2.5,0){B}(7,0){C}(9,0){D}(7,2){E}(0,5){F}(2.5,5){G}(7,5){H}(9,5){I}(10,5){J}
\psline{<->}(0,6.5)(A)(10,0)
\uput[225](A){$0$}
\psline(F)(J)
\uput[180](F){$V(x,0)$}
\psline(B)(G)
\uput[270](B){$t$}
\psline(C)(H)
\uput[270](C){$t+\diff{t}$}
\psline(D)(I)
\uput[270](D){$n$}
\arrow[19pt](G)(H){1-\sum_{j = 1}^{k}\mu_{x+t}^{(j)}\diff{t}}
\arrow[-15pt](G)(E){\mu_{x+t}^{(j)}\diff{t}}
\uput[90](G){$V(x,t)$}
\uput[225](G){$P(x,t)\diff{t}$}
\uput[0](E){$S_{x+t}^{(j)}$}
\uput[90](H){$V(x,t+\diff{t})$}
\uput[90](I){$V(x,n)$}
\end{pspicture}

\end{document}


In case you want the formulae to be tilted to follow the directions of the arrows instead of always being horizontal, you can add the option nrot = :U to \ncput:

\documentclass{article}

\usepackage{mathtools}

\newcommand*\diff[1]{\mathop{}\!\mathrm{d}#1}
\def\arrow[#1](#2)(#3)#4{%
\psline[linewidth = 2.5\pslinewidth]{->}(#2)(#3)
\pcline[linestyle = none, offset = #1](#2)(#3)
\ncput[nrot = :U]{$\displaystyle #4$}}

\begin{document}

\begin{pspicture}(-1.23,-0.4)(10,6.5)
\pnodes(0,0){A}(2.5,0){B}(7,0){C}(9,0){D}(7,2){E}(0,5){F}(2.5,5){G}(7,5){H}(9,5){I}(10,5){J}
\psline{<->}(0,6.5)(A)(10,0)
\uput[225](A){$0$}
\psline(F)(J)
\uput[180](F){$V(x,0)$}
\psline(B)(G)
\uput[270](B){$t$}
\psline(C)(H)
\uput[270](C){$t+\diff{t}$}
\psline(D)(I)
\uput[270](D){$n$}
\arrow[19pt](G)(H){1-\sum_{j = 1}^{k}\mu_{x+t}^{(j)}\diff{t}}
\arrow[-12pt](G)(E){\mu_{x+t}^{(j)}\diff{t}}
\uput[90](G){$V(x,t)$}
\uput[225](G){$P(x,t)\diff{t}$}
\uput[0](E){$S_{x+t}^{(j)}$}
\uput[90](H){$V(x,t+\diff{t})$}
\uput[90](I){$V(x,n)$}
\end{pspicture}

\end{document}


• Sometimes I got a problem with the PStrick package when I run/compile together with .cls or .sty. As I am writing my PhD dissertation I have to run several files and not always works as I expected. I will try this time with this code. Thank you very much. – José May 4 '16 at 6:46
• Sorry but how can I do that? :( – José May 5 '16 at 15:14
• – Svend Tveskæg May 6 '16 at 4:46