Developer ToolsFree generatorPDF, Excel, Word export

NCR Pareto Chart Python Script Generator

Many quality teams export NCRs from the ERP to a CSV and build a Pareto by hand each month. This generator gives you a small Python script that does it the same way every time.

Fill in

Generated code
Download:

ncr_pareto.py

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.ticker import PercentFormatter

CSV_FILE = "ncr_export.csv"
CATEGORY = "Defect Code"
QTY = "Reject Qty"
TOP_N = 10

df = pd.read_csv(CSV_FILE)

if QTY in df.columns:
    counts = df.groupby(CATEGORY)[QTY].sum()
else:
    counts = df.groupby(CATEGORY).size()

counts = counts.sort_values(ascending=False).head(TOP_N)
cum_pct = counts.cumsum() / counts.sum() * 100

fig, ax = plt.subplots(figsize=(10, 5))
ax.bar(counts.index.astype(str), counts.values, color="#134BAC")
ax.set_ylabel("Count or quantity")
ax.set_title("NCR Pareto by defect code")
plt.setp(ax.get_xticklabels(), rotation=45, ha="right")

ax2 = ax.twinx()
ax2.plot(counts.index.astype(str), cum_pct.values, color="#c0392b", marker="o")
ax2.yaxis.set_major_formatter(PercentFormatter())
ax2.set_ylim(0, 105)
ax2.set_ylabel("Cumulative percent")

fig.tight_layout()
fig.savefig("ncr_pareto.png", dpi=150)
print(counts.to_string())
print("Saved ncr_pareto.png")

Install and run

pip install pandas matplotlib
python ncr_pareto.py

The script treats a CSV with a header row and a numeric quantity column. If the quantity column is missing it counts rows per category. Cumulative percent is computed on the top N categories only, so use a large N if you want it relative to all defects. Run it locally so quality data does not leave your machine.

Automate quality charts from my ERP

Optional: talk to an expert about a scheduled, read-only quality dashboard.

Optional. The tool and every export work without this. No spam.

How to use the script

Export your NCR list to CSV with a header row. Enter the file name and the exact column headers for the defect category and the rejected quantity. Copy the script into a file named ncr_pareto.py in the same folder as the CSV, install pandas and matplotlib, and run it. It prints the ranked table and saves a chart image you can drop into a management review slide or a corrective action report.

What the chart shows

Bars show the count or quantity per category, sorted from largest to smallest. The line shows the cumulative percent of the total. A steep rise at the left means a few categories dominate and are worth attacking first. A flat line means problems are spread out and you may need to regroup categories, or look at a different dimension such as supplier or work center before choosing actions.

  • Check column headers match exactly, including spaces
  • Group near-duplicate defect codes first
  • Compare counts and quantities, they can rank differently

Practical tips

Filter to a time window before running, for example the last quarter, so the chart reflects current performance. If your export has a date column, add a filter line after reading the CSV. Keep the script under version control so changes to the method are visible. Use the chart as a starting point for investigation: a Pareto says where to look and never explains why the problem occurs.

Frequently Asked Questions

Do I need to be a programmer to run this?

You need Python installed and the ability to run two commands in a terminal. If that is a barrier, the SQL generator and Excel give the same Pareto without Python.

Why does the cumulative line not reach 100 percent?

It does reach 100 percent of the categories shown. If you limit to the top N, the line ends at 100 percent of those N only. Increase N to include every category.

Can it read an Excel file instead of CSV?

Yes. Replace pd.read_csv with pd.read_excel and give the workbook name. You also need the openpyxl package installed for xlsx files.

Is my data sent anywhere?

No. The script runs on your computer and reads a local file. This page only generates the text of the script from the field values you enter.

If you want these charts fed directly from your ERP quality data on a schedule, we can help set up a read-only pipeline that runs inside your network.