Created
April 7, 2019 06:57
-
-
Save acdick/52eef3cc723a2361a187e78ff808eb14 to your computer and use it in GitHub Desktop.
Visualizing the BCG Matrix in Plotly
This file contains 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.plotly as py | |
import plotly.graph_objs as go | |
hover_text = [] | |
color_range = [] | |
for index, row in bcg_matrix.iterrows(): | |
hover_text.append(('Borough: {borough}<br>'+ | |
'Neighborhood: {neighborhood}<br>'+ | |
'Share: {share}%<br>'+ | |
'Growth: {growth}%<br>'+ | |
'Stores: {stores}<br>'+ | |
'Dogs per Store: {pps}').format(borough=row['Borough'], | |
neighborhood=row['Neighborhood'], | |
share=row['Share 2016'], | |
growth=row['5Y CAGR 2014-2019'], | |
stores=row['Stores'], | |
pps=row['Dogs per Store'])) | |
color_range.append(min(row['Dogs per Store'],120)) | |
trace0 = go.Scatter( | |
x=bcg_matrix['Share 2016'], | |
y=bcg_matrix['5Y CAGR 2014-2019'], | |
text=hover_text, | |
mode='markers', | |
marker=dict( | |
size=bcg_matrix['Stores'], | |
color=color_range, | |
showscale=True, | |
reversescale=True, | |
colorbar=dict( | |
title='Dogs Per Store' | |
), | |
colorscale='RdBu' | |
) | |
) | |
data = [trace0] | |
layout = go.Layout( | |
title='Growth-Share Matrix of Licensed Dogs in New York', | |
xaxis=dict( | |
title='Neighborhood Share of Licensed Dogs, 2016 [%]', | |
gridcolor='rgb(255, 255, 255)', | |
zerolinewidth=1, | |
ticklen=5, | |
gridwidth=2, | |
), | |
yaxis=dict( | |
title='5-Year CAGR of Licensed Dogs, 2014-2019 [%]', | |
gridcolor='rgb(255, 255, 255)', | |
zerolinewidth=1, | |
ticklen=5, | |
gridwidth=2, | |
), | |
paper_bgcolor='rgb(243, 243, 243)', | |
plot_bgcolor='rgb(243, 243, 243)', | |
) | |
fig = go.Figure(data=data, layout=layout) | |
py.iplot(fig, filename='bcg-matrix') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The entire code can be found at https://github.com/acdick/framing_data_with_dataframes