Chapter 6: Visualizing Data with Charts and Graphs
A picture is worth a thousand words, and in data analysis, a chart is worth a thousand rows of numbers. Charts make it easy to spot trends, compare values, and understand complex data at a glance. In this chapter, you will learn how to automate the creation of charts and add them to your spreadsheets to build compelling reports and dashboards.
Getting Started: Charting Our Inventory
We will use the inventory_with_formulas.xlsx file created in the last chapter as our starting point. This file contains our inventory data, now with dynamic formulas.
To create charts, we need to import chart-related classes from the openpyxl.chart module. We'll start with a BarChart to compare the quantities of our products.
Creating Your First Chart: A Bar Chart
A bar chart is perfect for comparing distinct items. Let's create one that shows the quantity of each product in our inventory.
The process involves these key steps:
Create a
BarChartobject.Create
Referenceobjects to tell the chart where to find the data and the labels.Add the data and labels to the chart.
Add the chart to the worksheet.
Create a new Python file named create_charts.py and add the following code:
from openpyxl import load_workbook
from openpyxl.chart import BarChart, Reference, PieChart
wb = load_workbook("inventory_with_formulas.xlsx")
ws = wb.active
# 1. Create a BarChart object
chart1 = BarChart()
chart1.title = "Product Inventory Quantities"
chart1.x_axis.title = "Product"
chart1.y_axis.title = "Quantity"
# 2. Create Reference objects for the data
# NOTE: We subtract 2 from max_row because of the empty row and grand total
last_data_row = ws.max_row - 2
data = Reference(ws, min_col=2, min_row=2, max_row=last_data_row)
cats = Reference(ws, min_col=1, min_row=2, max_row=last_data_row)
# 3. Add the data and labels to the chart
chart1.add_data(data)
chart1.set_categories(cats)
# 4. Add the chart to the worksheet, placing it at cell F2
ws.add_chart(chart1, "F2")
# --- Creating a Pie Chart ---
# 1. Create a PieChart object
chart2 = PieChart()
chart2.title = "Percentage of Total Inventory Value by Product"
# 2. Select data for the pie chart
data = Reference(ws, min_col=4, min_row=2, max_row=last_data_row) # Column D
labels = Reference(ws, min_col=1, min_row=2, max_row=last_data_row)
# 3. Add the data and set the labels
chart2.add_data(data)
chart2.set_categories(labels)
# 4. Add the chart to the worksheet, placing it below the first chart at F18
ws.add_chart(chart2, "F18")
# Save the final dashboard
wb.save("inventory_dashboard.xlsx")
print("Dashboard with both charts saved successfully.")
Run the script. Your inventory_dashboard.xlsx file will now contain both the bar chart and a new pie chart, giving you a comprehensive visual overview of your inventory.
What You've Accomplished
You have now learned to automate one of the most powerful features in Excel: charting. You can programmatically generate visualizations from your data, customize them with titles and labels, and place them precisely within your spreadsheets. This elevates your scripts from simple data processors to sophisticated report-building engines.
Last updated