English 中文(简体)
Python XlsxWriter - Line Chart
  • 时间:2024-12-22

Python XlsxWriter - Line Chart


Previous Page Next Page  

A pne shows a series of data points connected with a pne along the X-axis. It is an independent axis because the values on the X-axis do not depend on the vertical Y-axis.

The Y-axis is a dependent axis because its values depend on the X-axis and the result is the pne that progress horizontally.

Working with XlsxWriter Line Chart

To generate the pne chart programmatically using XlsxWriter, we use add_series(). The type of chart object is defined as pne .

Example

In the following example, we shall plot pne chart showing the sales figures of two products over six months. Two data series corresponding to sales figures of Product A and Product B are added to the chart with add_series() method.


import xlsxwriter

wb = xlsxwriter.Workbook( hello.xlsx )
worksheet = wb.add_worksheet()
headings = [ Month ,  Product A ,  Product B ]

data = [
   [ Jan ,  Feb ,  Mar ,  Apr ,  May ,  June ],
   [10, 40, 50, 20, 10, 50],
   [30, 60, 70, 50, 40, 30],
]

bold=wb.add_format({ bold :True})
worksheet.write_row( A1 , headings, bold)
worksheet.write_column( A2 , data[0])
worksheet.write_column( B2 , data[1])
worksheet.write_column( C2 , data[2])

chart1 = wb.add_chart({ type :  pne })

chart1.add_series({
    name :  =Sheet1!$B$1 ,
    categories :  =Sheet1!$A$2:$A$7 ,
    values :  =Sheet1!$B$2:$B$7 ,
})
chart1.add_series({
    name : [ Sheet1 , 0, 2],
    categories : [ Sheet1 , 1, 0, 6, 0],
    values : [ Sheet1 , 1, 2, 6, 2],
})
chart1.set_title ({ name :  Sales analysis })
chart1.set_x_axis({ name :  Months })
chart1.set_y_axis({ name :  Units })

worksheet.insert_chart( D2 , chart1)

wb.close()

Output

After executing the above program, here is how XlsxWriter generates the Line chart −

Sales Analysis

Along with data_labels, the add_series() method also has a marker property. This is especially useful in a pne chart. The data points are indicated by marker symbols such as a circle, triangle, square, diamond etc. Let us assign circle and square symbols to the two data series in this chart.


chart1.add_series({
    name :  =Sheet1!$B$1 ,
    categories :  =Sheet1!$A$2:$A$7 ,
    values :  =Sheet1!$B$2:$B$7 ,
    data_labels : { value : True},
    marker : { type :  circle },
})
chart1.add_series({
    name : [ Sheet1 , 0, 2],
    categories : [ Sheet1 , 1, 0, 6, 0],
    values : [ Sheet1 , 1, 2, 6, 2],
    data_labels : { value : True},
    marker : { type :  square },})

The data labels and markers are added to the pne chart.

Sales Analysis1

Line chart also supports stacked and percent_stacked subtypes.

Sales Analysis2 Advertisements