企業の財務状況と言えばバランスシート。これを何とかして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社']
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
fig_y = 9
fig = plt.figure(figsize=(fig_x,fig_y), facecolor="white")
ax1 = fig.add_subplot(111)
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,
top_l-bottom_l,
bottom=bottom_l,
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,
(bottom_l[m]+top_l[m])/2,
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]
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]
xlabel_s = 15
xlabel_c = "#444444"
ax1.tick_params(axis='x', which='both', bottom=False, top=False)
ax1.set_xticks(ind)
ax1.set_xticklabels(labels, fontsize=xlabel_s, color=xlabel_c)
y_ticks = [0,1000,2000,3000,4000,5000]
ylabel_s = 15
ylabel_c = "#444444"
yticks_c = "#777777"
ax1.set_yticks(y_ticks, color=yticks_c)
ax1.set_yticklabels(y_ticks, fontsize=ylabel_s, color=ylabel_c)
ax1.grid(axis="y")
ax1.tick_params(axis='both', which='both', bottom=False, top=False, left=False, right=False)
spine_w = 1
spine_c = "#444444"
ax1.spines['top'].set_linewidth(0)
ax1.spines['bottom'].set_linewidth(spine_w)
ax1.spines['bottom'].set_color(spine_c)
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]