Python Xlsxwriter 简明教程

Python XlsxWriter - Line Chart

折线图直观显示了一系列数据点,这些数据点通过一条直线在 X 轴上连接起来。这是一条独立轴,因为 X 轴上的值不依赖于纵向 Y 轴。

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

Y 轴是一条从属轴,因为其值依赖于 X 轴,结果是水平延伸的折线。

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

Working with XlsxWriter Line Chart

要使用 XlsxWriter 以编程方式生成 line chart ,我们使用 add_series() 。图表对象类型定义为“ line ”。

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

Example

在以下示例中,我们绘制 line chart ,显示了六个月内两种产品的销售数字。通过 add_series() 方法将对应于产品 A 和产品 B 的销售数字的两个数据系列添加到图表中。

In the following example, we shall plot line 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': 'line'})

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

执行以上程序后,以下是如何:XlsxWriter 生成折线图 −

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

sales analysis

除了 data_labelsadd_series() 方法还具有 marker 属性。这在折线图中尤其有用。数据点由圆圈、三角形、正方形、菱形等标记符号表示。让我们将 circlesquare 符号指定给此图表中的两个数据系列。

Along with data_labels, the add_series() method also has a marker property. This is especially useful in a line 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 line chart.

sales analysis1

折线图还支持 stackedpercent_stacked 子类型。

Line chart also supports stacked and percent_stacked subtypes.

sales analysis2