Python openpyxl 面积制图 笔记

原创
小哥 4年前 (2022-10-17) 阅读数 1 #PYTHON

制图

2D面积图

from openpyxl import Workbook
from openpyxl.chart import(AreaChart, Reference)

wb = Workbook()
ws = wb.active

rows = [
    [number, batch1, batch2],
    [2, 40, 45],
    [3, 34, 23],
    [4, 34, 23],
    [5, 54, 24],
    [6, 56, 53],
    [7, 35, 26],
]

for row in rows:
    ws.append(row)

chart = AreaChart()
chart.title = "Area chart"
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, min_row=1, max_col=3, max_row=7)
chart.add_data(data, titles_from_data=True)
chart.set_categories(cats)

ws.add_chart(chart, A10)

wb.save(2D area chart.xlsx)

3D面积图

from openpyxl import Workbook
from openpyxl.chart import(AreaChart3D, Reference)

wb = Workbook()
ws = wb.active

rows = [
    [number, batch1, batch2],
    [2, 40, 45],
    [3, 34, 23],
    [4, 34, 23],
    [5, 54, 24],
    [6, 56, 53],
    [7, 35, 26],
]

for row in rows:
    ws.append(row)

chart = AreaChart3D()
chart.title = "Area 3D chart"
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, min_row=1, max_col=3, max_row=7)
chart.add_data(data, titles_from_data=True)
chart.set_categories(cats)

ws.add_chart(chart, A10)

wb.save(3D area chart.xlsx)

直方图(垂直)条形图(垂直)/水平/堆叠)

from openpyxl import Workbook
from openpyxl.chart import(BarChart, Reference)
from copy import deepcopy

wb = Workbook()
ws = wb.active

rows = [
    [number, batch1, batch2],
    [2, 40, 45],
    [3, 34, 23],
    [4, 34, 23],
    [5, 54, 24],
    [6, 56, 53],
    [7, 35, 26],
]

for row in rows:
    ws.append(row)

chart1 = BarChart()
chart1.title = "Bar chart"
chart1.style = 10
chart1.type = col
chart1.x_axis.title = test number
chart1.y_axis.title = sample length

cats = Reference(ws, min_col=1, min_row=1, max_row=7)
data = Reference(ws, min_col=2, min_row=1, max_col=3, max_row=7)
chart1.add_data(data, titles_from_data=True)
chart1.set_categories(cats)

ws.add_chart(chart1, A10)

chart2 = deepcopy(chart1)
chart2.style = 11
chart2.type = bar
chart2.title = horizontal bar chart

ws.add_chart(chart2, G10)

chart3 = deepcopy(chart1)
chart3.style = 12
chart3.type = col
chart3.grouping = stacked
chart3.overlap = 100
chart3.title = stcked chart

ws.add_chart(chart3, A27)

chart4 = deepcopy(chart1)
chart4.style = 13
chart4.type = bar
chart4.grouping = percentStacked
chart4.overlap = 100
chart4.title = percent stacked chart

ws.add_chart(chart4, G27)

wb.save(3D area chart.xlsx)

3D柱状图

from openpyxl import Workbook
from openpyxl.chart import(BarChart3D, Reference)
from copy import deepcopy

wb = Workbook()
ws = wb.active

rows = [
    [number, batch1, batch2],
    [2, 40, 45],
    [3, 34, 23],
    [4, 34, 23],
    [5, 54, 24],
    [6, 56, 53],
    [7, 35, 26],
]

for row in rows:
    ws.append(row)

chart1 = BarChart3D()
chart1.title = "3D Bar chart"

cats = Reference(ws, min_col=1, min_row=1, max_row=7)
data = Reference(ws, min_col=2, min_row=1, max_col=3, max_row=7)
chart1.add_data(data, titles_from_data=True)
chart1.set_categories(cats)

ws.add_chart(chart1, A10)

wb.save(bar3d.xlsx)

气泡图

from openpyxl import Workbook
from openpyxl.chart import Series, Reference, BubbleChart

wb = Workbook()
ws = wb.active

rows = [
    ("Number of Products", "Sales in USD", "Market share"),
    (14, 12200, 15),
    (20, 60000, 33),
    (18, 24400, 10),
    (22, 32000, 42),
    (),
    (12, 8200, 18),
    (15, 50000, 30),
    (19, 22400, 15),
    (25, 25000, 50),
]

for row in rows:
    ws.append(row)

chart = BubbleChart()
chart.style = 18

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")
chart.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")
chart.series.append(series)

ws.add_chart(chart, E1)

wb.save(bubble.xlsx)

旅行图片赚鸡腿吃旅行图片赚鸡腿

版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除