【matplotlib】バランスシートを作りたい【python】

企業の財務状況と言えばバランスシート。これを何とかしてpythonで描けないかと色々頑張ってみた。

色々頑張った甲斐あって、かなりpythonっぽくないグラフが出来たのではないかと思う。コードはいつにも増して長くなってしまったので必要なところだけ抜き出してご参考ください。


import
numpy as np import pandas as pd import matplotlib.pyplot as plt import japanize_matplotlib import matplotlib.patheffects as patheffects ##データ labels = ['A社', 'B社']#x軸のラベル data_labels_l = ['現金預金', '売上債権', '棚卸資産', 'その他流動資産', '有形固定資産', '無形固定資産', 'その他固定資産'] data_labels_r = ['買入債務', 'その他流動負債', '固定負債', '自己資本', 'その他純資産'] df_l = pd.DataFrame(columns=data_labels_l)#バランスシート左側(運用サイド) df_l.loc['A'] = [280, 810 ,800, 250, 1900, 1180, 520] df_l.loc['B'] = [260, 530 ,740, 220, 820, 670, 900] df_l['合計'] = df_l.sum(axis=1) df_r = pd.DataFrame(columns=data_labels_r)#バランスシート右側(調達サイド) df_r.loc['A'] = [470, 1180, 2120, 1560, 410] df_r.loc['B'] = [300, 970, 1400, 1170, 300] df_r['合計'] = df_r.sum(axis=1) ##グラフ設定 fig_x = 6#グラフサイズ(x) fig_y = 9#グラフサイズ(y) fig = plt.figure(figsize=(fig_x,fig_y), facecolor="white")#グラフの作成 ax1 = fig.add_subplot(111)#subplotの作成 plt.rcParams['font.family'] = 'Meiryo'#フォント指定 ##縦棒グラフの設定 bar_w = 0.4 #棒グラフの太さ bar_color_l = ["#22aa00", "#55cc44", "#aaee33", "#ddffcc", "#ffffaa", "#eebb00", "#ffff00"]#左側の色設定(翠・黄色) bar_color_r = ["#dd3333", "#dd6666", "#ddaaaa", "#0077bb", "#226688"]#右側の色設定(赤・青) bar_edge = "#777777"#各グラフの外枠 bar_label = "#000000"#データラベルの色 text_s = 12#データラベルのサイズ ind = np.arange(len(labels)) #要素の数 ##棒グラフの描画 #左側 top_l = df_l['合計']#棒グラフの上側を設定 for index, column in enumerate(data_labels_l): bottom_l = top_l - df_l[column]#棒グラフの下側を設定 #棒グラフの描画 ax1.bar(ind - bar_w/2,#x座標。左に少しずらす top_l-bottom_l,#棒グラフ上側のy座標 bottom=bottom_l,#棒グラフ下側のy座標 width=bar_w, #棒グラフの太さ label=column,#データラベル color=bar_color_l[index],#棒グラフの色 edgecolor=bar_edge)#縦棒グラフ #データラベル for m in np.arange(len(bottom_l)): ax1.text(ind[m]- bar_w/2,#x座標 (bottom_l[m]+top_l[m])/2,#y座標 column,#テキスト ha='center', va='center',#水平・垂直の中心位置 color=bar_label,#文字色 fontsize=text_s,#文字サイズ path_effects=[patheffects.withStroke(linewidth=4, foreground='white', capstyle="round")]#文字外周を白色に ) top_l = top_l - df_l[column]#棒グラフ上側のy座標を更新 #右側(左側とほぼ同様) top_r = df_r['合計'] for index, column in enumerate(data_labels_r): bottom_r = top_r - df_r[column] ax1.bar(ind + bar_w/2, top_r-bottom_r, bottom=bottom_r, width=bar_w, label=column, color=bar_color_r[index],edgecolor=bar_edge)#縦棒グラフ for m in np.arange(len(bottom_r)): ax1.text(ind[m]+ bar_w/2, (bottom_r[m]+top_r[m])/2, column, ha='center', va='center', color=bar_label, fontsize=text_s, path_effects=[patheffects.withStroke(linewidth=4, foreground='white', capstyle="round")]) top_r = top_r - df_r[column] ##x軸 xlabel_s = 15#文字サイズ xlabel_c = "#444444"#文字色 ax1.tick_params(axis='x', which='both', bottom=False, top=False)#xticksの目盛を消す ax1.set_xticks(ind)#x軸の補助目盛設定 ax1.set_xticklabels(labels, fontsize=xlabel_s, color=xlabel_c)#x軸のラベルの設定 ##y軸 y_ticks = [0,1000,2000,3000,4000,5000]#y軸の補助目盛 ylabel_s = 15#文字サイズ ylabel_c = "#444444"#文字色 yticks_c = "#777777"#補助目盛色 ax1.set_yticks(y_ticks, color=yticks_c)#y軸の補助目盛設定 ax1.set_yticklabels(y_ticks, fontsize=ylabel_s, color=ylabel_c)#y軸のラベル設定 ax1.grid(axis="y")#補助目盛あり ax1.tick_params(axis='both', which='both', bottom=False, top=False, left=False, right=False)#yticksの目盛を消す ##枠線を消す spine_w = 1 spine_c = "#444444" ax1.spines['top'].set_linewidth(0) ax1.spines['bottom'].set_linewidth(spine_w)#bottomだけは線を引く ax1.spines['bottom'].set_color(spine_c)#bottomだけは線を引く ax1.spines['left'].set_linewidth(0) ax1.spines['right'].set_linewidth(0) ##単位 fig.text(0.02, 0.83, '(億円)', ha='left', va='center', color=ylabel_c , fontsize= text_s) ##軸を背面に移動 ax1.set_axisbelow(True) plt.show()

[blog:g:11696248318754550880:banner][blog:g:11696248318755499803:banner]