< expLog

Simple Plots

One of my most frequent tasks is plotting x-y plots.

(require 'ob-ipython)
%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use('ggplot')
import os
import re
from functools import reduce

path = os.path.join(os.getcwd(), "simpleplot.org")
contents = None
with open(path) as simpleplot:
    contents = simpleplot.read()
contents = re.sub(r'[^a-zA-Z]', '', contents.lower())

def group(value, element):
    if not value[1] or value[1][-1] != element:
	value[0].append(1)
	value[1].append(str(element))
    else:
	value[0][-1] += 1
    return value

counts, characters = reduce(group, sorted(contents), ([], []))
counts, characters = zip(
    *sorted(zip(counts, characters), key=lambda x: x[0]))
ticks = range(len(counts))
plt.figure(figsize=(5, 7))
plt.barh(ticks, counts, tick_label=characters)
plt.show()

Testing

alphabets.png