- Python XlsxWriter Tutorial
- Python XlsxWriter - Home
- Python XlsxWriter - Overview
- Python XlsxWriter - Environment Setup
- Python XlsxWriter - Hello World
- Python XlsxWriter - Important classes
- Python XlsxWriter - Cell Notation & Ranges
- Python XlsxWriter - Defined Names
- Python XlsxWriter - Formula & Function
- Python XlsxWriter - Date and Time
- Python XlsxWriter - Tables
- Python XlsxWriter - Applying Filter
- Python XlsxWriter - Fonts & Colors
- Python XlsxWriter - Number Formats
- Python XlsxWriter - Border
- Python XlsxWriter - Hyperlinks
- Python XlsxWriter - Conditional Formatting
- Python XlsxWriter - Adding Charts
- Python XlsxWriter - Chart Formatting
- Python XlsxWriter - Chart Legends
- Python XlsxWriter - Bar Chart
- Python XlsxWriter - Line Chart
- Python XlsxWriter - Pie Chart
- Python XlsxWriter - Sparklines
- Python XlsxWriter - Data Validation
- Python XlsxWriter - Outlines & Grouping
- Python XlsxWriter - Freeze & Split Panes
- Python XlsxWriter - Hide/Protect Worksheet
- Python XlsxWriter - Textbox
- Python XlsxWriter - Insert Image
- Python XlsxWriter - Page Setup
- Python XlsxWriter - Header & Footer
- Python XlsxWriter - Cell Comments
- Python XlsxWriter - Working with Pandas
- Python XlsxWriter - VBA Macro
- Python XlsxWriter Useful Resources
- Python XlsxWriter - Quick Guide
- Python XlsxWriter - Useful Resources
- Python XlsxWriter - Discussion
Python XlsxWriter - Bar Chart
The bar chart is similar to a column chart, except for the fact that the data is represented in proportionate horizontal bars instead of vertical columns. To produce a bar chart, the type argument of add_chart() method must be set to 'bar'.
chart1 = workbook.add_chart({'type': 'bar'})
The bar chart appears as follows −
There are two subtypes of bar chart, namely stacked and percent_stacked. In the stacked chart, the bars of different colors for a certain category are placed one after the other. In a percent_stacked chart, the length of each bar shows its percentage in the total value in each category.
chart1 = workbook.add_chart({ 'type': 'bar', 'subtype': 'percent_stacked' })
Example
Program to generate percent stacked bar chart is given below −
import xlsxwriter wb = xlsxwriter.Workbook('hello.xlsx') worksheet = wb.add_worksheet() chart1 = wb.add_chart({'type': 'bar', 'subtype': 'percent_stacked'}) # 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
The output file will look like the one given below −
Advertisements
To Continue Learning Please Login
Login with Google