Skip to content

Instantly share code, notes, and snippets.

@acdick
Created April 7, 2019 06:57
Show Gist options
  • Save acdick/52eef3cc723a2361a187e78ff808eb14 to your computer and use it in GitHub Desktop.
Save acdick/52eef3cc723a2361a187e78ff808eb14 to your computer and use it in GitHub Desktop.
Visualizing the BCG Matrix in Plotly
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')
@SiberianIvan
Copy link

Hi. Could you specify the variable from the 7th line "bcg_matrix", please? I got an error message like "NameError: name 'bcg_matrix' is not defined". Thank you.

@acdick
Copy link
Author

acdick commented Feb 7, 2022

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