5 Best Ways to Create Charts in Excel Using Python with Openpyxl

Rate this post

πŸ’‘ Problem Formulation: In data analysis and reporting, it is often necessary to visualize data in the form of charts directly within an Excel spreadsheet. Python’s library openpyxl enables automation of this task. This article demonstrates five methods to create various types of charts in Excel sheets using Python, transforming data inputs like lists or arrays into a visually appealing and meaningful charts.

Method 1: Creating a Bar Chart

Bar charts are one of the simplest yet most effective visual tools for comparing categories of data. Openpyxl provides a BarChart class which can be easily configured to represent data graphically in this format. It involves creating a chart object, adding series, setting titles, and finally, adding the chart to the worksheet.

Here’s an example:

from openpyxl import Workbook
from openpyxl.chart import BarChart, Reference

wb = Workbook()
ws = wb.active
data = [
    ('Category', 'Values'),
    ('A', 10),
    ('B', 20),
    ('C', 30),
    ('D', 40),
]
for row in data:
    ws.append(row)

chart = BarChart()
chart.add_data(Reference(ws, min_col=2, min_row=2, max_row=5, max_col=2))
chart.set_categories(Reference(ws, min_col=1, min_row=2, max_row=5))
ws.add_chart(chart, "E3")

wb.save("bar_chart.xlsx")

Output: An Excel file named “bar_chart.xlsx” with a bar chart representing the category-value pairs (A to D).

This code initializes a new workbook and worksheet, enters columns of data, and uses the BarChart class along with data references to create a bar chart. It sets the categories and values, places the chart on the worksheet, and saves the workbook to an Excel file.

Method 2: Generating a Line Chart

Line charts are vital when it’s important to show trends over a period. In openpyxl, line charts are created with the LineChart class. It allows plotting data points and connecting them with lines, making it efficient for displaying data changes over time or across categories.

Here’s an example:

from openpyxl import Workbook
from openpyxl.chart import LineChart, Reference

wb = Workbook()
ws = wb.active
for i in range(10):
    ws.append([i, i**2])

chart = LineChart()
chart.add_data(Reference(ws, min_col=2, min_row=1, max_row=10))
chart.set_categories(Reference(ws, min_col=1, min_row=1, max_row=10))
ws.add_chart(chart, "C10")

wb.save("line_chart.xlsx")

Output: An Excel file named “line_chart.xlsx” containing a line chart depicting the curve of the function y = x2.

This code snippet creates a new Excel workbook, inputs a series of x and x-squared values, defines a LineChart object, and then plots these values as a line chart. It demonstrates the simplicity of connecting data points with lines using coordinates from the Excel sheet.

Method 3: Pie Chart for Data Proportions

Pie charts are effective for showing parts of a whole. When using openpyxl, creating a pie chart involves the PieChart class. This enables you to showcase the proportion of each category visually.

Here’s an example:

from openpyxl import Workbook
from openpyxl.chart import PieChart, Reference

wb = Workbook()
ws = wb.active
data = [
    ('Apple', 50),
    ('Banana', 30),
    ('Cherry', 20),
    ('Date', 10)
]
for fruit, value in data:
    ws.append([fruit, value])

chart = PieChart()
chart.add_data(Reference(ws, min_col=2, min_row=1, max_row=4), titles_from_data=True)
chart.set_categories(Reference(ws, min_col=1, min_row=2, max_row=4))
ws.add_chart(chart, "C1")

wb.save("pie_chart.xlsx")

Output: An Excel file named “pie_chart.xlsx” showing a pie chart representing the distribution of fruits.

In this scenario, the code models a simple dataset depicting types of fruit and their corresponding quantities, then utilizes the PieChart class to display this data as portions of a pie. Titles and categories are automatically extracted from the data provided, and the resulting chart is output to a file.

Method 4: Scatter Chart for Correlation

If the goal is to analyze the correlation between two sets of data, scatter charts are the go-to representation. Using openpyxl’s ScatterChart class, it’s straightforward to create a visual representation highlighting the relationship between variables.

Here’s an example:

from openpyxl import Workbook
from openpyxl.chart import ScatterChart, Series, Reference

wb = Workbook()
ws = wb.active
for i in range(1, 10):
    ws.append([i, i**3])

chart = ScatterChart()
xvalues = Reference(ws, min_col=1, min_row=1, max_row=9)
yvalues = Reference(ws, min_col=2, min_row=1, max_row=9)
series = Series(yvalues, xvalues, title="Cubed Numbers")
chart.series.append(series)
ws.add_chart(chart, "C10")

wb.save("scatter_chart.xlsx")

Output: An Excel file named “scatter_chart.xlsx” containing a scatter chart plotting the relationship of numbers and their cubes.

This code shows how to initialize a scatter chart using ScatterChart, setting up the x and y values and creating a series representing the correlation between the two variables’ sets of values. The serialization of data points onto an XY plane provides a clear interpretation of their relationship.

Bonus One-Liner Method 5: Quick Area Chart

An area chart illustrates quantities over time and can be easily generated with a call to the AreaChart class in openpyxl. This is perfect for visualizing the cumulative effect of data points.

Here’s an example:

from openpyxl import Workbook
from openpyxl.chart import AreaChart, Reference

wb = Workbook()
ws = wb.active
ws.append(["Number", "Batch1", "Batch2"])
for i in range(10):
    ws.append([i, i**0.5, i**0.3])

chart = AreaChart()
chart.add_data(Reference(ws, min_col=2, min_row=1, max_col=3, max_row=11), titles_from_data=True)
ws.add_chart(chart, "E1")

wb.save("area_chart.xlsx")

Output: An Excel file named “area_chart.xlsx” with an area chart displaying the square root and cubic root for numbers 0-9.

A concise one-liner area chart creation process, this code extends from an Excel sheet with numbers and their respective roots. The subsequent visualization, saved into an area chart, effectively underscores accumulated values over the dataset’s scope.

Summary/Discussion

  • Method 1: Bar Chart. Best for comparing different categories. Simple to implement. Not suitable for showing data changes over time.
  • Method 2: Line Chart. Ideal for trend analysis. Clearly shows developments across intervals. Can be confusing if too many data points overlap.
  • Method 3: Pie Chart. Effective for displaying proportions. Visually intuitive. Not recommended for comparing subtle differences or many categories.
  • Method 4: Scatter Chart. Great for correlation analysis. Offers direct insight into the relationship between variables. Requires more interpretation than bar or pie charts.
  • Method 5: Area Chart. Good for visualizing cumulative data. Emphasizes volume beneath the line. Overlapping series can diminish clarity.