The Wayback Machine - https://web.archive.org/web/20241009153236/https://www.geeksforgeeks.org/python-plotting-charts-in-excel-sheet-using-openpyxl-module-set-3/
Open In App

Python | Plotting charts in excel sheet using openpyxl module | Set 3

Last Updated : 19 May, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Prerequisite : Plotting charts in excel sheet using openpyxl module Set – 1 | Set – 2
Openpyxl is a Python library using which one can perform multiple operations on excel files like reading, writing, arithmetic operations and plotting graphs. 
Charts are composed of at least one series of one or more data points. Series themselves are comprised of references to cell ranges. Let’s see how to plot Doughnot, Radar, Surface, 3D Surface Chart on an excel sheet using openpyxl.
For plotting the charts on an excel sheet, firstly, create chart object of specific chart class( i.e SurfaceChart, RadarChart etc.). After creating chart objects, insert data in it and lastly, add that chart object in the sheet object. Let’s see how to plot different charts using realtime data.
Code #1 : Plot the Doughnut Chart
Doughnut charts are similar to pie charts except that they use a ring instead of a circle. They can also plot several series of data as concentric rings. For plotting the Doughnut chart on an excel sheet, use DoughnutChart class from openpyxl.chart submodule.
 

Python3




# import Workbook from openpyxl
from openpyxl import Workbook
 
# import DoughnutChart, Reference from openpyxl.chart sub_module .
from openpyxl.chart import DoughnutChart, Reference
 
# import DataPoint from openpyxl.chart.series class
from openpyxl.chart.series import DataPoint
 
# Call a Workbook() function of openpyxl
# to create a new blank Workbook object
wb = Workbook()
 
# Get workbook active sheet
# from the active attribute.
ws = wb.active
 
# data given
data = [
    ['Pie', 2014],
    ['Plain', 40],
    ['Jam', 2],
    ['Lime', 20],
    ['Chocolate', 30],
]
 
# write content of each row in 1st and 2nd
# column of the active sheet respectively .
for row in data:
    ws.append(row)
 
# Create object of DoughnutChart class
chart = DoughnutChart()
 
# create data for plotting
labels = Reference(ws, min_col = 1, min_row = 2, max_row = 5)
data = Reference(ws, min_col = 2, min_row = 1, max_row = 5)
 
# adding data to the Doughnut chart object
chart.add_data(data, titles_from_data = True)
 
# set labels in the chart object
chart.set_categories(labels)
 
# set the title of the chart
chart.title = "Doughnuts Chart"
 
# set style of the chart
chart.style = 26
 
# add chart to the sheet
# the top-left corner of a chart
# is anchored to cell E1 .
ws.add_chart(chart, "E1")
 
# save the file
wb.save("doughnut.xlsx")


Output: 
 

  
Code #2: Plot the Radar Chart
Data that is arranged in columns or rows on a worksheet can be plotted in a radar chart. Radar charts compare the aggregate values of multiple data series. It is effectively a projection of an area chart on a circular x-axis. For plotting the Radar chart on an excel sheet, use RadarChart class from openpyxl.chart submodule.
 

Python3




# import Workbook from openpyxl
from openpyxl import Workbook
 
# import RadarChart, Reference from openpyxl.chart sub_module .
from openpyxl.chart import RadarChart, Reference
 
# Call a Workbook() function of openpyxl
# to create a new blank Workbook object
wb = Workbook()
 
# Get workbook active sheet
# from the active attribute.
ws = wb.active
 
# data given
data = [
    ['Month', "Bulbs", "Seeds", "Flowers", "Trees & shrubs"],
    ['Jan', 0, 2500, 500, 0, ],
    ['Feb', 0, 5500, 750, 1500],
    ['Mar', 0, 9000, 1500, 2500],
    ['Apr', 0, 6500, 2000, 4000],
    ['May', 0, 3500, 5500, 3500],
    ['Jun', 0, 0, 7500, 1500],
    ['Jul', 0, 0, 8500, 800],
    ['Aug', 1500, 0, 7000, 550],
    ['Sep', 5000, 0, 3500, 2500],
    ['Oct', 8500, 0, 2500, 6000],
    ['Nov', 3500, 0, 500, 5500],
    ['Dec', 500, 0, 100, 3000 ],
]
 
