Python | Plotting charts in excel sheet using openpyxl module | Set – 2 (original) (raw)
Last Updated : 01 Jul, 2022
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 references to cell ranges. Let’s see how to plot Scatter, Bubble, Pie, 3D Pie 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 ScatterChart, PieChart 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 Bubble Chart. Bubble charts are similar to scatter charts but use a third dimension to determine the size of the bubbles. Charts can include multiple series. For plotting the bubble chart on an excel sheet, use BubbleChart class from openpyxl.chart submodule.
Python3
import
openpyxl
from
openpyxl.chart
import
BubbleChart, Reference, Series
wb
=
openpyxl.Workbook()
sheet
=
wb.active
rows
=
[
`` ("Number of Products", "Sales
in
USD", "Market share"),
`` (
14
,
12200
,
15
),
`` (
20
,
60000
,
33
),
`` (
18
,
24400
,
10
),
`` (
22
,
32000
,
42
),
]
for
row
in
rows:
`` sheet.append(row)
chart
=
BubbleChart()
xvalues
=
Reference(sheet, min_col
=
1
,
`` min_row
=
2
, max_row
=
5
)
yvalues
=
Reference(sheet, min_col
=
2
,
`` min_row
=
2
, max_row
=
5
)
size
=
Reference(sheet, min_col
=
3
,
`` min_row
=
2
, max_row
=
5
)
series
=
Series(values
=
yvalues, xvalues
=
xvalues,
`` zvalues
=
size, title
=
"
2013
")
chart.series.append(series)
chart.title
=
" BUBBLE
-
CHART "
chart.x_axis.title
=
" X_AXIS "
chart.y_axis.title
=
" Y_AXIS "
sheet.add_chart(chart, "E2")
wb.save("bubbleChart.xlsx")
Output: Code #2 : Plot the Scatter Chart Scatter, or xy charts are similar to some line charts. For plotting the Scatter chart on an excel sheet, use ScatterChart class from openpyxl.chart submodule.
Python3
import
openpyxl
from
openpyxl.chart
import
ScatterChart, Reference, Series
wb
=
openpyxl.Workbook()
sheet
=
wb.active
rows
=
[
`` ("Number of Products", "Sales
in
USD", "Market share"),
`` (
14
,
12200
,
15
),
`` (
20
,
60000
,
33
),
`` (
18
,
24400
,
10
),
`` (
22
,
32000
,
42
),
]
for
row
in
rows:
`` sheet.append(row)
chart
=
ScatterChart()
xvalues
=
Reference(sheet, min_col
=
1
,
`` min_row
=
2
, max_row
=
5
)
yvalues
=
Reference(sheet, min_col
=
2
,
`` min_row
=
2
, max_row
=
5
)
size
=
Reference(sheet, min_col
=
3
,
`` min_row
=
2
, max_row
=
5
)
series
=
Series(values
=
yvalues, xvalues
=
xvalues,
`` zvalues
=
size, title
=
"
2013
")
chart.series.append(series)
chart.title
=
" SCATTER
-
CHART "
chart.x_axis.title
=
" X_AXIS "
chart.y_axis.title
=
" Y_AXIS "
sheet.add_chart(chart, "E2")
wb.save(" ScatterChart.xlsx")
Output: Code #3 : Plot the Pie Chart Pie charts plot data as slices of a circle with each slice representing the percentage of the whole. Slices are plotted in a clockwise direction with 0° being at the top of the circle. Pie charts can only take a single series of data. For plotting the Pie chart on an excel sheet, use PieChart class from openpyxl.chart submodule.
Python
import
openpyxl
from
openpyxl.chart
import
PieChart, Reference
wb
=
openpyxl.Workbook()
sheet
=
wb.active
datas
=
[
`` [
'Pie'
,
'Sold'
],
`` [
'Apple'
,
50
],
`` [
'Cherry'
,
30
],
`` [
'Pumpkin'
,
10
],
`` [
'Chocolate'
,
40
],
]
for
row
in
datas:
`` sheet.append(row)
chart
=
PieChart()
labels
=
Reference(sheet, min_col
=
1
,
`` min_row
=
2
, max_row
=
5
)
data
=
Reference(sheet, min_col
=
2
,
`` min_row
=
1
, max_row
=
5
)
chart.add_data(data, titles_from_data
=
True
)
chart.set_categories(labels)
chart.title
=
" PIE
-
CHART "
sheet.add_chart(chart, "E2")
wb.save(" PieChart.xlsx")
Output: Code #4: Plot the Bar Chart For plotting the 3D pie chart on an excel sheet, use PieChart3D class from openpyxl.chart submodule.
Python3
import
openpyxl
from
openpyxl.chart
import
PieChart3D, Reference
wb
=
openpyxl.Workbook()
sheet
=
wb.active
datas
=
[
`` [
'Pie'
,
'Sold'
],
`` [
'Apple'
,
50
],
`` [
'Cherry'
,
30
],
`` [
'Pumpkin'
,
10
],
`` [
'Chocolate'
,
40
],
]
for
row
in
datas:
`` sheet.append(row)
chart
=
PieChart3D()
labels
=
Reference(sheet, min_col
=
1
,
`` min_row
=
2
, max_row
=
5
)
data
=
Reference(sheet, min_col
=
2
,
`` min_row
=
1
, max_row
=
5
)
chart.add_data(data, titles_from_data
=
True
)
chart.set_categories(labels)
chart.title
=
"
3DPIE
-
CHART "
sheet.add_chart(chart, "E2")
wb.save("
3DPieChart
.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 compo 6 min read
- Python | Plotting charts in excel sheet using openpyxl module | Set 3 Prerequisite : Plotting charts in excel sheet using openpyxl module Set - 1 | Set – 2Openpyxl 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 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 compos 5 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 com 10 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 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 6 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 on 3 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 o 6 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 7 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 3 min read