質問編集履歴
1
excellデータと、コードの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,15 +1,77 @@
|
|
1
1
|
pythonのopenpyxlで棒グラフを作成しています。
|
2
|
+
|
3
|
+
添付図の左図を表から作ることできました。
|
2
4
|
|
3
5
|
|
4
6
|
|
5
|
-
グラフデータの行/列の切り替えをしたいです。
|
7
|
+
左図のグラフデータの行/列の切り替え、左図を作成するときに右図のように出力したいのですが方法が分かりません。
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
そのような関数は用意されていますか?
|
10
|
-
|
11
|
-
一度表の行列を入れ替えて、グラフを作成するしかないですか?
|
12
8
|
|
13
9
|
|
14
10
|
|
15
11
|
ご存じの方、教えていただきたいです。
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+

|
16
|
+
|
17
|
+
[リンク内容](https://drive.google.com/file/d/1xIQLM1t3Cntzj6hZCwl2vxPI4mIOklCH/view?usp=sharing)
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
```python
|
22
|
+
|
23
|
+
import openpyxl
|
24
|
+
|
25
|
+
from openpyxl.chart import BarChart, Reference
|
26
|
+
|
27
|
+
from openpyxl.drawing.text import Paragraph, ParagraphProperties, CharacterProperties, Font
|
28
|
+
|
29
|
+
from openpyxl.chart.text import RichText
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
wb = openpyxl.load_workbook('test_chart.xlsx')
|
34
|
+
|
35
|
+
sheetnames = wb.get_sheet_names()
|
36
|
+
|
37
|
+
ws = wb[sheetnames[0]]
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
chart = BarChart()
|
42
|
+
|
43
|
+
chart.type = "col"
|
44
|
+
|
45
|
+
chart.style = 10
|
46
|
+
|
47
|
+
chart.grouping = "stacked"
|
48
|
+
|
49
|
+
chart.overlap = 100
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
font_text = Font(typeface='Meirio')
|
54
|
+
|
55
|
+
cp = CharacterProperties(latin=font_text, sz=1500)
|
56
|
+
|
57
|
+
chart.x_axis.txPr = RichText(p=[Paragraph(pPr=ParagraphProperties(defRPr=cp), endParaRPr=cp)])
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
data = Reference(ws, min_col=2, max_col=4, min_row=1, max_row=5)
|
62
|
+
|
63
|
+
cats = Reference(ws, min_col=1, min_row=2, max_row=5)
|
64
|
+
|
65
|
+
chart.add_data(data, titles_from_data=True)
|
66
|
+
|
67
|
+
chart.set_categories(cats)
|
68
|
+
|
69
|
+
ws.add_chart(chart, "A7")
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
wb.save('test_chart.xlsx')
|
74
|
+
|
75
|
+
wb.close()
|
76
|
+
|
77
|
+
```
|