openpyxl_chart_type_demos(openpyxl 4:도표 형식)
93783 단어 파충류
"""
openpyxl_chart_type_demos.py
(openpyxl : )
:
:
#AreaChart 、BarChartm / / 、BubbleChart 、
#LineChart /LineChart3D 、
#ScatterChart 、PieChart /PieChart3D /ProjectedPieChart 、
#StockChart ;
"""
#################### openpyxl.chart
######### :
#AreaChart 、BarChartm / / 、BubbleChart 、
#LineChart /LineChart3D 、
#ScatterChart 、PieChart /PieChart3D /ProjectedPieChart 、
#StockChart
import openpyxl
from openpyxl import Workbook, load_workbook
from openpyxl.chart import (AreaChart, #AreaChart (2D )
AreaChart3D, #AreaChart (3D )
BarChart, #BarChart / /
BubbleChart, #BubbleChart
LineChart, #LineChart
LineChart3D, #LineChart3D
ScatterChart, #ScatterChart
PieChart, #PieChart
PieChart3D, #PieChart3D 3D
ProjectedPieChart, #ProjectedPieChart
StockChart, #StockChart
Reference, #
Series) #
from copy import deepcopy #
from openpyxl.chart.axis import DateAxis #
from openpyxl.chart.series import DataPoint #
from openpyxl.chart.layout import (Layout, #
ManualLayout #
)
wb=Workbook()
wb_filename=r'openpyxl_cases\openpyxl_chart_type_demos_case1.xlsx'
#AreaChart (2D )
#####
ws_AreaChart2D=wb.create_sheet('AreaChart 2D')
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_AreaChart2D.append(row)
#####
# :openpyxl.chart.AreaChart() 2D
# :Chart.title
# :Chart.style
# :Chart.x_axis.title X
# :Chart.y_axis.title y
chart=AreaChart() # 。
chart.title='Area Chart' #
chart.style=30 #
chart.x_axis.title='Test' # X
chart.y_axis.title='Percentage' # Y
# :openpyxl.chart.Reference(worksheet=None, min_col=None, min_row=None, max_col=None, max_row=None, range_string=None)
# 。
labels=Reference(ws_AreaChart2D, min_col=1, min_row=1, max_row=2) # , x
data=Reference(ws_AreaChart2D, min_col=2, min_row=1, max_col=3,max_row=7) # , 。
# :Chart.set_categories(self, labels) / x 。
# :Chart.add_data(self, data, from_rows=False, titles_from_data=False)
# 。 , 。
chart.set_categories(labels) # /X 。
chart.add_data(data, titles_from_data=True) # 。 , 。
# :Worksheet.add_chart(chart, anchor=None) , 。
ws_AreaChart2D.add_chart(chart,'A10') # ,
#AreaChart3D (3D )
#####
ws_AreaChart3D=wb.create_sheet('AreaChart 3D')
for row in rows:
ws_AreaChart3D.append(row)
#####
## :openpyxl.chart.AreaChart3D() 3D
chart=AreaChart3D() # 3D
chart.title='Area Chart' #
chart.style=13 #
chart.x_axis.title='Test' # X
chart.y_axis.title='Percentage' # Y
chart.legend=None #
labels=Reference(ws_AreaChart3D, min_col=1, min_row=1, max_row=7) # , x
data=Reference(ws_AreaChart3D, min_col=2, min_row=1, max_col=3, max_row=7) # , 。
chart.set_categories(labels) # /X
chart.add_data(data, titles_from_data=True) # 。 , 。
ws_AreaChart3D.add_chart(chart,'A10') # , 。
#BarChart
#####
ws_BarChart=wb.create_sheet('BarChart ',index=0)
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_BarChart.append(row)
#####
# :openpyxl.chart.BarChart() 、 、
# : , 。
# : type col bar, 。
# : , X Y 。
chart1=BarChart() #
chart1.title='Bar Chart' #
chart1.style=10 #
chart1.type='col' # ’col'
chart1.x_axis.title='Test Number' # X
chart1.y_axis.title='Sample length(mm)' # Y
labels=Reference(ws_BarChart, min_col=1, min_row=2, max_row=7) # , X 。
data=Reference(ws_BarChart, min_col=2, min_row=1, max_row=7, max_col=3) # , 。
chart1.set_categories(labels) # / X
chart1.add_data(data,titles_from_data=True) # , , 。
chart1.shape=4
ws_BarChart.add_chart(chart1, 'A10') # , 。
#
chart2=deepcopy(chart1) #
chart2.title='Horizontal Bar Chart' #
chart2.style=11 #
chart2.type='bar' # : ’bar'
ws_BarChart.add_chart(chart2, 'I10') # , 。
# 、
# : , overlap 100
# :Chart.grouping :{'standard', 'stacked', 'clustered', 'percentStacked'} 。
# 、 。
chart3=deepcopy(chart1) #
chart3.title='Stacked Chart' #
chart3.style=12 #
chart3.type='col' # : ‘col'
chart3.grouping='stacked' # :
chart3.overlap=100 # : overlap 100 。
ws_BarChart.add_chart(chart3, 'A27')
# 、
chart4=deepcopy(chart1) #
chart4.title='Percent Stacked Chart' #
chart4.style=13 #
chart4.type='bar' #
chart4.grouping='percentStacked' # : +
chart4.overlap=100 # : overlap 100 。
ws_BarChart.add_chart(chart4, 'I27') # , 。
#BubbleChart
#####
ws_BubbleChart=wb.create_sheet('BubbleChart ')
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_BubbleChart.append(row)
#####
# :openpyxl.chart.BubbleChart() BubbleChart
# :Series(values, xvalues=None, zvalues=None, title=None, title_from_data=False)
#
# values : y
# xvalues=None : x
# zvalues=None :
# title=None :
# :Chart.series.append(Series) Series 。
chart=BubbleChart() #
chart.style=18 #
#
xvalues=Reference(ws_BubbleChart, min_col=1, min_row=2, max_row=5) # , X :xvalues
yvalues=Reference(ws_BubbleChart, min_col=2, min_row=2, max_row=5) # , Y :values
size=Reference(ws_BubbleChart, min_col=3, min_row=2, max_row=5) # , :zvalues
series=Series(values=yvalues, xvalues=xvalues, zvalues=size, title='2013') # Series 。
chart.series.append(series) # Series 。
#
xvalues=Reference(ws_BubbleChart, min_col=1, min_row=7, max_row=10) # , X :xvalues
yvalues=Reference(ws_BubbleChart, min_col=2, min_row=7, max_row=10) # , Y :values
size=Reference(ws_BubbleChart, min_col=3, min_row=7, max_row=10) # , :zvalues
series=Series(values=yvalues, xvalues=xvalues, zvalues=size, title='2014') # Series 。
chart.series.append(series) # Series
ws_BubbleChart.add_chart(chart, 'E1') # , 。
#LineChart
#####
from datetime import date
ws_LineChart=wb.create_sheet('LineChart ')
rows=[
['Data', 'Batch1', 'Batch2', 'Batch3'],
[date(2015,9,1), 40, 30, 25],
[date(2015,9,2), 40, 25, 30],
[date(2015,9,3), 50, 30, 45],
[date(2015,9,4), 30, 25, 40],
[date(2015,9,5), 25, 35, 30],
[date(2015,9,6), 20, 40, 35]]
for row in rows:
ws_LineChart.append(row)
#####
# :openpyxl.chart.LineChart() LineChart
c1=LineChart() #
c1.title='Line Chart' #
c1.style=13 #
c1.x_axis.title='Test Number' # X
c1.y_axis.title='Size' # Y
data=Reference(ws_LineChart, min_col=2, max_col=4, min_row=1, max_row=7) # , 。
c1.add_data(data, titles_from_data=True) # 。
#
# :Chart.series 。
s1=c1.series[0] #
s1.marker.symbol='triangle' #
s1.marker.graphicalProperties.solidFill='FF0000' #
s1.marker.graphicalProperties.line.solidFill='FF0000' #
s1.graphicalProperties.line.noFill=True # 。
s2=c1.series[1]
s2.graphicalProperties.line.solifFill='00AAAA'
s2.graphicalProperties.line.dashStyle='sysDot'
s2.graphicalProperties.line.width=100050
s3=c1.series[2]
s3.smooth=True
ws_LineChart.add_chart(c1, 'A10')
stacked=deepcopy(c1)
stacked.grouping='stacked'
stacked.title='Stacked Line Chart' # {' percent', 'standard', ' '}
ws_LineChart.add_chart(stacked, 'A27')
percent_stacked=deepcopy(c1)
percent_stacked.grouping='percentStacked' # {' percent', 'standard', ' '}
percent_stacked.title='Percent Stacked Line Chart'
ws_LineChart.add_chart(percent_stacked, 'A44')
c4=LineChart()
c4.title='Date Axis'
c4.style=12
c4.y_axis.title='Size'
c4.y_axis.crossAx=500
c4.x_axis.title='Date'
c4.x_axis=DateAxis(crossAx=100)
c4.x_axis.number_format='d-mmm'
c4.x_axis.majorTimeUnit='days'
c4.add_data(data, titles_from_data=True)
dates=Reference(ws_LineChart, min_col=1, min_row=2, max_row=7)
c4.set_categories(dates)
ws_LineChart.add_chart(c4, 'A61')
#LineChart3D 3D
#####
ws_LineChart3D=wb.create_sheet('LineChart3D')
for row in rows:
ws_LineChart3D.append(row)
#####
# :openpyxl.chart.LineChart3D() LineChart3D 3D
c1=LineChart3D()
c1.title='3D Line Chart'
c1.legend=None
c1.style=15
c1.y_axis.title='Size'
c1.x_axis.title='Test Number'
data=Reference(ws_LineChart3D, min_col=2, max_col=4, min_row=1, max_row=7)
c1.add_data(data, titles_from_data=True)
ws_LineChart3D.add_chart(c1, 'A10')
#ScatterChart
#####
ws_ScatterChart=wb.create_sheet('ScatterChart ')
rows=[
['Size', 'Batch 1', 'Batch 3'],
[2, 40, 30],
[3, 40, 25],
[4, 50, 30],
[5, 30, 25],
[6, 25, 35],
[7, 20, 40]]
for row in rows:
ws_ScatterChart.append(row)
#####
# :openpyxl.chart.ScatterChart() ScatterChart
chart=ScatterChart()
chart.title='Scatter Chart'
chart.style=13
chart.x_axis.title='Size'
chart.y_axis.title='Percentage'
xvalues=Reference(ws_ScatterChart, min_col=1, min_row=2, max_row=7)
for i in range(2,4):
values=Reference(ws_ScatterChart, min_col=i, min_row=1, max_row=7)
series=Series(values, xvalues, title_from_data=True)
chart.series.append(series)
ws_ScatterChart.add_chart(chart, 'A10')
#PieChart
#####
ws_PieChart=wb.create_sheet('PieChart ')
data=[
['Pie', 'Sold'],
['Apple', 50],
['Cherry', 30],
['Pumpkin', 10],
['Chocolate', 40]]
for row in data:
ws_PieChart.append(row)
#####
# :openpyxl.chart.PieChart() PieChart
pie=PieChart()
labels=Reference(ws_PieChart, min_col=1, min_row=2, max_row=5)
data=Reference(ws_PieChart, min_col=2, min_row=1, max_row=5)
pie.add_data(data, titles_from_data=True)
pie.set_categories(labels)
pie.title='Pies sold by category'
slice=DataPoint(idx=0, explosion=20)
pie.series[0].data_points=[slice]
ws_PieChart.add_chart(pie, 'D1')
#PieChart3D 3D
#####
# :openpyxl.chart.PieChart3D() PieChart3D 3D
pie = PieChart3D()
labels = Reference(ws_PieChart, min_col=1, min_row=2, max_row=5)
data = Reference(ws_PieChart, min_col=2, min_row=1, max_row=5)
pie.add_data(data, titles_from_data=True)
pie.set_categories(labels)
pie.title = "Pies sold by category"
ws_PieChart.add_chart(pie, "D20")
#ProjectedPieChart
#####
# :openpyxl.chart.ProjectedPieChart() ProjectedPieChart
ws_ProjectedPieChart=wb.create_sheet('ProjectedPieChart ')
data=[
['Page', 'Views'],
['Search', 95],
['Products', 4],
['Offers', 0.5],
['Sales', 0.5]]
for row in data:
ws_ProjectedPieChart.append(row)
projected_pie=ProjectedPieChart()
projected_pie.type='pie'
projected_pie.splitType='val'
labels=Reference(ws_ProjectedPieChart, min_col=1, min_row=2, max_row=5)
data=Reference(ws_ProjectedPieChart, min_col=2, min_row=1, max_row=5)
projected_pie.add_data(data, titles_from_data=True)
projected_pie.set_categories(labels)
ws_ProjectedPieChart.add_chart(projected_pie, 'A10')
projected_bar=deepcopy(projected_pie)
projected_bar.type='bar'
projected_bar.splitType='pos'
ws_ProjectedPieChart.add_chart(projected_bar, 'A27')
#StockChart
#####
from openpyxl import Workbook
from openpyxl.chart import (StockChart, #StockChart
BarChart, #BarChart / /
Reference, #Reference : 。
Series
)
from openpyxl.chart.axis import (DateAxis,
ChartLines # : hiLoLines。K upDownLines。
)
from openpyxl.chart.updown_bars import UpDownBars # : hiLoLines。K upDownLines。
#wb=Workbook()
ws_StockChart=wb.create_sheet('StcokChart ',index=0)
rows=[
['Date', 'Valume', 'Open', 'High', 'Low', 'Close'],
['2015-01-01', 20000, 26.2, 27.2, 23.49, 25.45],
['2015-01-02', 10000, 25.45, 25.03, 19.55, 23.05],
['2015-01-03', 15000, 25.45, 25.03, 19.55, 23.05],
['2015-01-04', 2000, 22.42, 23.97, 20.07, 21.90],
['2015-01-05', 12000, 21.9, 23.65, 19.50, 21.51]]
for row in rows:
ws_StockChart.append(row)
# ( - - )
#####
# :openpyxl.chart.StockChart() StockChart
# :openpyxl.chart.Reference(worksheet=None, min_col=None, min_row=None, max_col=None, max_row=None, range_string=None)
# 。
# :Chart.add_data(self, data, from_rows=False, titles_from_data=False)
# 。 , 。
# :Chart.set_categories(self, labels) / x 。
# : - - 。
# : XYZ, hiLoLines。K upDownLines。
c1=StockChart() #
c1.title='High-low-close'
c1.hiLowLines=ChartLines() # : XYZ, hiLoLines。K upDownLines。
labels=Reference(ws_StockChart, min_col=1, min_row=2, max_row=6)
data=Reference(ws_StockChart, min_col=4, max_col=6, min_row=1, max_row=6)
c1.add_data(data, titles_from_data= True)
c1.set_categories(labels)
for s in c1.series:
s.graphicalProperties.line.noFill= True
s.marker.symbol='dot'
s.marker.size=5
# : Excel BUG , , / 。
# :
from openpyxl.chart.data_source import (NumData,
NumVal)
pts=[NumVal(idx=i) for i in range(len(data) -1)]
cache=NumData(pt=pts)
c1.series[-1].val.numRef.numCache = cache
ws_StockChart.add_chart(c1, 'A10')
# K ( - - - )
#####
# : - - - - - 。
# : XYZ, hiLoLines。K upDownLines。
c2=StockChart()
c2.title='Open-hight-low-close'
c2.hiLowLines=ChartLines() # : XYZ, hiLoLines。K upDownLines。
c2.upDownBars=UpDownBars() # : XYZ, hiLoLines。K upDownLines。
labels=Reference(ws_StockChart, min_col=1, min_row=2, max_row=6)
data=Reference(ws_StockChart, min_col=3, max_col=6, min_row=1, max_row=6)
c2.add_data(data, titles_from_data=True)
c2.set_categories(labels)
for s in c2.series:
s.graphicalProperties.line.noFill = True
# : Excel BUG , , / 。
# :
from openpyxl.chart.data_source import (NumData,
NumVal)
pts=[NumVal(idx=i) for i in range(len(data) -1)]
cache=NumData(pt=pts)
c2.series[-1].val.numRef.numCache = cache
ws_StockChart.add_chart(c2, 'I10')
# + ( + )
bar=BarChart()
data = Reference(ws_StockChart, min_col=2, min_row=1, max_row=6)
bar.add_data(data, titles_from_data=True)
bar.set_categories(labels)
b1=deepcopy(bar)
c3=deepcopy(c1)
c3.y_axis.majorGridlines = None
c3.y_axis.title='Price'
b1.y_axis.axId=20
b1.z_axis = c3.y_axis
b1.y_axis.crosses = 'max'
b1 += c3
c3.title='High low close volume'
ws_StockChart.add_chart(b1, 'A27')
# K + ( - - - - )
b2 = deepcopy(bar)
c4 = deepcopy(c2)
c4.y_axis.majorGridlines = None
c4.y_axis.title = 'Price'
b2.y_axis.axId = 20
b2.z_axis = c4.y_axis
b2.y_axis.crosses = 'max'
b2 += c4
ws_StockChart.add_chart(b2, 'I27')
wb.save(wb_filename)
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
python은 어떻게 파충류의 효율을 향상시킬 것인가단일 스레드 + 멀티태스킹 비동기 협동 협정 함수(특수 함수)를 정의할 때 async 수식을 사용합니다. 함수 호출 후 내부 문장은 즉시 실행되지 않고 협동 대상으로 되돌아옵니다. 퀘스트 대상 작업 대상 = 고급 협...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.