顯示具有 Pivot table 標籤的文章。 顯示所有文章
顯示具有 Pivot table 標籤的文章。 顯示所有文章

2024年5月27日 星期一

在Excel工作表中新增漁產品交易折線圖

上一篇文章:自動化Python+Excel建立漁產品交易行情樞紐分析表


範例一、新增交易量折線圖

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
from openpyxl import load_workbook
from openpyxl.chart import LineChart, Reference

wb = load_workbook("AquaticTransData_pivot.xlsx")
ws = wb.active 
data = Reference(ws,
                 min_col = 2, 
                 min_row = 1,
                 max_row = 12)  
chart = LineChart()
chart.add_data(data,
               titles_from_data=True)
labels = Reference(ws, min_col=1, min_row=2, max_row=12)
chart.set_categories(labels)
chart.title = "漁產品交易行情圖表"
chart.x_axis.title = "交易日期"
chart.y_axis.title = "交易量"
ws.add_chart(chart, "E1")
wb.save("AquaticTransData_pivot_graph.xlsx")
wb.close()

執行結果:

範例二、交易量用長條圖、平均價用折線圖

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from openpyxl import load_workbook
from openpyxl.chart import LineChart, BarChart, Reference

wb = load_workbook("AquaticTransData_pivot.xlsx")
ws = wb.active 
data = Reference(ws,
                 min_col = 2,
                 min_row = 1,
                 max_row = 12)  
chart = BarChart()
chart.add_data(data,
               titles_from_data=True)
labels = Reference(ws, min_col=1, min_row=2, max_row=12)
chart.set_categories(labels)
chart.title = "漁產品交易行情圖表"
chart.x_axis.title = "交易日期"
chart.y_axis.title = "交易量"
chart.y_axis.majorGridlines = None


data2 = Reference(ws,
                 min_col = 3,
                 min_row = 1,
                 max_row = 12)  
chart2 = LineChart()
chart2.add_data(data2,
               titles_from_data=True)
labels2 = Reference(ws, min_col=1, min_row=2, max_row=12)
chart2.set_categories(labels2)
chart2.y_axis.title = "平均價"
chart2.y_axis.axId = 200 #要給一個獨特的ID
chart2.y_axis.crosses = "max"

chart+=chart2

ws.add_chart(chart, "E1")
wb.save("AquaticTransData_pivot_graph.xlsx")
wb.close()

執行結果:



自動化Python+Excel建立漁產品交易行情樞紐分析表

上一篇文章:把近日的漁產品交易行情儲存到Excel檔案中

樞紐分析表又稱為透視表,是用來分類進行加總、平均等計算很好用的工具,本篇文章就用它來說明如何分析漁產品交易行情的數據。

範例一、分析各市場交易量

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import requests
import json
import pandas as pd

# 從URL取得JSON資料
r = requests.get('https://data.moa.gov.tw/Service/OpenData/FromM/AquaticTransData.aspx')
json_data = json.loads(r.text)

# 轉換成DataFrame
df = pd.DataFrame(json_data)

# 儲存到Excel檔案
df.to_excel('AquaticTransData.xlsx', index=False)

print("漁產品交易資料已儲存到 AquaticTransData.xlsx")

#進行樞杻分析
pivot_aquatic = df.pivot_table(index=["市場名稱"],                  
                                values=["交易量"],
                                aggfunc="sum")
print(pivot_aquatic)

# 將樞杻分析表的資料儲存到Excel檔案
pivot_aquatic.to_excel("AquaticTransData_pivot.xlsx",
                        engine="openpyxl")
print("樞杻分析資料已儲存到 AquaticTransData_pivot.xlsx")

執行結果:
漁產品交易資料已儲存到 AquaticTransData.xlsx
            交易量
市場名稱           
三重     295271.9
佳里      10210.7
台中     384518.1
台北     612065.8
台南      68311.5
嘉義     324195.2
埔心     255472.7
基隆     250774.0
岡山     122505.4
彰化     188285.7
斗南      87748.4
新港     138251.2
新營      11308.7
新竹     132487.7
東港    1607521.0
桃園     143057.0
梓官      14915.0
澎湖      69262.1
興達港       300.0
花蓮       8302.4
苗栗      47076.7
蘇澳     287038.0
頭城     210618.6
高雄    1398667.0
樞杻分析資料已儲存到 AquaticTransData_pivot.xlsx

AquaticTransData.xlsx表格內容:

AquaticTransData_pivot.xlsx表格內容

範例二、近日交易量和平均價

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import requests
import json
import pandas as pd

# 從URL取得JSON資料
r = requests.get('https://data.moa.gov.tw/Service/OpenData/FromM/AquaticTransData.aspx')
json_data = json.loads(r.text)

# 轉換成DataFrame
df = pd.DataFrame(json_data)

# 儲存到Excel檔案
df.to_excel('AquaticTransData.xlsx', index=False)

print("漁產品交易資料已儲存到 AquaticTransData.xlsx")

#進行樞杻分析
pivot_aquatic = df.pivot_table(index=["交易日期"],                  
                                values=["交易量", "平均價"],
                                aggfunc="sum")
print(pivot_aquatic)

# 將樞杻分析表的資料儲存到Excel檔案
pivot_aquatic.to_excel("AquaticTransData_pivot.xlsx",
                        engine="openpyxl")
print("樞杻分析資料已儲存到 AquaticTransData_pivot.xlsx")

執行結果:
漁產品交易資料已儲存到 AquaticTransData.xlsx
               交易量       平均價
交易日期                        
1130516    28086.7   12888.4
1130517   520185.8  183953.0
1130518   493360.0  178200.1
1130519   311885.2  166672.9
1130520   458216.9   50116.8
1130521  1183959.2  242617.6
1130522   896460.7  209834.5
1130523   835120.8  183898.3
1130524   975239.0  211522.9
1130525   677660.3  205467.5
1130526   287990.2  154860.1
樞杻分析資料已儲存到 AquaticTransData_pivot.xlsx