應用
生成現金流量圖的最佳工具
我經常需要生成現金流量圖。
我想知道是否有人有一個很好的工具來生成它們 $ \LaTeX $ 還是圖片?
我在這個頁面上找到了一個很好的例子,使用 pgf/tikZ 包 $ \LaTeX $ .
該網站正在提供程式碼 $ \LaTeX $ ,如果您使用的是 LyX,為了獲得與網站相同的樣本,您必須執行以下操作。首先使用 Document->Settings 編輯文件序言:
\usepackage{siunitx} \usepackage{tikz} \usetikzlibrary{calc,matrix}
然後是程式碼塊中的以下內容:
\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}
這可以使用帶有以下功能的 python 來完成(所有文件都可以在這個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)