Creating Maps in Python with Plotnine

Choropleth map of U.S. state populations using 2024 ACS five-year estimates, with darker blue indicating larger populations.

Maps are cool

Over the last 10 years I’ve taught a graduate level applied statistics course and at some point along the way I thought we should cover maps. Yes, I know, “maps” != “statistics”, but. . . maps are cool.

For my students in the field of environmental science, using maps to visualize their work is a relevant skill. For the rest of us? Well. . . maps are cool. It ends up, the R programming language has some excellent tools for generating maps. For many of my students, this course is their first experience using code to carryout data analyses and visualizations—for statistical methods, R is still a logical choice. There are some excellent packages in R built around the “Tidyverse” that make learning data manipulation (dplyr) and visualization (ggplot2) a great first language.

While I still use R for my course, much of my professional work has moved to Python.

R to Python

R is still a popular language; this is seen in several online indexes including TIOBE, where R is ranked #7—up from #12 just a year ago. R is not going away. Of course, Python is ranked #1 and has been the most popular general purpose language for at least five years. Two Python packages that facilitated my adoption of Python are Polars for data manipulation and Plotnine for visualizations.

The Polars’ community has adopted a saying that people “come for the speed and stay for the API.” Personally, I came for the API. It can’t replace Pandas (yet), but for data manipulation and transformation, it’s my preferred Python tool when working with data.

And Plotnine? From the matplotlib.org site

plotnine is an implementation of a grammar of graphics in Python. The grammar allows users to compose plots by explicitly mapping data to the visual objects that make up the plot.

The syntax is very similar to ggplot2. I love working in ggplot2 and Plotnine allows me to take those skills directly into a Python environment.

Choropleth Maps in Python

Until I had worked through this process, if I needed to make a map, I would have done it in R. I knew the framework that leveraged ggplot2 and I was able to get reproducible results. Unfortunately, it wasn’t a drop-in replacement. I’ve worked to document the equivalent workflow that allows a user to build choropleth maps in Python using Plotnine.

A choropleth map is a thematic map that uses color, shading, or patterns (pseudocolor) to represent statistical or numerical data aggregated over predefined geographic regions, such as states, counties, or countries —Wikipedia

I’ve posted a JupyterLab notebook on GitHub demonstrating how I worked through this task and hope it’s helpful if you’d like to use maps in your work.

Can AI-assisted workflows really help?

I consider myself proficient in data analysis and enjoy the challenge of producing a good visualization. I’ve experimented with AI agents over the last several years now and have had mixed results. At times, it felt like the agent was getting in my way. For a recent project, I wanted to pull data from the SEC’s Electronic Data Gathering, Analysis, and Retrieval (EDGAR) system. Working with API calls is an area where I struggle, so I was hopeful this might be a good use case.

The goal of the project was to produce a simple graphic showing gross margins of selected U.S. semiconductor companies across three sectors that could be easily reproduced, updated with future data, or used to produce similar visualizations with a different or updated company list.

Overall, I was happy with the final result (Figure below).

Line graph showing gross margin trends for Fabless companies, IDM (Integrated Device Manufacturers), and WFE (Wafer Fab Equipment suppliers) from 2010 to 2030, with notable companies indicated in each category.

At some point in the past, I would have manually pulled each company’s 10-K (annual) filing and dutifully entered the needed values into a spreadsheet as Excel was the tool I, like many, used and abused. Here, I pull data from data.sec.gov/api/xbrl/companyfacts/ using the API https://data.sec.gov/api/xbrl/companyfacts/CIK##########.json. This will provide all the company concepts data for a company returned in a single API call.

Code is available on GitHub

The basic steps:
– Get the SEC’s company identifier (CIK)
– Use the CIK in the above API call (produces a JSON file)
– Extract revenue, gross profit, and cost tags from the JSON file
– Calculate the gross margin as (revenue – cost)/revenue; some entries did not report a gross profit tag, so I compute it from the revenue and cost
– Combine each company’s data into a final data frame

The final data frame becomes the input for the visualization (created with Plotnine).

Where was the agent most helpful?

