-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmarketmap.py
More file actions
38 lines (28 loc) · 1.12 KB
/
marketmap.py
File metadata and controls
38 lines (28 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from bs4 import BeautifulSoup
import requests
import matplotlib.pyplot as plt
import squarify
def make_plot():
url = "https://companiesmarketcap.com/dow-jones/largest-companies-by-market-cap/"
response = requests.get(url)
soup = BeautifulSoup(response.text, "lxml")
rows = soup.findChildren("tr")
symbols = []
marketcaps = []
sizes = []
for row in rows:
try:
symbol = row.find("div",{"class":"company-code"}).text
marketcap = row.findAll('td')[2].text
marketcaps.append(marketcap)
symbols.append(symbol)
if marketcap.endswith('T'):
sizes.append(float(marketcap[1:-2]) * 10 ** 12)
elif marketcap.endswith('B'):
sizes.append(float(marketcap[1:-2]) * 10 ** 9)
except AttributeError:
pass
labels = [f"{symbols[i]}\n({marketcaps[i]})" for i in range(len(symbols))]
colors = [plt.cm.tab20c(i/ float(len(symbols))) for i in range(len(symbols))]
squarify.plot(sizes=sizes,label=labels, color=colors, bar_kwargs={"linewidth":0.5,"edgecolor":"#111111"})
plt.show()