# write content of each row in 1st and 2nd
# column of the active sheet respectively .
for row in data:
    ws.append(row)
 
# Create object of RadarChart class
chart = RadarChart()
 
# filled type of radar chart
chart.type = "filled"
 
# create data for plotting
labels = Reference(ws, min_col = 1, min_row = 2, max_row = 13)
data = Reference(ws, min_col = 2, max_col = 5, min_row = 2, max_row = 13)
 
# adding data to the Radar chart object
chart.add_data(data, titles_from_data = True)
 
# set labels in the chart object
chart.set_categories(labels)
 
# set the title of the chart
chart.title = "Radar Chart"
 
# set style of the chart
chart.style = 26
 
# delete y axis from the chart
chart.y_axis.delete = True
 
# add chart to the sheet
# the top-left corner of a chart
# is anchored to cell G2 .
ws.add_chart(chart, "G2")
 
# save the file
wb.save("Radar.xlsx")


Output: 
 

  
Code #3 : Plot The Surface Chart
Data that is arranged in columns or rows on a worksheet can be plotted in a surface chart. A surface chart is useful when you want to find optimum combinations between two sets of data. As in a topographic map, colors and patterns indicate areas that are in the same range of values. For plotting the Surface chart on an excel sheet, use SurfaceChart class from openpyxl.chart submodule.
 

Python3




# import Workbook from openpyxl
from openpyxl import Workbook
 
# import SurfaceChart, Reference, Series from openpyxl.chart sub_module .
from openpyxl.chart import SurfaceChart, Reference, Series
 
# Call a Workbook() function of openpyxl
# to create a new blank Workbook object
wb = Workbook()
 
# Get workbook active sheet
# from the active attribute.
ws = wb.active
 
# given data
data = [
    [None, 10, 20, 30, 40, 50, ],
    [0.1, 15, 65, 105, 65, 15, ],
    [0.2, 35, 105, 170, 105, 35, ],
    [0.3, 55, 135, 215, 135, 55, ],
    [0.4, 75, 155, 240, 155, 75, ],
    [0.5, 80, 190, 245, 190, 80, ],
    [0.6, 75, 155, 240, 155, 75, ],
    [0.7, 55, 135, 215, 135, 55, ],
    [0.8, 35, 105, 170, 105, 35, ],
    [0.9, 15, 65, 105, 65, 15],
]
 
# write content of each row in 1st and 2nd
# column of the active sheet respectively .
for row in data:
    ws.append(row)
 
# Create object of SurfaceChart class
chart = SurfaceChart()
 
# create data for plotting
labels = Reference(ws, min_col = 1, min_row = 2, max_row = 10)
data = Reference(ws, min_col = 2, max_col = 6, min_row = 1, max_row = 10)
 
# adding data to the Surface chart object
chart.add_data(data, titles_from_data = True)
 
# set labels in the chart object
chart.set_categories(labels)
 
# set the title of the chart
chart.title = "Surface Chart"
 
# set style of the chart
chart.style = 26
 
# add chart to the sheet
# the top-left corner of a chart
# is anchored to cell H2 .
ws.add_chart(chart, "H2")
 
# save the file
wb.save("Surface.xlsx")


Output: 
 

Code #4 : Plot the Surface 3D chart
For plotting the 3D Surface chart on an excel sheet, use SurfaceChart3D class from openpyxl.chart submodule.
 

Python3




# import Workbook from openpyxl
from openpyxl import Workbook
 
# import SurfaceChart3D, Reference, Series from openpyxl.chart sub_module .
from openpyxl.chart import SurfaceChart3D, Reference, Series
 
# Call a Workbook() function of openpyxl
# to create a new blank Workbook object
wb = Workbook()
 
# Get workbook active sheet
# from the active attribute.
ws = wb.active
 
