回答編集履歴

1

編集

2021/04/12 20:06

投稿

FredericChang
FredericChang

スコア54

test CHANGED
@@ -1,14 +1,12 @@
1
+ いろいろの資料を調べて、色が付いたDFを画像に直接に転換できないだと思います。
2
+
3
+ 今回は、ax.table(cellColours=colors) を使って、画像の欄を色が付いた。
4
+
1
- あなたコード削除し、色を付けできました
5
+ サンプル参考にしてください
2
6
 
3
7
 
4
8
 
5
9
  ```python
6
-
7
- import pandas as pd
8
-
9
-
10
-
11
-
12
10
 
13
11
  def cell_style(value):
14
12
 
@@ -28,18 +26,48 @@
28
26
 
29
27
 
30
28
 
31
- list1=["a","b","c","d","e","f"]
29
+ # 適当なデータフレームを作成
32
30
 
33
- list2=["A","B","C"]
31
+ df = pd.DataFrame(
34
32
 
35
- df=pd.DataFrame(np.random.randint(0, 100, size=(3, 6)), index=list2, columns=list1)
33
+ np.random.randint(0, 100, size=(5, 3)),
34
+
35
+ columns=["col0", "col1", "col2"]
36
+
37
+ )
38
+
39
+ df.loc[3, "col0"] = None
40
+
41
+ df.loc[1, "col2"] = None
42
+
43
+ df.style.applymap(cell_style)
36
44
 
37
45
 
38
46
 
47
+
48
+
49
+ colors = [["#56b5fd","w","w"],[ "#1ac3f5","w","w"],[ "#1ac3f5","w","w"],[ "#1ac3f5","w","w"],[ "#1ac3f5","w","w"]]
50
+
51
+
52
+
53
+ fig,ax = plt.subplots(figsize=((len(df.columns))*1.2, (len(df))*0.5))
54
+
55
+ ax.axis('off')
56
+
39
- df.style.applymap(cell_style)
57
+ tbl = ax.table(cellText=df.values,
58
+
59
+ bbox=[0,0,1,1],
60
+
61
+ colLabels=df.columns,
62
+
63
+ rowLabels=df.index,
64
+
65
+ cellColours=colors )
66
+
67
+ plt.savefig('table.png')
40
68
 
41
69
  ```
42
70
 
43
71
 
44
72
 
45
- ![イメージ説明](5f84c78cecbdc63e23b1fa32a7808927.jpeg)
73
+ ![イメージ説明](d33b3a266ed261cdc5ce2a123abf9163.jpeg)