> For the complete documentation index, see [llms.txt](https://www.bayant.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.bayant.com/python-for-excel-automation/00-preface/06-visualization.md).

# 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:

1. Create a `BarChart` object.
2. Create `Reference` objects to tell the chart where to find the data and the labels.
3. Add the data and labels to the chart.
4. 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.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.bayant.com/python-for-excel-automation/00-preface/06-visualization.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
