언어/Python

Python Excel chart 사용

darkdevilness 2017. 2. 22. 01:54
728x90
참조 : http://xlsxwriter.readthedocs.io/working_with_charts.html

Code :

'''

Created on 2017. 2. 21.


@author: HOME

'''

from enum import auto


if __name__ == '__main__':

    pass


import xlsxwriter


workbook = xlsxwriter.Workbook('chart_line.xlsx')

worksheet = workbook.add_worksheet()

bold = workbook.add_format({'bold': 1})


# Add the worksheet data that the charts will refer to.

headings = ['Number', 'Batch 1', 'Batch 2', 'Batch 3']

headings1 = ['Number1', 'Batch 11', 'Batch 22', 'Batch 33']

data = [

    [2, 3, 4, 5, 6, 7],

    [10, 40, 50, 20, 10, 50],

    [30, 60, 70, 50, 40, 30],

    [20, 50, 60, 35, 25, 40],

]


worksheet.write_row('A1', headings, bold)

worksheet.write_row('A2', headings1, bold)

worksheet.write_column('A3', data[0])

worksheet.write_column('B3', data[1])

worksheet.write_column('C3', data[2])

worksheet.write_column('D3', data[3])


# Create a new chart object. In this case an embedded chart.

chart1 = workbook.add_chart({'type': 'scatter'})

#chart1 = workbook.add_chart({'type': 'line'})

# Configure the first series.

chart1.add_series({

    'name'      : '=Sheet1!$B$1:$B$2',

    'categories': '=Sheet1!$A$3:$A$8',

    'values'    : '=Sheet1!$B$3:$B$8',

    'line'      : {

        #'none'   : 'False',

        'color'  : 'brown',

        'width'  : 0.5,

        'dash_type': 'solid',

    },

    'marker': {

        'type'   : 'none',

        'size,'  : 2,

        'border' : {'color': 'red'},

        'fill'   : {'color': 'yellow'}

    },

    'smooth'    :     True,                      

})


# Configure second series. Note use of alternative syntax to define ranges.

chart1.add_series({

    'name'      : '=Sheet1!$C$1:$C$2',

    #'name'      : ['Sheet1', 0, 2, 1, 2],

    'categories': ['Sheet1', 2, 0, 7, 0],

    'values'    : ['Sheet1', 2, 2, 7, 2],

    'line'      : {

        #'none'   : 'False',

        'color'  : 'blue',

        'width'  : 1.25,

        'dash_type': 'solid',

    },

    'marker': {

        'type'   : 'square',

        'size,'  : 2,

        'border' : {'color': 'red'},

        'fill'   : {'color': 'yellow'}

    }, 

    'smooth'    :     True,                   

})


# Configure third series. Note use of alternative syntax to define ranges.

chart1.add_series({

    'name'      : ['Sheet1', 0, 3, 1, 3],

    'categories': ['Sheet1', 2, 0, 7, 0],

    'values'    : ['Sheet1', 2, 3, 7, 3],

    'line'      : {

        #'none'   : 'False',

        'color'  : 'orange',

        'width'  : 0.5,

        'dash_type': 'dash',

    },

    'marker': {

        'type'   : 'automatic',

        'size,'  : 5,

        'border' : {'color': 'orange'},

        'fill'   : {'color': 'orange'}

    },      

    'smooth'    :     True,             

})


# Add a chart title and some axis labels.

chart1.set_title ({'name': 'Results of sample analysis'})

chart1.set_x_axis({'name': 'Test number',

                   #'min': 0, 

                   #'max': 8

                   })


chart1.set_y_axis({'name': 'Sample length (mm)',

                   #'min': 0, 

                   #'max': 100

                   })


# Set an Excel chart style. Colors with white outline and shadow.

chart1.set_style(10)


# Insert the chart into the worksheet (with an offset).

worksheet.insert_chart('B4', chart1, {'x_offset': 25, 'y_offset': 10})

workbook.close()





728x90