经济学/金融中的现金流量图

经济学/金融中的现金流量图

以下类型的图表(下面的 MWE)描述了 10 个日期内收到和支付的流量。

在此处输入图片描述

有问题

在经济学/金融学中,我们经常需要“现金流量图” 现金流量图描述在一段时间内收到什么和支付什么,支付的内容是确定的、随机的还是有条件的,以及支付的频率。

我需要一个更好的解决方案(链?)

我有一个有效的解决方案,直到......我需要更多的灵活性。正如您在 MWE 中看到的,我对每个日期以及每个节点的固定箭头和浮动箭头进行了费力的定义。已经很无聊了 :) 此外,我硬编码了一个事实,即如果向上的箭头是固定的,则向下的箭头是浮动的,反之亦然。而今天我想选择向上/向下的箭头是浮动的还是固定的。

食谱

所有这些图表都需要

  • 日期
  • 时间线上下的流动与时间线下的流动
  • 从这些日期向上和向下的箭头
  • 上升流和下降流的频率
  • 描述箭头的样式(流动类型)

在该领域通常

  • “固定”付款是固定的直箭头。收到时向上,付款时向下。
  • “浮动”付款(可变,基于rand)付款可以使用蛇形箭头。同样,当它们收到时,它们在下方支付。
  • 我们可以用虚线箭头定义其他的东西,比如流程的条件性。

我必须承认,大多数时候,通用工具提供的视觉效果很差,并且显然缺乏 TikZ 提供吸引人的自解释图表的潜力。

在此处输入图片描述在此处输入图片描述

和以前的帖子(绘制现金流和这里 绘制现金流量图)并没有回答我现在的问题。

平均能量损失

\documentclass{standalone}
%----------------Tikz libraries -------------------------
\usetikzlibrary{
    arrows,
    arrows.meta,
    decorations,
    decorations.shapes,
    decorations.pathmorphing,
    decorations.text,
}

\def\M{10}      %Number od dates
\def\Couleur{blue}
\def\SigTF{1}   %+1 Arrow up, i receive the fix flow, -1 Arrow down, i pay the fix flow
\def\TF{2}      % amount of the flow in 
\def\SigTV{-1}  %%+1 Arrow up, i receive the variable flow, -1 Arrow down, i pay the variable flow
\def\TV{3}      % amount of the variable in 
\def\SpreadTV{1} % +1 Arrow up, on top of the floating arrow
\def\TSpread{3}     % sze of the arrow  
\def\SigUF{0}       % Payment on Start Date
\def\UF{0}          % Amount
\def\SigLastF{0}    % Payment on Last Date
\def\LastF{0}       % Amout


\begin{document}

\pgfmathsetseed{1}

\begin{tikzpicture}                     
    \tikzset{
    InitialPoint/.style={circle,draw=red!40,fill=red!40,minimum size=40},
    D0/.style={draw=red!40,fill=red!40,circle,minimum size=10},
    N_date/.style={circle,draw=blue!50,minimum size=20,draw=\Couleur,},
    N_TF/.style={circle,minimum size=20},
    N_TV/.style={circle,minimum size=0},
    fleche/.style={>=latex,very thick},
    flecheTV/.style={->,ultra thick, decorate,decoration={snake, amplitude=1mm,segment length=3mm,  pre length=3mm, post length=3mm}, color=\Couleur!50!white},
    flecheTF/.style={fleche, color=\Couleur!50!white},      
    flecheSpread/.style={>=latex,very thick,gray},
    flecheUF/.style={fleche,->,ultra thick,decorate,decoration={zigzag, amplitude=1mm,segment length=15mm,  pre length= 30mm, post length=30mm},\Couleur},
    }                           


%------------- Starting poinr ---------------
    \node [D0]  (D0) at (0,0) {Start};
%-------------    Upfront   payment   ---------------
    \ifnum\numexpr\UF=0\relax
    \else
        \node [N_TF]    (UF)    at (0,\SigUF*\UF)   {UpFront};
        \draw [flecheUF] (D0) -- (UF);
    \fi                             
%-------------    Last payment      ---------------
    \ifnum\numexpr\LastF=0\relax
    \else
        \node [N_TF]    (LastF)     at (1+1.5*\M,\SigLastF*\LastF)  {Reimbursment};
        \draw [flecheUF] (D\M) -- (LastF);
    \fi                             
%------------- Nodes for dates ---------------
    \foreach \x in {1,...,\M}{
        \pgfmathtruncatemacro{\prev}{\x - 1}  
        \node [N_date] (D\x) at (1+1.5*\x,0) {\scriptsize{$t_{\x}$}};
        \draw[opacity=0.5] (D\prev) -- (D\x);   
    }

