Skip to main content

Graphing With Python in Org-Mode

Recently, I've had to do some data analysis in my day job. We use Databricks at work, but I have an affinity for downloading a CSV and playing around with plots in python. Rather than play around with scripts, I decided to find a workflow with org-babel. This allows me to include commentary, code, visualizations, or whatever else I want to all in one document.

Playing with Virtual Environments

First thing's first, we need to install seaborn, my (and many others) graphing library of choice. Since I use Fedora Silverblue, we can't simply run pip install seaborn (though you might want to). We can use a Python virtual environment, though.

python -m venv ~/.virtualenvs/org
$HOME/.virtualenvs/org/bin/python -m pip install -q seaborn
$HOME/.virtualenvs/org/bin/python -m pip show seaborn
Name: seaborn
Version: 0.13.0
Summary: Statistical data visualization
Home-page:
Author:
Author-email: Michael Waskom <[email protected]>
License:
Location: /var/home/digyx/.virtualenvs/org/lib64/python3.11/site-packages
Requires: matplotlib, numpy, pandas
Required-by:

Perfect. Now, we need to tell org-babel to use this virutal environment. This can either be done globally, which is what I do, or you can set it by running an elisp source block.

(setq org-babel-python-command "/var/home/digyx/.virtualenvs/org/bin/python")
/var/home/digyx/.virtualenvs/org/bin/python

Babel Time

Now it's time to setup our source blocks. This is relatively easy. By adding the following headers, we can save the object returned by the sns.kdeplot function, or any function returning a matplotlib Figure object, to an image file and then org-babel will link that image as the results in our org document.

#+begin_src python :results graphics file output :file example.png
...
#+end_src

Now let's do some graphing using one of seaborn's built-in datasets!

import seaborn as sns
import matplotlib.pyplot as plt

plt.figure(figsize=(9, 6))
df = sns.load_dataset("penguins")
sns.kdeplot(
    x="body_mass_g",
    hue="species",
    data=df,
).set(
    title="Penguin Body Mass by Species",
    xlabel="Body Mass in Grams",
    ylabel="Density",
)

example.png

There are no articles to list here yet.