Last active
June 11, 2022 12:40
-
-
Save eliasdabbas/b285883a196087e336bfa681a3e33245 to your computer and use it in GitHub Desktop.
Interactive emailable HTML chart of top 500 companies. Users can select which countries to display
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import plotly.express as px | |
| import pandas as pd | |
| import requests | |
| dflist = [] | |
| for i in range(1, 6): | |
| resp = requests.get(f'https://companiesmarketcap.com/page/{i}/') | |
| df = pd.read_html(resp.text)[0] | |
| dflist.append(df) | |
| marketcap = pd.concat(dflist, ignore_index=True) | |
| marketcap['Market Cap'] = (marketcap['Market Cap'] | |
| .str.replace('\$', '', regex=True) | |
| .str.replace(' T', '000000000') | |
| .str.replace(' B', '0000000') | |
| .str.replace('\.', '') | |
| .astype(int)) | |
| hovertemp = '<b>%{hovertext}</b><br><br>Market cap: <b>$%{y}</b><br>Global rank: <b>%{customdata[1]}</b><br>Country: <b>%{customdata[0]}</b><extra></extra>' | |
| fig = px.scatter(marketcap, | |
| x='Name', | |
| y='Market Cap', | |
| hover_name='Name', | |
| template='none', | |
| size_max=15, | |
| size=[10]*500, | |
| color='Country', | |
| hover_data=['Country', 'Rank'], | |
| opacity=1, | |
| title=f'<b>Top 500 Companies by Market Capitalization - June 11, 2022</b><br>Source: CompaniesMarketCap.com', | |
| labels={'Name': '', 'Market Cap': ''}) | |
| for i in range(marketcap['Country'].nunique()): | |
| fig.data[i].hovertemplate = hovertemp | |
| fig.data[i].visible = 'legendonly' | |
| fig.layout.legend.title = f'Select Country:' | |
| fig.layout.xaxis.showgrid = False | |
| fig.layout.xaxis.showticklabels = False | |
| fig.layout.paper_bgcolor = 'black' | |
| fig.layout.plot_bgcolor = 'black' | |
| fig.layout.font.color = 'white' | |
| fig.layout.yaxis.gridcolor = 'white' | |
| fig.layout.legend.title.font.color = 'white' | |
| fig.write_html('companies_by_marketcap.html', config={'displayModeBar': False}) | |
| fig |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Get a copy of the interactive chart: https://bit.ly/3MBlLl2