%------------- Nodes for variables flows---------------
    \foreach \x in {1,...,\M}{          
        \pgfmathtruncatemacro{\prev}{\x - 1}  
        \pgfmathsetmacro{\TVnew}{\SigTV*\TV*rnd}%
        \ifnum\numexpr\TV>0\relax
            \node [N_TV] (TV\x) at (1+1.5*\x,\SigTV*1.5+\TVnew) {\normalsize {$TV_{\x}$}};
            \draw [flecheTV] (D\x) -- (TV\x);
        \fi
        \ifnum\numexpr\TV=0\relax
        \else
            \ifnum\numexpr\SpreadTV=0\relax
            \else
                \node [N_TV,gray] (spread\x) at (1+1.5*\x,\SigTV*1.5+\TVnew+\SigTV*2) 
                {\normalsize{$spread$}};
                %\SpreadTV) {\normalsize{$spread$}};
                \draw [->,ultra thick,gray!50] (TV\x)--(spread\x);
            \fi
        \fi         
    }

%------------- Fix payment flows ---------------
    \foreach \x in {2,4,...,\M}{
        \pgfmathtruncatemacro{\z}{0.5*\x}             
        \ifnum\numexpr\TF=0\relax
        \else
            \node[N_TF]     (TF\x)  at (1+1.5*\x,\SigTF*\TF) {\normalsize{$TF_{\z}$}};
            \draw [->,very thick, blue!80] (D\x) -- (TF\x);
        \fi
    }               



\end{tikzpicture}                                                                               

\end{document}  

编辑 按照@BambOo 真正灵活而高效的答案,标签上的一些参数,\newif显示或不展开,更新的表格(展开列尚未发挥作用)处理单个最终流(无展开)。

在此处输入图片描述