The agent was most helpful in creating functions that pulled tags for revenue, gross profit, and cost. For these companies, there were three different tags for revenue and two for cost. I’ve done similar tasks before and it has always been painful and having the AI agent write these functions was very helpful. The generated code was well-structured and reasonably commented. I’m sure my initial effort would have been less clean.

So where did I contribute? The Plotnine visualization. Plotnine is a Python implementation of R’s ggplot grammar of graphics. I’ve been working with ggplot for nearly a decade and with Plotnine since roughly 2023 when the tool seemed to reach a point of maturity and stability. I now have my favorite tool (which is built on Matplotlib functionality) in Python. This is where, frankly, I don’t need (or want) much help.

It took me a couple hours to pull everything together—without help, this would have been a good day’s worth of work.

Additional Info

“AI agent” == “Claude code”

Company names and trading symbols (ordered by revenue)

NameSymbol
NVIDIA CorporationNVDA
Broadcom Inc.AVGO
Intel CorporationINTC
QUALCOMM IncorporatedQCOM
Advanced Micro Devices, Inc.AMD
Micron Technology, Inc.MU
Applied Materials, Inc.AMAT
Lam Research CorporationLRCX
KLA CorporationKLAC

Companies fall into three sectors: fabless, integrated device manufacturers (IDM), and wafer fab equipment (WFE) suppliers.

Color Sources:
– NVIDIA, logo.dev (https://www.logo.dev/search/brands/nvidia.com)
– AMD, Brandpalettes (https://brandpalettes.com/amd-colors/)
– Broadcom, Designpieces
(https://www.designpieces.com/palette/broadcom-color-palette-hex-and-rgb/)
– Intel, Designpieces
(https://www.designpieces.com/palette/intel-color-palette-hex-and-rgb/)
– Micron, logo.dev (https://www.logo.dev/search/brands/micron.com)
– Applied Materials, logo.dev (https://www.logo.dev/search/brands/appliedmaterials.com)
– Lam Research, Brandpalettes (https://brandpalettes.com/lam-research-colors/)
– KLA, Brand Color Code (https://www.brandcolorcode.com/kla-corporation)
– Qualcomm (https://www.designpieces.com/palette/qualcomm-color-palette-hex-and-rgb/)

A 5-Week Passion Project

NIST/SEMATECH e-Handbook of Statistical Methods (Tidyverse Edition)

I’ve used the NIST/SEMATECH e-Handbook of Statistical Methods for nearly 20 years. First, as an applications scientist working for a semiconductor capital equipment company and later as an educator teaching a graduate level applied statistics course. A guiding principle of the book is to make the examples and case studies accessible with most allowing the reader to work out the problems by hand. While the book promotes the use of NIST’s Dataplot software, it was straight forward to adapt the case studies to R. Overall, this online book is a valuable resource.

My biggest complaint? The format—which uses dedicated HTML pages for each subsection of each chapter. This makes the book hard to navigate when you know what you’re looking for and nearly impossible to browse when you don’t.

Over the last year, I, like many people have been experimenting with LLMs and was curious if I could leverage an LLM to convert this e-Handbook into a Quarto book. After a month of work (okay, five weeks—which was more than I expected), I have a working Quarto book on GitHub. What I refer to as the “Tidyverse Edition.”

My goal was to stay faithful to the original text and only update the in-text examples, graphics, and case studies with reproducible R code that leans heavily on the tools of the Tidyverse (dplyr, ggplot2, etc.). That said, I did try to correct minor typos or inconsistencies when found and document more serious issues (which were few) with call-out notes where appropriate.

The original e-Handbook was released into the public domain and this effort follows their convention.

Was this a worthwhile project? Yes, I think so. Over the last several years there have been several instances where, during a semester, the NIST website was unavailable due to a government shutdown. While an archived version of the e-Handbook is available for download, this is less than idea for most students. Additionally, there seems to be a risk that this type of government work may be removed from public view if it’s deemed unnecessary or no longer considered a government responsibility.

Was this project appropriate use of an LLM? Definitely. I estimate that manually editing the original document, even with tools like pandoc, the project would have become a part-time endeavor taking many months, or years instead of a full-time effort over five weeks.

Finally, I hope others might be inspired to use this repository as a launching point for their own projects. Several of the chapters could easily become their own dedicated books and there are opportunities to build new case studies from other industries.