8
$\begingroup$

I often have to generate cashflow diagrams.

I was wondering if anyone has a good tool to generate them in either $\LaTeX$ or a picture?

$\endgroup$
2
$\begingroup$

I found a nice example on this page using the pgf/tikZ package for $\LaTeX$.

The website is giving the code for $\LaTeX$, if you are using LyX, in order to get the same sample than the website you have to do the following. First edit the document preamble using Document->Settings:

 \usepackage{siunitx}
 \usepackage{tikz}
 \usetikzlibrary{calc,matrix}

and then the following in a code block:

 \begin{tikzpicture}[>=stealth,ultra thick]
    \draw[->] (0,0) -- (10,0);
    \draw (-1,0) node[below] {n} (-1,0.5) node[above] {PMT};
    \foreach \x/\n in {1/0,3/1,5/2,7/3,9/4}
      \draw (\x,0) node(\n)[below] {\n} -- (\x,0.5);
    \foreach \x/\n in {1/0,3/1,5/2,7/3}
      \draw (\x,0.5) node[above] {\num{5000}};
    \matrix (calc) [matrix of math nodes,matrix anchor=north west,nodes={anchor=east}] at (9,-1) {%
      \$ \num{5300.00} & = & \num{5000}\times (\num{1.06})^1 \\
      \$ \num{5618.00} & = & \num{5000}\times (\num{1.06})^2 \\
      \$ \num{5955.08} & = & \num{5000}\times (\num{1.06})^3 \\
      \$ \num{6312.38} & = & \num{5000}\times (\num{1.06})^4 \\
      \$ \num{23185.46} \\
    };
    \draw[thick] (calc-5-1.north west) -- (calc-5-1.north east);
    \foreach \n/\l in {0/4,1/3,2/2,3/1}
      \draw[->,gray!50] (\n) |- ($(calc-\l-1.west)+(-0.5,0)$);
  \end{tikzpicture}
$\endgroup$
  • $\begingroup$ Why do you think this requires LyX? $\endgroup$ – chrisaycock Apr 16 '13 at 12:50
  • $\begingroup$ It does not, I just gave the steps to use it with LyX, will edit to clarify. I put the steps for $LyX$ because I know many people who are not familiar with working directly with $\LaTeX$ files. $\endgroup$ – BlueTrin Apr 16 '13 at 12:51
2
$\begingroup$

Aside from TikZ approach that you've found, these scripting languages would work:

  • MetaPost
  • PSTricks
  • Asymptote
  • MATLAB, see cfplot
  • Mathematica, see Cashflow

Vector graphics are most suitable for the task, so if you prefer GUI point-and-click:

  • Adobe Illustrator
  • CorelDRAW
  • Inkscape (free)
$\endgroup$
  • 1
    $\begingroup$ Beyond a list of software packages, can you give examples of how someone would create the plots? Like are there some MetaPost commands the OP could copy and paste? $\endgroup$ – chrisaycock Apr 17 '13 at 2:09
  • 1
    $\begingroup$ Yes I could, but it appears from his question that he asked for a good tool, not specific commands. $\endgroup$ – madilyn Apr 17 '13 at 18:44
2
$\begingroup$

This can be done using python with the following function (all the documentation is available in this GitHub Repo):

import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator

def diagram (t, value, cashflow, c1="k", c2="k", currency="$", path = "test.png", aspect = [12,6], int_x = True, bar = True, show = False):

    fig, ax1 = plt.subplots(figsize=(aspect))

    # Define x axis as integer (optional)
    if int_x:
        ax1.xaxis.set_major_locator(MaxNLocator(integer=True))

    # Plot value (left axis) with color c1
    if (bar):
        ax1.bar(t, value, color = c1,  alpha= 0.3)
    else:
        ax1.plot(t, value, color = c1,  alpha= 0.3)
    ax1.set_xlabel('time (year)')

    # Set value label and currency
    ax1.set_ylabel('Future Value '+ currency, color = c1)
    ax1.tick_params('y', colors = c1)

    # Define twin axis for cashflow
    ax2 = ax1.twinx()

    # Find positive and negative values in cashflow
    pos = [i for i in range(len(cashflow)) if cashflow[i] > 0]
    neg = [i for i in range(len(cashflow)) if cashflow[i] < 0]

    # Plot cashflow (right axis) in the correct direction with color c2
    if (len(pos)):
        markerline, stemlines, baseline = ax2.stem(t[pos], cashflow[pos], markerfmt='^', basefmt=" ")
        plt.setp(stemlines, 'color', c2)
        plt.setp(markerline, 'color', c2)
    if (len(neg)):
        markerline, stemlines, baseline = ax2.stem(t[neg], cashflow[neg] ,markerfmt='v', basefmt=" ")
        plt.setp(stemlines, 'color', c2)
        plt.setp(markerline, 'color', c2)

    # Set cashflow label and currency
    ax2.set_ylabel('Cash Flow '+ currency, color=c2)
    ax2.tick_params('y', colors=c2)
    fig.tight_layout()

    # Remove the frame to visualize data more clearly
    ax1.spines['top'].set_visible(False)
    ax1.spines['right'].set_visible(False)
    ax1.spines['bottom'].set_visible(False)
    ax1.spines['left'].set_visible(False)
    ax2.spines['top'].set_visible(False)
    ax2.spines['right'].set_visible(False)
    ax2.spines['bottom'].set_visible(False)
    ax2.spines['left'].set_visible(False)

    # Save the plot to the specified path with 500dpi
    fig.savefig(path, dpi = 500)

    # Show plot (optional)
    plt.show (show) 
$\endgroup$

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.