\documentclass[tikz,margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usetikzlibrary{decorations.pathmorphing,decorations.markings,arrows.meta}
\usepgfplotslibrary{dateplot}
\pgfmathsetseed{2}

\newif\ifShowSpread     % Display Spread or not
\ShowSpreadtrue         % Display or not the gray arrow
\def\Couleur{orange}    % Parameter the color
\def\FixLabel{C}        % Label for fixed
\def\FloatingLabel{V}   % Label for Floating
\def\SpreadLabel{$+50$bps} % Label for Spread


\tikzset{
    fixedarrow/.style={
    -stealth, color=\Couleur,  thick, solid,
  },
  floatingarrow/.style={
    -stealth, color=\Couleur!50, thick, solid, decorate,
    decoration={snake, amplitude=1mm, segment length=3mm, pre length=1mm,
                post length=1mm},
  },
  Conditonarrow/.style={
    -stealth, color=\Couleur!50, ultra thick, densely dotted, decorate,
    decoration={snake, amplitude=1mm, segment length=3mm, pre length=1mm,
                post length=1mm},
  },
}

\pgfplotsset{
    fixed/.style={
        point meta=\thisrow{fixed},
        quiver={u=0, v=\thisrow{fixed},every arrow/.append style={fixedarrow},
        after arrow/.code={
            \pgfplotspointgetcoordinates
            \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
            \pgfmathsetmacro{\opa}{ifthenelse(\pgfkeysvalueof{/data point/meta}==0,0,1)}
            \pgfmathtruncatemacro{\newindex}{\coordindex+1}
            \node[anchor=south,opacity=\opa] at (axis cs:\pgfkeysvalueof{/data point/x},\pgfkeysvalueof{/data point/meta}) {$\FixLabel_{\newindex}$};
            \pgfkeys{/pgf/fpu=false}
            }}},
    floating/.style={
        point meta=\thisrow{floating},
        quiver={u=0, 
                v=\thisrow{floating},
                every arrow/.append style={Conditonarrow}, %% sould not it be {floatingarrow}, ?
        after arrow/.code={
            \pgfplotspointgetcoordinates
            \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
            \pgfmathsetmacro{\opa}{ifthenelse(\pgfkeysvalueof{/data point/meta}==0,0,1)}
            \pgfmathtruncatemacro{\newindex}{\coordindex+1}
            \pgfkeys{/pgf/fpu=false}
            \node[anchor=north,opacity=\opa] (tempnode) at (axis cs:\pgfkeysvalueof{/data point/x},\pgfkeysvalueof{/data point/meta}) {$\FloatingLabel_{\newindex}$};

    \ifShowSpread            
\draw[gray,-stealth,thick] (tempnode) --++ (0pt,-20pt) node[at end,below,font=\itshape] {\SpreadLabel}; 
    \fi
            }}},
    fixedConditional/.style={fixed,densely dotted},
    floatingConditional/.style={floating,densely dotted},
}

%% for testing
%Table ReceiveFixPayFloating where I receive fix....
\begin{filecontents*}[overwrite]{ReceiveFixPayFloating.dat}
date        fixed    floating  spread
2019-12-28  5       0        0  %Fixed upfront received, no floating but no spread    
2020-02-01  7       -5      0.5
2020-03-01  0       -6      0.5
2020-04-01  5       -1      0.5
2020-05-01  0       -4      0.5
2020-06-01  7       -3      0.5
2020-07-01  0      -4.5     0.5
2020-08-01  6       -1      0.5
2020-09-01  0       -1.     0.5
2020-10-01  0       -5       0   % Floating final payment paid
\end{filecontents*}

%Table ReceiveFloatingPayFix where I receive floating....
\begin{filecontents*}[overwrite]{ReceiveFloatingPayFix.dat}
date        fixed    floating  spread
2019-12-28  -5       0       0  %Fixed upfront received, no floating but no spread    
2020-02-01  -7       5      0.5
2020-03-01  -0       6      0.5
2020-04-01  -5       1      0.5
2020-05-01  -0       4      0.5
2020-06-01  -7       3      0.5
2020-07-01  -0      4.5     0.5
2020-08-01  -6       1      0.5
2020-09-01  -0       1.     0.5
2020-10-01  -0       5       0   % Floating final payment paid
\end{filecontents*}


\begin{document}

\begin{tikzpicture}
    \begin{axis}[
            width=15cm,
            height=5cm,
            axis y line=none,
            axis x line*=center,
            date coordinates in=x,
            tick align=inside,
            xtick=data,
            xticklabel={$d_{\pgfmathprintnumber[/pgf/number format/fixed]{\month}}$},
            xticklabel style={anchor=center,outer sep=0pt, inner sep=0pt,circle,draw=\Couleur,fill=\Couleur!20,thick,text width=1.5em,align=center},
            axis on top=true,
            clip=false,
        ]

%% All good here
        \addplot[fixed] table [x=date,y expr=0]     {ReceiveFixPayFloating.dat};
        \addplot[floating] table [x=date,y expr=0]  {ReceiveFixPayFloating.dat};

%% Pb of above/below labels 
%       \addplot[fixed] table [x=date,y expr=0]     {ReceiveFloatingPayFix.dat};
%       \addplot[floating] table [x=date,y expr=0]  {ReceiveFloatingPayFix.dat};

%% I created 
%    fixedConditional/.style={fixed,densely dotted},
%    floatingConditional/.style={floating,densely dotted},
% but they don't seem to apply. 
%
    \addplot[fixedConditional] table [x=date,y expr=0]  {ReceiveFixPayFloating.dat};
    \addplot[floatingConditional] table [x=date,y expr=0]   {ReceiveFixPayFloating.dat};


%       \node[circle,fill=red!50,xshift=-2cm] at (2020-01-01,0) {Start};
    \end{axis}
\end{tikzpicture}
\end{document}

答案1

这是我的建议(我花了一整个下午才弄清楚),希望这会有所帮助。

基于frougon 对你之前的问题的建议为了使用quiver图表来获得装饰箭头,我添加了一些代码来显示额外的标签。

该代码以数据文件作为绘图的参数,因此它应该比纯 Ti 更容易使用上面的 Z 脚本。

肯定还有改进的空间,因为我自己在完成这项工作时遇到了很多困难。

\documentclass[tikz,margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usetikzlibrary{decorations.pathmorphing,decorations.markings,arrows.meta}
\usepgfplotslibrary{dateplot}

\tikzset{
    fixedarrow/.style={
    -stealth, color=blue,  thick, solid,
  },
  floatingarrow/.style={
    -stealth, color=blue!50, thick, solid, decorate,
    decoration={snake, amplitude=1mm, segment length=3mm, pre length=1mm,
                post length=1mm},
  },
}

\pgfplotsset{
    fixed/.style={
        point meta=\thisrow{fixed},
        quiver={u=0, v=\thisrow{fixed},every arrow/.append style={fixedarrow},
        after arrow/.code={
            \pgfplotspointgetcoordinates
            \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
            \pgfmathsetmacro{\opa}{ifthenelse(\pgfkeysvalueof{/data point/meta}==0,0,1)}
            \pgfmathtruncatemacro{\newindex}{\coordindex+1}
            \node[anchor=south,opacity=\opa] at (axis cs:\pgfkeysvalueof{/data point/x},\pgfkeysvalueof{/data point/meta}) {$TF_{\newindex}$};
            \pgfkeys{/pgf/fpu=false}
            }}},
    floating/.style={
        point meta=\thisrow{floating},
        quiver={u=0, v=\thisrow{floating},every arrow/.append style={floatingarrow},
        after arrow/.code={
            \pgfplotspointgetcoordinates
            \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
            \pgfmathsetmacro{\opa}{ifthenelse(\pgfkeysvalueof{/data point/meta}==0,0,1)}
            \pgfmathtruncatemacro{\newindex}{\coordindex+1}
            \pgfkeys{/pgf/fpu=false}
            \node[anchor=north,opacity=\opa] (tempnode) at (axis cs:\pgfkeysvalueof{/data point/x},\pgfkeysvalueof{/data point/meta}) {$TV_{\newindex}$};
            \draw[gray,-stealth,thick] (tempnode) --++ (0pt,-20pt) node[at end,below,font=\itshape] {spread};
            }}},
    conditional/.style={fixed,densely dashed}, %base on fixed style
}

\begin{filecontents*}[overwrite]{test.dat}
date        fixed   floating 
2020-01-01  0       -50 
2020-02-01  10      -5   
2020-03-01  0       -60  
2020-04-01  10      -10   
2020-05-01  0       -40     
2020-06-01  10      -30   
2020-07-01  0       -45   
2020-08-01  10      -10   
2020-09-01  0       -15     
2020-10-01  10      -40      
\end{filecontents*}

\begin{document}

\begin{tikzpicture}
    \begin{axis}[
            width=15cm,
            height=10cm,
            axis y line=none,
            axis x line*=center,
            date coordinates in=x,
            tick align=inside,
            xtick=data,
            xticklabel={$t_{\pgfmathprintnumber[/pgf/number format/fixed]{\month}}$},
            xticklabel style={anchor=center,outer sep=0pt, inner sep=0pt,circle,draw=blue,thick,fill=white,text width=1.5em,align=center},
            axis on top=true,
            clip=false,
        ]
        \addplot[fixed] table [x=date,y expr=0] {test.dat};
        \addplot[floating] table [x=date,y expr=0] {test.dat};
        \node[circle,fill=red!50,xshift=-2cm] at (2020-01-01,0) {Start};
    \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

更新

  1. north/south根据 的值对锚点进行修正point meta
  2. 正确应用箭头conditional样式
  3. 使用从列的最小值开始到比例因子结束的spread附加图来处理箭头,使得灰色箭头足够长以具有意义(否则这些箭头太短而无法读取)。quiverfixed/floating-spread*scale
\documentclass[tikz,margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usetikzlibrary{decorations.pathmorphing}
\usepgfplotslibrary{dateplot}
\pgfmathsetseed{2}

\newif\ifShowSpread     % Display Spread or not
\ShowSpreadtrue         % Display or not the gray arrow
\def\Couleur{orange}    % Parameter the color
\def\FixLabel{C}        % Label for fixed
\def\FloatingLabel{V}   % Label for Floating
\def\SpreadLabel{$+50$bps} % Label for Spread
\def\SpreadScale{10}


\tikzset{
    fixedarrow/.style={-stealth, color=\Couleur,  thick},
    floatingarrow/.style={-stealth, color=\Couleur!50, thick, decorate,decoration={snake, amplitude=1mm, segment length=3mm, pre length=1mm,post length=1mm}},
    spreadarrow/.style={-stealth, color=gray,  thick},
}

\pgfplotsset{
    fixed/.style={
        point meta={\thisrow{fixed}},
        quiver={u=0, v={\thisrow{fixed}},every arrow/.style={fixedarrow},
        after arrow/.code={
            \pgfplotspointgetcoordinates
            \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
            \pgfmathsetmacro{\opa}{ifthenelse(\pgfkeysvalueof{/data point/meta}==0,0,1)}
            \pgfmathsetmacro{\anc}{ifthenelse(\pgfkeysvalueof{/data point/meta}>=0,-90,90)}
            \pgfmathtruncatemacro{\newindex}{\coordindex+1}
            \pgfkeys{/pgf/fpu=false}
            \node[anchor=\anc,opacity=\opa] at (axis cs:\pgfkeysvalueof{/data point/x},\pgfkeysvalueof{/data point/meta}) {$\FixLabel_{\newindex}$};
            }}},
    floating/.style={
        point meta=\thisrow{floating},
        quiver={u=0,v=\thisrow{floating},every arrow/.style={floatingarrow},
        after arrow/.code={
            \pgfplotspointgetcoordinates
            \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
            \pgfmathsetmacro{\opa}{ifthenelse(\pgfkeysvalueof{/data point/meta}==0,0,1)}
            \pgfmathsetmacro{\anc}{ifthenelse(\pgfkeysvalueof{/data point/meta}>=0,-90,90)}
            \pgfmathtruncatemacro{\newindex}{\coordindex+1}
            \pgfkeys{/pgf/fpu=false}
            \node[anchor=\anc,opacity=\opa] (tempnode) at (axis cs:\pgfkeysvalueof{/data point/x},\pgfkeysvalueof{/data point/meta}) {$\FloatingLabel_{\newindex}$};
            }}},
        spread/.style={
        point meta={-\SpreadScale*\thisrow{spread}},
        quiver={u=0,v={-\SpreadScale*\thisrow{spread}},every arrow/.style={spreadarrow},
        after arrow/.code={
            \pgfplotspointgetcoordinates
            \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
            \pgfmathsetmacro{\opa}{ifthenelse(\pgfkeysvalueof{/data point/meta}==0,0,1)}
            \pgfmathsetmacro{\anc}{ifthenelse(\pgfkeysvalueof{/data point/meta}>=0,-90,90)}
            \pgfmathtruncatemacro{\newindex}{\coordindex+1}
            \pgfmathsetmacro{\spreadend}{\pgfkeysvalueof{/data point/y}+\pgfkeysvalueof{/data point/meta}}
            \pgfkeys{/pgf/fpu=false}
            \node[anchor=\anc,gray,opacity=\opa] (tempnode) at (axis cs:\pgfkeysvalueof{/data point/x},\spreadend) {\SpreadLabel};
            }}},
    fixedConditional/.style={fixed,densely dotted},
    floatingConditional/.style={floating,densely dotted},
    spreadConditional/.style={spread,densely dotted},
    customaxis/.style={
        width=15cm,
            height=5cm,
            axis y line=none,
            axis x line*=center,
            date coordinates in=x,
            tick align=inside,
            xtick=data,
            xticklabel={$d_{\pgfmathprintnumber[/pgf/number format/fixed]{\month}}$},
            xticklabel style={anchor=center,outer sep=0pt, inner sep=0pt,circle,draw=\Couleur,fill=\Couleur!20,thick,text width=1.5em,align=center},
            axis on top=true,
            clip=false,
    }
}

%% for testing
%Table ReceiveFixPayFloating where I receive fix....
\begin{filecontents*}[overwrite]{ReceiveFixPayFloating.dat}
date        fixed    floating  spread
2019-12-28  5       0       0  %Fixed upfront received, no floating but no spread    
2020-02-01  7       -5      0.5
2020-03-01  0       -6      0.5
2020-04-01  5       -1      0.5
2020-05-01  0       -4      0.5
2020-06-01  7       -3      0.5
2020-07-01  0      -4.5     0.5
2020-08-01  6       -1      0.5
2020-09-01  0       -1.     0.5
2020-10-01  0       -5      0   % Floating final payment paid
\end{filecontents*}

%Table ReceiveFloatingPayFix where I receive floating....
\begin{filecontents*}[overwrite]{ReceiveFloatingPayFix.dat}
date        fixed    floating  spread
2019-12-28  -5       0      0  %Fixed upfront received, no floating but no spread    
2020-02-01  -7       5      0.5
2020-03-01  -0       6      0.5
2020-04-01  -5       1      0.5
2020-05-01  -0       4      0.5
2020-06-01  -7       3      0.5
2020-07-01  -0      4.5     0.5
2020-08-01  -6       1      0.5
2020-09-01  -0       1.     0.5
2020-10-01  -0       5      0   % Floating final payment paid
\end{filecontents*}


\begin{document}
\begin{tikzpicture}
    \begin{axis}[customaxis]
        \addplot[fixed] table [x=date,y expr=0]     {ReceiveFixPayFloating.dat};
        \addplot[floating] table [x=date,y expr=0]  {ReceiveFixPayFloating.dat};
        \addplot[spread] table [x=date,y expr={min(\thisrow{floating},\thisrow{fixed})}]  {ReceiveFixPayFloating.dat};
    \end{axis}
\end{tikzpicture}
\begin{tikzpicture}
    \begin{axis}[customaxis]
        \addplot[fixed] table [x=date,y expr=0]     {ReceiveFloatingPayFix.dat};
        \addplot[floating] table [x=date,y expr=0]  {ReceiveFloatingPayFix.dat};
        \addplot[spread] table [x=date,y expr={min(\thisrow{floating},\thisrow{fixed})}]  {ReceiveFloatingPayFix.dat};
    \end{axis}
\end{tikzpicture}
\begin{tikzpicture}
    \begin{axis}[customaxis]
        \addplot[fixedConditional] table [x=date,y expr=0]  {ReceiveFixPayFloating.dat};
        \addplot[floatingConditional] table [x=date,y expr=0]   {ReceiveFixPayFloating.dat};
        \addplot[spreadConditional] table [x=date,y expr={min(\thisrow{floating},\thisrow{fixed})}]   {ReceiveFixPayFloating.dat};
    \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

在此处输入图片描述

在此处输入图片描述

更新 2最后样式调整

\documentclass[tikz,margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usetikzlibrary{decorations.pathmorphing}
\usepgfplotslibrary{dateplot}
\pgfmathsetseed{2}

\newif\ifShowSpread     % Display Spread or not
\ShowSpreadtrue         % Display or not the gray arrow
\def\Couleur{orange}    % Parameter the color
\def\FixLabel{C}        % Label for fixed
\def\FloatingLabel{V}   % Label for Floating
\def\SpreadLabel{$+50$bps} % Label for Spread
\def\SpreadScale{4}


\tikzset{
    fixedarrow/.style={-stealth, color=\Couleur,  thick},
    floatingarrow/.style={-stealth, color=\Couleur!50, thick, decorate,decoration={snake, amplitude=1mm, segment length=3mm, pre length=1mm,post length=1mm}},
    spreadarrow/.style={-stealth, color=gray,  thick},
    labelnode/.style={fill opacity=0.5,text opacity=1,draw opacity=1,fill=#1,draw=none,text=black,inner sep=1pt, outer sep=0pt,font=\small}
}

\pgfplotsset{
    fixed/.style={
        %y filter/.expression={\thisrow{fixed}==0 ? nan : y},
        point meta={\thisrow{fixed}},
        quiver={u=0, v={\thisrow{fixed}},every arrow/.style={fixedarrow},
        after arrow/.code={
            \pgfplotspointgetcoordinates
            \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
            \pgfmathtruncatemacro{\newindex}{\coordindex+1}
            \pgfmathsetmacro{\fixedend}{0.5*\pgfkeysvalueof{/data point/meta}}
            \pgfkeys{/pgf/fpu=false}
            \node[labelnode={\Couleur}] at (axis cs:\pgfkeysvalueof{/data point/x},\fixedend) {$\FixLabel_{\newindex}$};
            }
        }},
    floating/.style={
        y filter/.expression={\thisrow{floating}==0 ? nan : y},
        point meta=\thisrow{floating},
        quiver={u=0,v=\thisrow{floating},every arrow/.style={floatingarrow},
        after arrow/.code={
            \pgfplotspointgetcoordinates
            \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
            \pgfmathtruncatemacro{\newindex}{\coordindex+1}
            \pgfmathsetmacro{\floatingend}{0.5*\pgfkeysvalueof{/data point/meta}}
            \pgfkeys{/pgf/fpu=false}
            \node[labelnode={\Couleur!50}] (tempnode) at (axis cs:\pgfkeysvalueof{/data point/x},\floatingend) {$\FloatingLabel_{\newindex}$};
            }
        }},
        spread/.style={
        y filter/.expression={\thisrow{spread}==0 ? nan : y},
        point meta={-\SpreadScale*\thisrow{spread}},
        quiver={u=0,v={-\SpreadScale*\thisrow{spread}},every arrow/.style={spreadarrow},
        after arrow/.code={
            \pgfplotspointgetcoordinates
            \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
            \pgfmathtruncatemacro{\newindex}{\coordindex+1}
            \pgfmathsetmacro{\spreadend}{\pgfkeysvalueof{/data point/y}+0.5*\pgfkeysvalueof{/data point/meta}}
            \pgfkeys{/pgf/fpu=false}
            \node[labelnode={gray}] (tempnode) at (axis cs:\pgfkeysvalueof{/data point/x},\spreadend) {\SpreadLabel};
            }
        }},
    fixedConditional/.style={fixed,densely dotted},
    floatingConditional/.style={floating,densely dotted},
    spreadConditional/.style={spread,densely dotted},
    customaxis/.style={
            width=15cm,
            height=10cm,
            axis y line*=box,
            axis x line*=center,
            date coordinates in=x,
            tick align=inside,
            xtick=data,
            enlargelimits = true,
            xticklabel={$d_{\pgfmathprintnumber[/pgf/number format/fixed]{\month}}$},
            xticklabel style={anchor=center,outer sep=0pt, inner sep=0pt,circle,draw=\Couleur,fill=\Couleur!20,thick,text width=1.5em,align=center},
            axis on top=true,
            clip=false,
    }
}

%% for testing
%Table ReceiveFixPayFloating where I receive fix....
\begin{filecontents*}[overwrite]{ReceiveFixPayFloating.dat}
date        fixed    floating  spread
2019-12-28  5       0       0  %Fixed upfront received, no floating but no spread    
2020-02-01  7       -5      0.5
2020-03-01  0       -6      0.5
2020-04-01  5       -1      0.5
2020-05-01  0       -4      0.5
2020-06-01  7       -3      0.5
2020-07-01  0      -4.5     0.5
2020-08-01  6       -1      0.5
2020-09-01  0       -1.     0.5
2020-10-01  0       -5      0   % Floating final payment paid
\end{filecontents*}

%Table ReceiveFloatingPayFix where I receive floating....
\begin{filecontents*}[overwrite]{ReceiveFloatingPayFix.dat}
date        fixed    floating  spread
2019-12-28  -5       0      0  %Fixed upfront received, no floating but no spread    
2020-02-01  -7       5      0.5
2020-03-01  -0       6      0.5
2020-04-01  -5       1      0.5
2020-05-01  -0       4      0.5
2020-06-01  -7       3      0.5
2020-07-01  -0      4.5     0.5
2020-08-01  -6       1      0.5
2020-09-01  -0       1.     0.5
2020-10-01  -0       5      0   % Floating final payment paid
\end{filecontents*}


\begin{document}
\begin{tikzpicture}
    \begin{axis}[customaxis]
        \addplot[fixed] table [x=date,y expr=0]     {ReceiveFixPayFloating.dat};
        \addplot[floating] table [x=date,y expr=0]  {ReceiveFixPayFloating.dat};
        \addplot[spread] table [x=date,y expr={min(\thisrow{floating},\thisrow{fixed})*ifthenelse(\thisrow{spread}==0,nan,1)}]  {ReceiveFixPayFloating.dat};
    \end{axis}
\end{tikzpicture}
\begin{tikzpicture}
    \begin{axis}[customaxis]
        \addplot[fixed] table [x=date,y expr=0]     {ReceiveFloatingPayFix.dat};
        \addplot[floating] table [x=date,y expr=0]  {ReceiveFloatingPayFix.dat};
        \addplot[spread] table [x=date,y expr={min(\thisrow{floating},\thisrow{fixed})}]  {ReceiveFloatingPayFix.dat};
    \end{axis}
\end{tikzpicture}
\begin{tikzpicture}
    \begin{axis}[customaxis]
        \addplot[fixedConditional] table [x=date,y expr=0]  {ReceiveFixPayFloating.dat};
        \addplot[floatingConditional] table [x=date,y expr=0]   {ReceiveFixPayFloating.dat};
        \addplot[spreadConditional] table [x=date,y expr={min(\thisrow{floating},\thisrow{fixed})}]   {ReceiveFixPayFloating.dat};
    \end{axis}
\end{tikzpicture}
\end{document}

编辑第 3 条:修正扩散箭头。

\documentclass[tikz,margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usetikzlibrary{decorations.pathmorphing}
\usepgfplotslibrary{dateplot}
\pgfmathsetseed{2}

\newif\ifShowSpread     % Display Spread or not
\ShowSpreadtrue         % Display or not the gray arrow
\def\Couleur{orange}    % Parameter the color
\def\FixLabel{C}        % Label for fixed
\def\FloatingLabel{V}   % Label for Floating
\def\SpreadLabel{$+50$bps} % Label for Spread
\def\SpreadScale{4}


\tikzset{
    fixedarrow/.style={-stealth, color=\Couleur,  thick},
    floatingarrow/.style={-stealth, color=\Couleur!50, thick, decorate,decoration={snake, amplitude=1mm, segment length=3mm, pre length=1mm,post length=1mm}},
    spreadarrow/.style={-stealth, color=gray,  thick},
    labelnode/.style={fill opacity=0.5,text opacity=1,draw opacity=1,fill=#1,draw=none,text=black,inner sep=1pt, outer sep=0pt,font=\small}
}

\pgfplotsset{
    fixed/.style={
        %y filter/.expression={\thisrow{fixed}==0 ? nan : y},
        point meta={\thisrow{fixed}},
        quiver={u=0, v={\thisrow{fixed}},every arrow/.style={fixedarrow},
        after arrow/.code={
            \pgfplotspointgetcoordinates
            \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
            \pgfmathtruncatemacro{\newindex}{\coordindex+1}
            \pgfmathsetmacro{\fixedend}{0.5*\pgfkeysvalueof{/data point/meta}}
            \pgfkeys{/pgf/fpu=false}
            \node[labelnode={\Couleur}] at (axis cs:\pgfkeysvalueof{/data point/x},\fixedend) {$\FixLabel_{\newindex}$};
            }
        }},
    floating/.style={
        y filter/.expression={\thisrow{floating}==0 ? nan : y},
        point meta=\thisrow{floating},
        quiver={u=0,v=\thisrow{floating},every arrow/.style={floatingarrow},
        after arrow/.code={
            \pgfplotspointgetcoordinates
            \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
            \pgfmathtruncatemacro{\newindex}{\coordindex+1}
            \pgfmathsetmacro{\floatingend}{0.5*\pgfkeysvalueof{/data point/meta}}
            \pgfkeys{/pgf/fpu=false}
            \node[labelnode={\Couleur!50}] (tempnode) at (axis cs:\pgfkeysvalueof{/data point/x},\floatingend) {$\FloatingLabel_{\newindex}$};
            }
        }},
        spread/.style={
        y filter/.expression={\thisrow{spread}==0 ? nan : y},
        point meta={-\SpreadScale*\thisrow{spread}},
        quiver={u=0,v={-\SpreadScale*\thisrow{spread}},every arrow/.style={spreadarrow},
        after arrow/.code={
            \pgfplotspointgetcoordinates
            \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
            \pgfmathtruncatemacro{\newindex}{\coordindex+1}
            \pgfmathsetmacro{\spreadend}{\pgfkeysvalueof{/data point/y}+0.5*\pgfkeysvalueof{/data point/meta}}
            \pgfkeys{/pgf/fpu=false}
            \node[labelnode={gray}] (tempnode) at (axis cs:\pgfkeysvalueof{/data point/x},\spreadend) {\SpreadLabel};
            }
        }},
    fixedConditional/.style={fixed,densely dotted},
    floatingConditional/.style={floating,densely dotted},
    spreadConditional/.style={spread,densely dotted},
    customaxis/.style={
            width=15cm,
            height=10cm,
            axis y line*=box,
            axis x line*=center,
            date coordinates in=x,
            tick align=inside,
            xtick=data,
            enlargelimits = true,
            xticklabel={$d_{\pgfmathprintnumber[/pgf/number format/fixed]{\month}}$},
            xticklabel style={anchor=center,outer sep=0pt, inner sep=0pt,circle,draw=\Couleur,fill=\Couleur!20,thick,text width=1.5em,align=center},
            axis on top=true,
            clip=false,
    }
}

%% for testing
%Table ReceiveFixPayFloating where I receive fix....
\begin{filecontents*}[overwrite]{ReceiveFixPayFloating.dat}
date        fixed    floating  spread
2019-12-28  5       0       0  %Fixed upfront received, no floating but no spread    
2020-02-01  7       -5      0.5
2020-03-01  0       -6      0.5
2020-04-01  5       -1      0.5
2020-05-01  0       -4      0.5
2020-06-01  7       -3      0.5
2020-07-01  0      -4.5     0.5
2020-08-01  6       -1      0.5
2020-09-01  0       -1.     0.5
2020-10-01  0       -5      0   % Floating final payment paid
\end{filecontents*}

%Table ReceiveFloatingPayFix where I receive floating....
\begin{filecontents*}[overwrite]{ReceiveFloatingPayFix.dat}
date        fixed    floating  spread
2019-12-28  -5       0      0  %Fixed upfront received, no floating but no spread    
2020-02-01  -7       5      -0.5
2020-03-01  -0       6      -0.5
2020-04-01  -5       1      -0.5
2020-05-01  -0       4      -0.5
2020-06-01  -7       3      -0.5
2020-07-01  -0      4.5     -0.5
2020-08-01  -6       1      -0.5
2020-09-01  -0       1.     -0.5
2020-10-01  -0       5      0   % Floating final payment paid
\end{filecontents*}


\begin{document}
\begin{tikzpicture}
    \begin{axis}[customaxis]
        \addplot[fixed] table [x=date,y expr=0]     {ReceiveFixPayFloating.dat};
        \addplot[floating] table [x=date,y expr=0]  {ReceiveFixPayFloating.dat};
        \addplot[spread] table [x=date,y expr={\thisrow{floating}*ifthenelse(\thisrow{spread}==0,nan,1)}]  {ReceiveFixPayFloating.dat};
    \end{axis}
\end{tikzpicture}
\begin{tikzpicture}
    \begin{axis}[customaxis]
        \addplot[fixed] table [x=date,y expr=0]     {ReceiveFloatingPayFix.dat};
        \addplot[floating] table [x=date,y expr=0]  {ReceiveFloatingPayFix.dat};
        \addplot[spread] table [x=date,y expr={\thisrow{floating}*ifthenelse(\thisrow{spread}==0,nan,1)}]  {ReceiveFloatingPayFix.dat};
    \end{axis}
\end{tikzpicture}
\begin{tikzpicture}
    \begin{axis}[customaxis]
        \addplot[fixedConditional] table [x=date,y expr=0]  {ReceiveFixPayFloating.dat};
        \addplot[floatingConditional] table [x=date,y expr=0]   {ReceiveFixPayFloating.dat};
        \addplot[spreadConditional] table [x=date,y expr={\thisrow{floating}*ifthenelse(\thisrow{spread}==0,nan,1)}]   {ReceiveFixPayFloating.dat};
    \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

在此处输入图片描述

在此处输入图片描述

相关内容