# given data
data = [
    [None, 10, 20, 30, 40, 50, ],
    [0.1, 15, 65, 105, 65, 15, ],
    [0.2, 35, 105, 170, 105, 35, ],
    [0.3, 55, 135, 215, 135, 55, ],
    [0.4, 75, 155, 240, 155, 75, ],
    [0.5, 80, 190, 245, 190, 80, ],
    [0.6, 75, 155, 240, 155, 75, ],
    [0.7, 55, 135, 215, 135, 55, ],
    [0.8, 35, 105, 170, 105, 35, ],
    [0.9, 15, 65, 105, 65, 15],
]
 
# write content of each row in 1st and 2nd
# column of the active sheet respectively .
for row in data:
    ws.append(row)
 
# Create object of SurfaceChart3D class
chart = SurfaceChart3D()
 
# create data for plotting
labels = Reference(ws, min_col = 1, min_row = 2, max_row = 10)
data = Reference(ws, min_col = 2, max_col = 6, min_row = 1, max_row = 10)
 
# adding data to the Surface chart 3D object
chart.add_data(data, titles_from_data = True)
 
# set labels in the chart object
chart.set_categories(labels)
 
# set the title of the chart
chart.title = "Surface Chart 3D"
 
# set style of the chart
chart.style = 26
 
# add chart to the sheet
# the top-left corner of a chart
# is anchored to cell H2 .
ws.add_chart(chart, "H2")
 
# save the file
wb.save("Surface3D.xlsx")


Output : 
 

 



Similar Reads

