Skip to content

Instantly share code, notes, and snippets.

@tdowgielewicz
Last active October 10, 2019 19:54
Show Gist options
  • Save tdowgielewicz/55ce82770e72292cf27ceef1ab75daf7 to your computer and use it in GitHub Desktop.
Save tdowgielewicz/55ce82770e72292cf27ceef1ab75daf7 to your computer and use it in GitHub Desktop.
US Navy shpis commission per year chart
import requests
from datetime import datetime
import dateparser
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from bs4 import BeautifulSoup
url = 'https://en.wikipedia.org/wiki/List_of_current_ships_of_the_United_States_Navy'
html_content = requests.get(url).text
soup = BeautifulSoup(html_content, "lxml")
statki = []
wykres = []
rows = soup.find_all("tr")
for row in rows:
try:
name = row.contents[1].contents[0].text
date = row.contents[9].next.contents[0]
date = dateparser.parse(date)
wykres.append(datetime.timestamp(date))
statki.append((name, date))
except IndexError:
pass
except Exception as e:
print(e)
print(wykres)
for statek in statki:
print(statek)
mpl_data = mdates.epoch2num(wykres)
fig, ax = plt.subplots(1, 1)
ax.hist(mpl_data, bins=52, color='lightblue')
ax.xaxis.set_major_locator(mdates.YearLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y'))
ax.set_title('US Navy shpis commission per year')
ax.set_xlabel("Year of commission")
ax.set_ylabel("Number of ships")
for tick in ax.get_xticklabels():
tick.set_rotation(85)
plt.show()
@tdowgielewicz
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment