Python Xlsxwriter 简明教程

Python XlsxWriter - Chart Legends

根据图表类型,数据以不同颜色或图案的形式在柱状图、条形图、折线图、圆弧图等形式中进行视觉呈现。图表图例能够轻松直观地显示哪种颜色/图案对应于哪种数据系列。

Depending upon the type of chart, the data is visually represented in the form of columns, bars, lines, arcs, etc. in different colors or patterns. The chart legend makes it easy to quickly understand which color/pattern corresponds to which data series.

Working with Chart Legends

为了设置图例并配置其属性,例如位置和字体,XlsxWriter 提供了 set_legend() 方法。其属性为 −

To set the legend and configure its properties such as position and font, XlsxWriter has set_legend() method. The properties are −

  1. None − In Excel chart legends are on by default. The none=True option turns off the chart legend.

  2. Position − Set the position of the chart legend. It can be set to top, bottom, left, right, none.

  3. Font − Set the font properties (like name, size, bold, italic etc.) of the chart legend.

  4. Border − Set the border properties of the legend such as color and style.

  5. Fill − Set the solid fill properties of the legend such as color.

  6. Pattern − Set the pattern fill properties of the legend.

  7. Gradient − Set the gradient fill properties of the legend.

某些图例属性可用于图表,如下所示 −

Some of the legend properties are set for the chart as below −

chart1.set_legend(
   {'position':'bottom', 'font': {'name':'calibri','size': 9, 'bold': True}}
)

Example

以下是按上述特性显示图例的完整代码 −

Here is the complete code to display legends as per the above characteristics −

import xlsxwriter

wb = xlsxwriter.Workbook('hello.xlsx')
worksheet = wb.add_worksheet()
chart1 = wb.add_chart({'type': 'column'})

# Add the worksheet data that the charts will refer to.
headings = ['Name', 'Phy', 'Maths']
data = [
   ["Jay", 30, 60],
   ["Mohan", 40, 50],
   ["Veeru", 60, 70],
]

worksheet.write_row(0,0, headings)
worksheet.write_row(1,0, data[0])
worksheet.write_row(2,0, data[1])
worksheet.write_row(3,0, data[2])
chart1.add_series({
   'name': '=Sheet1!$B$1',
   'categories': '=Sheet1!$A$2:$A$4',
   'values': '=Sheet1!$B$2:$B$4',
})
chart1.add_series({
   'name': ['Sheet1', 0, 2],
   'categories': ['Sheet1', 1, 0, 3, 0],
   'values': ['Sheet1', 1, 2, 3, 2],
})
chart1.set_title ({'name': 'Marklist', 'name_font':
   {'name':'Times New Roman', 'size':24}})

chart1.set_x_axis({'name': 'Students', 'name_font':
   {'name':'Arial', 'size':16, 'bold':True},})

chart1.set_y_axis({'name': 'Marks','name_font':
   {'name':'Arial', 'size':16, 'bold':True},
   'num_font':{'name':'Arial', 'italic':True}})

chart1.set_legend({'position':'bottom', 'font':
   {'name':'calibri','size': 9, 'bold': True}})

worksheet.insert_chart('B7', chart1)

wb.close()

Output

图表在 X 轴标题的下方显示图例。

The chart shows the legend below the caption of the X axis.

legend

在图表中,对应于 physicsmaths 的列以不同的颜色显示。图表右侧的小色块符号是图例,用来显示哪种颜色对应于 physicsmaths

In the chart, the columns corresponding to physics and maths are shown in different colors. The small colored box symbols to the right of the chart are the legends that show which color corresponds to physics or maths.