Python | Plotting charts in excel sheet using openpyxl module | Set - 1
Prerequisite: Reading & Writing to excel sheet using openpyxl Openpyxl is a Python library using which one can perform multiple operations on excel files like reading, writing, arithmetic operations and plotting graphs. Let's see how to plot different charts using realtime data. Charts are composed of at least one series of one or more data poi
6 min read
Python | Plotting charts in excel sheet using openpyxl module | Set – 2
Prerequisite: Python | Plotting charts in excel sheet using openpyxl module | Set – 1 Openpyxl is a Python library using which one can perform multiple operations on excel files like reading, writing, arithmetic operations and plotting graphs. Charts are composed of at least one series of one or more data points. Series themselves are comprised of
6 min read
Python | Plotting charts in excel sheet with Data Tools using XlsxWriter module | Set - 1
Prerequisite: Create and Write on an excel sheetXlsxWriter is a Python library using which one can perform multiple operations on excel files like creating, writing, arithmetic operations and plotting graphs. Let’s see how to plot charts with different types of Data Tools using realtime data. Charts are composed of at least one series of one or mor
7 min read
Python | Plotting charts in excel sheet with data tools using XlsxWriter module | Set – 2
Prerequisite: Create and Write on an excel sheetXlsxWriter is a Python library using which one can perform multiple operations on excel files like creating, writing, arithmetic operations and plotting graphs. Let’s see how to plot charts with different types of Data Tools using realtime data.Charts are composed of at least one series of one or more
7 min read
Python | Plotting Area charts in excel sheet using XlsxWriter module
Prerequisite: Create and write on an excel file XlsxWriter is a Python library using which one can perform multiple operations on excel files like creating, writing, arithmetic operations and plotting graphs. Let’s see how to plot different charts using realtime data. Charts are composed of at least one series of one or more data points. Series the
6 min read
Python | Plotting bar charts in excel sheet using XlsxWriter module
Prerequisite: Create and Write on an excel file.XlsxWriter is a Python library using which one can perform multiple operations on excel files like creating, writing, arithmetic operations and plotting graphs. Let’s see how to plot different type of Bar charts using realtime data. Charts are composed of at least one series of one or more data points
6 min read
Python | Plotting Radar charts in excel sheet using XlsxWriter module
Prerequisite:Create and Write on excel file XlsxWriter is a Python library using which one can perform multiple operations on excel files like creating, writing, arithmetic operations and plotting graphs. Let’s see how to plot different type of Radar charts using realtime data. Charts are composed of at least one series of one or more data points.
6 min read
Python | Plotting scatter charts in excel sheet using XlsxWriter module
Prerequisite: Create and Write on an excel file. XlsxWriter is a Python library using which one can perform multiple operations on excel files like creating, writing, arithmetic operations and plotting graphs. Let’s see how to plot different type of Scatter charts using realtime data. Charts are composed of at least one series of one or more data p
10 min read
Python | Plotting column charts in excel sheet using XlsxWriter module
Prerequisite: Create and Write on an excel file.XlsxWriter is a Python library using which one can perform multiple operations on excel files like creating, writing, arithmetic operations and plotting graphs. Let’s see how to plot different type of Column charts using realtime data. Charts are composed of at least one series of one or more data poi
7 min read
Python | Plotting Pie charts in excel sheet using XlsxWriter module
Prerequisite: Create and Write on an excel sheet XlsxWriter is a Python library using which one can perform multiple operations on excel files like creating, writing, arithmetic operations and plotting graphs. Let’s see how to plot different types of pie charts using realtime data. Charts are composed of at least one series of one or more data poin
5 min read
Python | Plotting Doughnut charts in excel sheet using XlsxWriter module
Prerequisite: Create and Write on an excel sheet XlsxWriter is a Python library using which one can perform multiple operations on excel files like creating, writing, arithmetic operations and plotting graphs. Let’s see how to plot different variety of doughnut charts using realtime data. Charts are composed of at least one series of one or more da
7 min read
Python | Plotting Stock charts in excel sheet using XlsxWriter module
Prerequisite: Create and Write on an excel sheet XlsxWriter is a Python library using which one can perform multiple operations on excel files like creating, writing, arithmetic operations and plotting graphs. Let’s see how to plot Stock charts using realtime data. Charts are composed of at least one series of one or more data points. Series themse
3 min read
Python | Plotting Line charts in excel sheet using XlsxWriter module
Prerequisite: Create and Write on an excel sheet XlsxWriter is a Python library using which one can perform multiple operations on excel files like creating, writing, arithmetic operations and plotting graphs. Let’s see how to plot Line charts using realtime data. Charts are composed of at least one series of one or more data points. Series themsel
3 min read
Python | Plotting Combined charts in excel sheet using XlsxWriter module
Prerequisite: Create and Write on an excel sheetXlsxWriter is a Python library using which one can perform multiple operations on excel files like creating, writing, arithmetic operations, and plotting graphs. Let’s see how to plot Combined charts using real-time data. Charts are composed of at least one series of one or more data points. Series th
5 min read
Python | Plotting Different types of style charts in excel sheet using XlsxWriter module
Prerequisite: Create and Write on an excel sheet XlsxWriter is a Python library using which one can perform multiple operations on excel files like creating, writing, arithmetic operations and plotting graphs. Let’s see how to plot different types of Style charts, using realtime data. Charts are composed of at least one series of one or more data p
3 min read
Python | Plotting column charts in excel sheet with data tables using XlsxWriter module
Prerequisite: Create and Write on an excel sheetXlsxWriter is a Python library using which one can perform multiple operations on excel files like creating, writing, arithmetic operations and plotting graphs. Let’s see how to plot charts with Data tables using realtime data.Charts are composed of at least one series of one or more data points. Seri
5 min read
Python | Writing to an excel file using openpyxl module
Prerequisite : Reading an excel file using openpyxl Openpyxl is a Python library for reading and writing Excel (with extension xlsx/xlsm/xltx/xltm) files. The openpyxl module allows Python program to read and modify Excel files. For example, user might have to go through thousands of rows and pick out few handful information to make small changes b
3 min read
Python | Adjusting rows and columns of an excel file using openpyxl module
Prerequisites : Excel file using openpyxl writing | reading Set the height and width of the cells:Worksheet objects have row_dimensions and column_dimensions attributes that control row heights and column widths. A sheet’s row_dimensions and column_dimensions are dictionary-like values; row_dimensions contains RowDimension objects and column_dimens
3 min read
Reading an excel file using Python openpyxl module
Openpyxl is a Python library for reading and writing Excel (with extension xlsx/xlsm/xltx/xltm) files. The Openpyxl Module allows Python programs to read and modify Excel files. For example, users might have to go through thousands of rows and pick out a few handfuls of information to make small changes based on some criteria. Using Openpyxl module
3 min read
Creating Charts using openpyxl
Charts are a powerful way to visualize data, making it easier to analyze and present information. While working with Excel files in Python, the library openpyxl comes into the picture and can be used as a tool for automating tasks like reading data, writing data, formatting cells, and of course creating charts. In this article, we will learn to cre
7 min read
How to get sheet names using openpyxl - Python
The openpyxl library is widely used to handle excel files using Python. Often when dealing with excel files one might have noticed multiple sheets. The sheets are normally mentioned in the bottom left corner of the screen. One can install this library by running the following command. pip install openpyxl In this article, we will be seeing how to g
2 min read
Python | Plotting an Excel chart with pattern fills in column using XlsxWriter module
Prerequisite: Create and Write on an excel sheetXlsxWriter is a Python library using which one can perform multiple operations on excel files like creating, writing, arithmetic operations and plotting graphs. Let’s see how to plot chart with pattern fills, using realtime data.Charts are composed of at least one series of one or more data points. Se
3 min read
Python | Plotting an Excel chart with Gradient fills using XlsxWriter module
Prerequisite: Create and Write on an excel sheetXlsxWriter is a Python library using which one can perform multiple operations on excel files like creating, writing, arithmetic operations, and plotting graphs. Let’s see how to plot a chart with Gradient fills, using real-time data.Charts are composed of at least one series of one or more data point
5 min read
Python | Arithmetic operations in excel file using openpyxl
Prerequisite: Reading & Writing to excel sheet using openpyxlOpenpyxl is a Python library using which one can perform multiple operations on excel files like reading, writing, arithmetic operations and plotting graphs. Let's see how to perform different arithmetic operations using openpyxl. =SUM(cell1:cell2) : Adds all the numbers in a range of
3 min read
Python | Trigonometric operations in excel file using openpyxl
Prerequisite : Adjusting rows and columns of an excel sheet using openpyxl. Openpyxl is a Python library using which one can perform multiple operations on excel files like reading, writing, mathematical operations and plotting graphs. Let’s see how to perform different Trigonometric operations using openpyxl. Simple trigonometric functions : Code
3 min read
Adding Conditional Formatting to Excel Using Python Openpyxl
Adding conditional formatting using openpyxl is an easy and straightforward process. While working with Excel files, conditional formatting is useful for the visualization of trends in the data, highlighting crucial data points, and making data more meaningful and understandable. In this article, we will give a detailed step-by-step explanation for
5 min read
Merge and Unmerge Excel Cells using openpyxl in Python
In this article, we are going to learn about Python programs to merge and unmerge excel cells using openpyxl. IntroductionOpenpyxl is a Python library commonly used for reading and writing Excel Files. In this article, we will be using this library to merge cells in Excel. Merging is helpful when we want to display some special information in order
4 min read
Plotting multiple bar charts using Matplotlib in Python
A multiple bar chart is also called a Grouped Bar chart. A Bar plot or a Bar Chart has many customizations such as Multiple bar plots, stacked bar plots, horizontal bar charts. Multiple bar charts are generally used for comparing different entities. In this article, plotting multiple bar charts are discussed. Example 1: Simple multiple bar chart In
4 min read
How to delete one or more rows in excel using Openpyxl?
Openpyxl is a Python library to manipulate xlsx/xlsm/xltx/xltm files. With Openpyxl you can create a new Excel file or a sheet and can also be used on an existing Excel file or sheet. Installation This module does not come built-in with Python. To install this type the below command in the terminal. pip3 install openpyxl In this article, we will di
5 min read
Excel Automation with Openpyxl in Python
Excel is a powerful tool that allows you to store, manipulate and analyze data. It is widely used in various industries for data analysis and reporting. However, as the size of the data increases, Excel becomes slow and it takes a lot of time to perform complex operations. This is where Python comes into play. Python is a versatile programming lang
4 min read
Article Tags :
Practice Tags :