# TikZ/pgf piecewise function

Could anybody help to build charts like in the picture?

With the invaluable help of another user and his code, I reached the following:

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

\begin{document}

\begin{tikzpicture}[
>={Triangle[]}
]
% axes
\draw[->] (-0.1,0) -- (3.5,0.0) node[right] {$t$};
\draw[->] (0,-0.1) -- (0,3.5) node[above] {$_{t}p_{x}^{'^{(3)}}$};
\node[below left] at (0,0) {0};
% curve
\path[draw=red, ultra thick]
(0,2) node[left] {$1$} -- ++ (1,0.0) -- + (0.0,0.0)
(1,1.69)  -- ++ (1,0) -- + (0,0)
coordinate[midway,pin=above:$1-\frac{1}{2}q'^{(3)}_x$] (a)
(2,1.4)  -- ++ (0.5,0) -- + (0,0)
coordinate[midway,pin=right:$1-q'^{(3)}_x$] (b);
% dashed line
\draw[densely dashed]
(0,1.7) -| (1,0) node[below] {$1/2$}
(2,1.4) |- (1,0) %node[below] {$1$};
\end{tikzpicture}

\begin{tikzpicture}[>={Triangle[]}
]
% axes
\draw[->] (-0.1,0) -- (3,0.0) node[below] {$t$};
\draw[dashed] (0.75,0) -- (0.75,1.5) node[below] {$1/2$};
\draw[dashed] (1.55,0) -| (1.55,0.93) node[below] {$1$};
\draw[->] (0,-0.1) -- (0,3.5) node[above] {$_{t}p_{x}^{'^{(\gamma)}}$};
\node[below left] at (0,0) {0};
% curves
\path[draw=red, ultra thick]
(0,1.9) node[left] {$1$}
.. controls + (2mm,-1mm) and + (1mm,-0.5mm) .. (0.7,1.65)
coordinate[midway,pin=above right:$p'^{(1)}_xp'^{(2)}_x$ (1)] (a)
(0.75,1.5) .. controls + (2mm,-2mm) and + (2mm,-1mm) .. (1.5,1.15)
coordinate[midway,pin=right:$p'^{(1)}_xp'^{(2)}_x\Big(1-\frac{1} {2}q'^{(3)}_{x}\Big)$] (a)
(1.65,0.95) .. controls + (-2mm,1mm) and + (-3mm,0.5mm) .. (1.9,0.9)
coordinate[midway,pin=below right:$p'^{(1)}_xp'^{(2)}_x(1-q'^{(3)}_{x})$](b);
% dashed line
%\draw[densely dashed]
%(0.75,0) -- (0.75,1.5) node[below] {$1/2$}
%(1.55,0) -| (1.55,0.93) %node[below] {$1$};
% brace
%\draw[decorate,decoration={brace,mirror,raise=0.5mm}]
%(0,2) -- coordinate[left=2mm,
%pin=below left:$q^{(\gamma)}_x$] (a) (0,1);
\end{tikzpicture}

\end{document}


However two problems arised:

1. The x-axis, as I am not able to place the coordinates properly because in one of them I am able to place it where needed but the dashed line has to be only vertical, whereas in the other despite of having the vertical dashed line, the coordinate is not possible to place it in the x-axis properly.

2. In the original charts can be seen a dot at the beginning of each piecewise function which I am not able to reproduce it.

3. Finally in the curved chart, I am having problems to make them exactly and proportionally curved one with the following as can be seen with my code. Also the position of the equations cannot be placed but above, above-right, above-left, right, left or below, without playing with them around (in any case this is a minor issue not very important)

Hope it is clear what I am trying to get.

• Sorry, but I cannot understand the points 1 (do you simply want to remove the horizontal dashed line?), and 3 (do you want a simpler way to add the equations?), can you clarify them? – Alenanno Aug 30 '16 at 20:25
• Sorry for the explanation, I'll try to be more concise: point 1) means to remove the horizontal dashed line (in chart 1) and place the coordinates (1/2 and 1) in the x-axis, not below the curve (in chart 2), the problem is that . Point 2) To place a dot at the beginning of each piecewise function. Point 3) to make the curves more accurate to the original picture, with my code I got different curvature and some pikes which I am not able to remove. For the equations, I am just looking for a way to place anywhere in the chart. Thanks in advance!!! – José Aug 30 '16 at 20:43
• Well, I got something like this image. The dot on the last curve doesn't appear at the beginning because you have some negative x values when creating the path, so your line goes back. – Alenanno Aug 30 '16 at 20:50
• Could be fine. What code did you use? Thanks – José Aug 30 '16 at 22:04

As mentioned in the comments, the last dot doesn't appear at the beginning of the small curve because when building the path, you use negative x coordinates, so in regards to where the path starts, the line goes left (as in back), so that the dot appears to be ill-positioned.

Fixing the path should solve the issue. I would have done it myself, but I'm not sure how you want it to appear. Nonetheless, the current solution should fix your other issues, while also bringing some improvements to the code.

## Code

\documentclass[tikz,margin=10pt]{standalone}
\usepackage{amsmath}

\usetikzlibrary{arrows.meta,decorations.pathreplacing,positioning,quotes}

\tikzset{
every label/.style={inner sep=0, font=\footnotesize},
every pin/.style={inner sep=0, font=\footnotesize},
every node/.style={font=\footnotesize},
dot/.style={circle, fill=red, inner sep=1pt, label={
[fill=white]left:#1}
},
}

\begin{document}
\begin{tikzpicture}[>={Triangle[]}]
% axes
\draw[->] (-0.1,0) -- (3.5,0.0) node[right] {$t$};
\draw[->] (0,-0.1) -- (0,3.5) node[above] {$_{t}p_{x}^{'^{(3)}}$};
\node[below left] at (0,0) {0};

% curve
\draw[draw=red, ultra thick]
(0,2) node[dot=$1$] {} -- ++ (1,0)
(1,1.69) -- ++ (1,0)
coordinate[dot, pos=0] (dot1)
coordinate[midway,pin={80:$1-\frac{1}{2}q'^{(3)}_x$}] (a)
(2,1.4)  -- ++ (0.5,0)
coordinate[dot, pos=0] (dot2)
coordinate[midway,pin={45:$1-q'^{(3)}_x$}] (b);

% dashed line
\draw[densely dashed]
(dot1) -- (1,0) node[below] {$1/2$}
(dot2) -- (2,0); %node[below] {$1$};
\begin{scope}[yshift=-5cm]
% axes
\draw[->] (-0.1,0) -- (6,0) node[below] {$t$};
\draw[->] (0,-0.1) -- (0,3.5) node[above] {$_{t}p_{x}^{'^{(\gamma)}}$};
\node[below left] at (0,0) {0};

% curves
\path[draw=red, ultra thick]
(0,1.9) .. controls + (2mm,-1mm) and + (1mm,-0.5mm) .. (0.7,1.65)
coordinate[pos=0, dot=$1$]
coordinate[midway,pin=86:$p'^{(1)}_xp'^{(2)}_x$ (1)] (a)
(0.75,1.5) .. controls + (2mm,-2mm) and + (2mm,-1mm) .. (1.5,1.15)
coordinate[pos=0, dot] (dot3)
coordinate[midway,pin=35:$p'^{(1)}_xp'^{(2)}_x\Big(1-\frac{1}{2}q'^{(3)}_{x}\Big)$] (a)
(1.65,0.95) .. controls + (-2mm,1mm) and + (-3mm,0.5mm) .. (1.9,0.9)
coordinate[pos=0, dot] (dot4)
coordinate[midway,pin=0:$p'^{(1)}_xp'^{(2)}_x(1-q'^{(3)}_{x})$](b);

% dashed line
\draw[dashed]
(dot3) -- (0.75,0) node[below] {$1/2$}
(dot4) -- (1.65,0) node[below] {$1$};
\end{scope}
\end{tikzpicture}
\end{document}