Skip to content

Instantly share code, notes, and snippets.

@sdushantha
Created August 4, 2020 13:08
Show Gist options
  • Save sdushantha/4c801c038fcfd570c1f4c3a16f55253c to your computer and use it in GitHub Desktop.
Save sdushantha/4c801c038fcfd570c1f4c3a16f55253c to your computer and use it in GitHub Desktop.

Source code for graph used in EE

import matplotlib.pyplot as plt
from collections import Counter, OrderedDict
import requests

r = requests.get("https://sherlock-holm.es/stories/plain-text/cano.txt")
text = r.text

chars_to_remove = []
from matplotlib.pyplot import figure

# sorted()
counts = Counter(text.lower())
for letter in counts:
    if not letter.isalpha() or not letter.isascii():
        chars_to_remove.append(letter)

for char in chars_to_remove:
    del counts[char]
        
letters = counts.keys()
frequencies = counts.values()

percentage_frequencies = [(value/sum(frequencies)*100) for value in frequencies]


fig = plt.figure()
fig= plt.figure(figsize=(8,5))

ax = fig.add_axes([0,0,1,1])
ax.bar(letters, percentage_frequencies, width=0.8, align='edge')

for a, i in enumerate(ax.patches): 
    plt.text(i.get_x()+0.1, i.get_height()+0.15,  
             str(round(percentage_frequencies[a], 2))+"%", 
             fontsize = 10)

    
ax.set_ylabel('Percentage Frequency (%)')
ax.set_xlabel('Letters from the English Alphabet')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment