#やりたいこと
pythonのpandasで表を作成しています。
そこで、表の中の特定のセルに色を付け、その表を画像データとして保存したい。
#困っていること
以下のサイトを参考にして表を作成していましたが、保存した表に色が付きません。
https://analytics-note.xyz/programming/style-applymap/
現在のコードを以下に示します。
若干コードを諸略していますが、表に関する部分は以下の箇所になります。
また,flow1というリストには、数値データが格納されています。
有識者の方々ご教授のほどよろしく
python
1from argparse import ArgumentParser 2import csv 3import matplotlib.pyplot as plt 4import pandas as pd 5import seaborn as sns 6 7def cell_style(value): 8 if value != value: 9 return "background-color: gray; color: white" 10 if value <= 40: 11 return "background-color: yellow; font-weight: bold" 12 else: 13 return "" 14 15if __name__ == '__main__': 16 17 list1=["a","b","c","d","e","f"] 18 list2=["A","B","C"] 19 df=pd.DataFrame(data=flow1, index=list2, columns=list1) 20 21 df.style.applymap(cell_style) 22 23 24 fig,ax = plt.subplots(figsize=((len(df.columns))*1.2, (len(df))*0.5)) 25 ax.axis('off') 26 tbl = ax.table(cellText=df.values, 27 bbox=[0,0,1,1], 28 colLabels=df.columns, 29 rowLabels=df.index) 30 plt.savefig('table.png')
#環境
OS:ubuntu18.04
言語:python2.7
#追記
###プログラム変更点
python
1 2df.style.applymap(cell_style) 3 4 colors = [["#56b5fd","w","w"],[ "#1ac3f5","w","w"],[ "#1ac3f5","w","w"]] 5 fig,ax = plt.subplots(figsize=((len(df.columns))*1.2, (len(df))*0.5)) 6 ax.axis('off') 7 tbl = ax.table(cellText=df.values, 8 bbox=[0,0,1,1], 9 colLabels=df.columns, 10 rowLabels=df.index, 11 cellColours=colors) 12 plt.savefig('table.png')
###エラー
ValueError:Each row in 'cellColours' must have 6 columns
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/04/12 20:27
2021/04/12 20:44
2021/04/12 22:12