- Python XlsxWriter - VBA Macro
- Python XlsxWriter - Working with Pandas
- Python XlsxWriter - Cell Comments
- Python XlsxWriter - Header & Footer
- Python XlsxWriter - Page Setup
- Python XlsxWriter - Insert Image
- Python XlsxWriter - Textbox
- Python XlsxWriter - Hide/Protect Worksheet
- Python XlsxWriter - Freeze & Split Panes
- Python XlsxWriter - Outlines & Grouping
- Python XlsxWriter - Data Validation
- Python XlsxWriter - Sparklines
- Python XlsxWriter - Pie Chart
- Python XlsxWriter - Line Chart
- Python XlsxWriter - Bar Chart
- Python XlsxWriter - Chart Legends
- Python XlsxWriter - Chart Formatting
- Python XlsxWriter - Adding Charts
- Python XlsxWriter - Conditional Formatting
- Python XlsxWriter - Hyperlinks
- Python XlsxWriter - Border
- Python XlsxWriter - Number Formats
- Python XlsxWriter - Fonts & Colors
- Python XlsxWriter - Applying Filter
- Python XlsxWriter - Tables
- Python XlsxWriter - Date and Time
- Python XlsxWriter - Formula & Function
- Python XlsxWriter - Defined Names
- Python XlsxWriter - Cell Notation & Ranges
- Python XlsxWriter - Important classes
- Python XlsxWriter - Hello World
- Python XlsxWriter - Environment Setup
- Python XlsxWriter - Overview
- Python XlsxWriter - Home
Python XlsxWriter Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Python XlsxWriter - Chart Legends
Depending upon the type of chart, the data is visually represented in the form of columns, bars, pnes, 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
To set the legend and configure its properties such as position and font, XlsxWriter has set_legend() method. The properties are −
None − In Excel chart legends are on by default. The none=True option turns off the chart legend.
Position − Set the position of the chart legend. It can be set to top, bottom, left, right, none.
Font − Set the font properties (pke name, size, bold, itapc etc.) of the chart legend.
Border − Set the border properties of the legend such as color and style.
Fill − Set the sopd fill properties of the legend such as color.
Pattern − Set the pattern fill properties of the legend.
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 : capbri , 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 : Markpst , 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 , itapc :True}}) chart1.set_legend({ position : bottom , font : { name : capbri , size : 9, bold : True}}) worksheet.insert_chart( B7 , chart1) wb.close()
Output
The chart shows the legend below the caption of the X axis.
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.
Advertisements