Python Xlsxwriter 简明教程
Python XlsxWriter - Chart Formatting
可以自定义图表的默认外观使其更具吸引力、解释性和用户友好性。使用 XlsxWriter ,我们可以对图表对象进行以下增强 −
The default appearance of chart can be customized to make it more appealing, explanatory and user friendly. With XlsxWriter, we can do following enhancements to a Chart object −
-
Set and format chart title
-
Set the X and Y axis titles and other parameters
-
Configure the chart legends
-
Chat layout options
-
Setting borders and patterns
Title
您可以通过调用图表对象的 set_title() 方法来设置和配置图表对象的标题。可以是各种参数如下 −
You can set and configure the main title of a chart object by calling its set_title() method. Various parameters that can be are as follows −
-
Name − Set the name (title) for the chart to be displayed above the chart. The name property is optional. The default is to have no chart title.
-
name_font − Set the font properties for the chart title.
-
Overlay − Allow the title to be overlaid on the chart.
-
Layout − Set the (x, y) position of the title in chart relative units.
-
None − Excel adds an automatic chart title. The none option turns this default title off. It also turns off all other set_title() options.
X and Y axis
两种方法 set_x_axis() 和 set_y_axis() 用于轴标题, name_font 用于标题文本, num_font 用于 X 和 Y 轴上显示的数字。
The two methods set_x_axis() and set_y_axis() are used to axis titles, the name_font to be used for the title text, the num_font to be used for numbers displayed on the X and Y axis.
-
name − Set the title or caption for the axis.
-
name_font − Set the font properties for the axis title.
-
num_font − Set the font properties for the axis numbers.
-
num_format − Set the number format for the axis.
-
major_gridlines − Configure the major gridlines for the axis.
-
display_units − Set the display units for the axis.
在前一个示例中,marklist 的数据已显示为柱形图形式,我们设置了图表格式化选项(例如图表标题和 X/Y 轴标题及其其他显示属性),如下所示: −
In the previous example, where the data of marklist has been shown in the form of a column chart, we set up the chart formatting options such as the chart title and X as well as Y axis captions and their other display properties as follows −
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}
}
)
Example
在完整代码中添加以上代码段。现在如下所示: −
Add the above snippet in the complete code. It now looks as given below −
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}
})
worksheet.insert_chart('B7', chart1)
wb.close()