今天講解下如何使用Python繪制各種Excel圖表,下面我們以繪制餅狀圖、柱狀圖、水平圖、氣泡圖、2D面積圖、3D面積圖為例來說明。
import openpyxl
from openpyxl import Workbook
from openpyxl.chart import (
Reference,
Series,
PieChart,
BarChart,
BubbleChart,
AreaChart,
AreaChart3D
)
繪制餅圖
wb = openpyxl.Workbook()
ws = wb.active
ws.title = 'pieChart'
rows = [
['Pie', 'Sold'],
['Apple', 50],
['Cherry', 30],
['Pumpkin', 10],
['Chocolate', 40]
]
# for循環寫入Excel
for row in rows:
ws.append(row)
# 創建一個餅圖對象
pie = PieChart()
# 定義標簽和數據范圍
labels = Reference(ws, min_col=1, min_row=2, max_row=5)
data = Reference(ws, min_col=2, min_row=2, max_row=5)
# 添加數據和標簽
pie.add_data(data)
pie.set_categories(labels)
# 設置餅圖標題
pie.title = 'Pies sold by category'
# 設置餅圖的位置
ws.add_chart(pie, 'C1')
繪制柱形圖
ws = wb.create_sheet('columnChart')
rows = [
('Number', 'Batch1', 'Batch2'),
(2, 10, 30),
(3, 40, 60),
(4, 50, 70),
(5, 20, 10),
(6, 10, 40),
(7, 50, 30)
]
for row in rows:
ws.append(row)
# 創建一個柱形圖對象
columnChart = BarChart()
columnChart.type = 'col'
columnChart.style = 10 # 這種風格,色彩對比很鮮明
columnChart.title = 'batchColumnChart'
# 定義橫縱坐標的標題
columnChart.x_axis.title = 'Test number'
columnChart.y_axis.title = 'Sample length(mm)'
# 定義category和data范圍
categories = Reference(ws, min_col=1, min_row=2, max_row=7)
data = Reference(ws, min_col=2, max_col=3, min_row=2, max_row=7)
# 添加category和data
columnChart.set_categories(categories)
columnChart.add_data(data)
# 設置柱形圖的位置
ws.add_chart(columnChart, 'D1')
繪制水平圖
ws = wb.create_sheet('barChart')
rows = [
('Number', 'Batch1', 'Batch2'),
(2, 10, 30),
(3, 40, 60),
(4, 50, 70),
(5, 20, 10),
(6, 10, 40),
(7, 50, 30)
]
for row in rows:
ws.append(row)
# 創建一個水平圖對象
barChart = BarChart()
barChart.type = 'bar'
barChart.style = 10 # 這種風格,色彩對比很鮮明
barChart.title = 'batchBarChart'
# 定義橫縱坐標的標題
barChart.y_axis.title = 'Sample length(mm)'
barChart.x_axis.title = 'Test number'
# 注意:對於柱形圖變成水平圖,x和y軸的標題不用改變。
# 定義category和data范圍
categories = Reference(ws, min_col=1, min_row=2, max_row=7)
data = Reference(ws, min_col=2, max_col=3, min_row=2, max_row=7)
# 添加category和data
barChart.set_categories(categories)
barChart.add_data(data)
# 設置水平圖的位置
ws.add_chart(barChart, 'D1')
繪制氣泡圖
ws = wb.create_sheet('bubbleChart')
rows = [
('Number of products', 'Sales in USA', 'Market share'),
(14, 12200, 15),
(20, 60000, 33),
(18, 24400, 10),
(22, 32000, 42),
(),
(12, 8200, 18),
(15, 50000, 30),
(19, 24400, 15),
(25, 25000, 50)
]
for row in rows:
ws.append(row)
# 創建一個氣泡圖對象
bubbleChart = BubbleChart()
bubbleChart.style = 18 # 這種風格,色彩對比很鮮明
bubbleChart.title = 'bubbleChart'
# 添加第一組數據
xValues = Reference(ws, min_col=1, min_row=2, max_row=5)
yValues = Reference(ws, min_col=2, min_row=2, max_row=5)
size = Reference(ws, min_col=3, min_row=2, max_row=5)
series = Series(values=yValues, xvalues=xValues, zvalues=size, title=2013)
bubbleChart.series.append(series)
# 添加第二組數據
xValues = Reference(ws, min_col=1, min_row=7, max_row=10)
yValues = Reference(ws, min_col=2, min_row=7, max_row=10)
size = Reference(ws, min_col=3, min_row=7, max_row=10)
series = Series(values=yValues, xvalues=xValues, zvalues=size, title=2014)
bubbleChart.series.append(series)
# 設置氣泡圖的位置
ws.add_chart(bubbleChart, 'D1')
繪制2D面積圖
wb = Workbook()
ws = wb.active
ws.title = 'areaChart2D'
rows = [
['Number', 'Batch 1', 'Batch 2'],
[2, 40, 30],
[3, 40, 25],
[4, 50, 30],
[5, 30, 10],
[6, 25, 5],
[7, 50, 10],
]
for row in rows:
ws.append(row)
chart = AreaChart()
chart.title = 'Area Chart2D'
chart.style = 13
chart.x_axis.title = 'Test'
chart.y_axis.title = 'Percentage'
cats = Reference(ws, min_col=1, min_row=1, max_row=7)
data = Reference(ws, min_col=2, max_col=3, min_row=1, max_row=7)
chart.set_categories(cats)
chart.add_data(data, titles_from_data=True)
ws.add_chart(chart, 'D1')
繪制3D面積圖
wb = Workbook()
ws = wb.active
ws.title = 'areaChart3D'
rows = [
['團隊名稱', 'Q1', 'Q2', 'Q3'],
['精英隊', 1200, 1800, 2200],
['王者隊', 1500, 2000, 2500],
['野戰隊', 1000, 2200, 3000],
['虎狼隊', 1100, 1650, 2550],
['戰狼隊', 1150, 1700, 2650],
['金牌隊', 1200, 1950, 3150],
['無敵隊', 1050, 1700, 2730]
]
for row in rows:
ws.append(row)
chart = AreaChart3D()
chart.title = '各團隊每季度銷售業績3D對比圖(單位:萬元)'
chart.style = 10
chart.x_axis.title = 'Team name' # 團隊名稱
chart.y_axis.title = 'Sales volume' # 銷售業績
# chart.z_axis.title = 'quarter' # 季度
# chart.y_axis.scaling.min = 0 # y軸最小值
# chart.y_axis.majorUnit = 500 # 間距
# chart.y_axis.scaling.max = 3500 # y軸最大值
chart.width = 20 # 默認15
chart.height = 13 # 默認7.5
chart.legend = None # 顏色區域說明
cats = Reference(ws, min_col=1, min_row=1, max_row=8)
data = Reference(ws, min_col=2, max_col=4, min_row=1, max_row=8)
chart.set_categories(cats)
chart.add_data(data, titles_from_data=True)
ws.add_chart(chart, 'F1')
# 保存工作簿
wb.save('charts.xlsx')
運行程序結果如下:

需要注意的一點是:實測使用WPS不支持該方法創建三維面積圖!!!
最后,覺得有用的朋友麻煩點贊,推薦一下,更多精彩內容持續更新中!也歡迎大家評論區留言、交流,共同學習進步。