Python | Plotting Different types of style charts in excel sheet using XlsxWriter module
Last Updated :
26 Dec, 2018
Prerequisite: Create and Write on an excel sheet
XlsxWriter
is a Python library using which one can perform multiple operations on excel files like creating, writing, arithmetic operations and plotting graphs. Let’s see how to plot different types of Style charts, using realtime data.
Charts are composed of at least one series of one or more data points. Series themselves are comprised of references to cell ranges. For plotting the charts on an excel sheet, firstly, create chart object of specific chart type( i.e Line, Column chart etc.). After creating chart objects, insert data in it and lastly, add that chart object in the sheet object.
Code : Plot different types of style charts.
For plotting different types of style charts on an excel sheet, use set_style()
method of the chart object with respective style id .
import xlsxwriter
workbook = xlsxwriter.Workbook( 'chart_styles.xlsx' )
chart_types = [ 'column' , 'area' ]
for chart_type in chart_types:
worksheet = workbook.add_worksheet(chart_type.title())
worksheet.set_zoom( 30 )
style_number = 1
for row_num in range ( 0 , 90 , 15 ):
for col_num in range ( 0 , 64 , 8 ):
chart = workbook.add_chart({ 'type' : chart_type})
chart.add_series({ 'values' : '= Data !$A$1:$A$6' })
chart.set_title ({ 'name' : 'Style % d' % style_number})
chart.set_legend({ 'none' : True })
chart.set_style(style_number)
worksheet.insert_chart(row_num, col_num, chart)
style_number + = 1
data_worksheet = workbook.add_worksheet( 'Data' )
data = [ 10 , 40 , 50 , 20 , 10 , 50 ]
data_worksheet.write_column( 'A1' , data)
data_worksheet.hide()
workbook.close()
|
Output :
Share your thoughts in the comments
Please Login to comment...