Spaces:
Runtime error
Runtime error
none
commited on
Commit
·
64a9c12
1
Parent(s):
7b7c508
Add donut plot
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import joblib
|
|
| 3 |
import time
|
| 4 |
|
| 5 |
import plotly.graph_objects as go
|
|
|
|
| 6 |
import streamlit as st
|
| 7 |
import pandas as pd
|
| 8 |
import numpy as np
|
|
@@ -193,6 +194,46 @@ def breadth_first_traverse(tree):
|
|
| 193 |
|
| 194 |
return visited_nodes
|
| 195 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
def main():
|
| 198 |
# load the data
|
|
@@ -211,6 +252,10 @@ def main():
|
|
| 211 |
st.markdown('## Setting the scene')
|
| 212 |
st.markdown("""
|
| 213 |
I make a lot of dashboards, which means I make a lot of the same plots over and over.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
Desperate for some creative outlet, I wanted to make a new visualization—
|
| 215 |
something I'd never seen before.
|
| 216 |
Inspired by interactive visualizations like
|
|
@@ -330,8 +375,8 @@ It takes a second to get going after you hit `Play`.
|
|
| 330 |
'method': 'animate',
|
| 331 |
'args':[None, {
|
| 332 |
'fromcurrent': True,
|
| 333 |
-
'frame': {'duration':
|
| 334 |
-
'transition': {'duration':
|
| 335 |
}],
|
| 336 |
},
|
| 337 |
{
|
|
@@ -363,7 +408,8 @@ That helpful poster didn't point out any other examples, so that one is still pr
|
|
| 363 |
Later on,
|
| 364 |
[a different answer by the same poster](https://community.plotly.com/t/animation-with-slider-not-moving-when-pressing-play/34763)
|
| 365 |
got me out of a jam.
|
| 366 |
-
|
|
|
|
| 367 |
""")
|
| 368 |
|
| 369 |
st.markdown('## Check out the data!')
|
|
@@ -392,13 +438,16 @@ This plot is similar to the plot above, but the slider here coordinates with a t
|
|
| 392 |
st.plotly_chart(figures[idx])
|
| 393 |
st.dataframe(trees[idx])
|
| 394 |
st.markdown("""
|
| 395 |
-
This section is mostly just to warn you against making the same foolhardy decision to marry the innermost guts of SciKit-Learn to the sparsely documented world of
|
| 396 |
|
| 397 |
I'm glad it was challenging, though.
|
| 398 |
I did go into this hoping for something more interesting than a donut plot.
|
| 399 |
Maybe I'll think on the `value` and `gain` fields a bit and come up with a version 2.
|
| 400 |
""")
|
| 401 |
|
|
|
|
|
|
|
|
|
|
| 402 |
if __name__=='__main__':
|
| 403 |
main()
|
| 404 |
|
|
|
|
| 3 |
import time
|
| 4 |
|
| 5 |
import plotly.graph_objects as go
|
| 6 |
+
import plotly.express as px
|
| 7 |
import streamlit as st
|
| 8 |
import pandas as pd
|
| 9 |
import numpy as np
|
|
|
|
| 194 |
|
| 195 |
return visited_nodes
|
| 196 |
|
| 197 |
+
def build_donut_plot():
|
| 198 |
+
display_duration = ['a moment', 'an eternity']
|
| 199 |
+
legend_name = [
|
| 200 |
+
'<br>researching the data,<br>cleaning the data,<br>and building the model<br>for this project',
|
| 201 |
+
'battling Plotly'
|
| 202 |
+
]
|
| 203 |
+
duration = [1, 19]
|
| 204 |
+
df = pd.DataFrame().from_dict({'duration': duration, 'legend_name': legend_name, 'display_duration': display_duration})
|
| 205 |
+
|
| 206 |
+
fig = px.pie(
|
| 207 |
+
df,
|
| 208 |
+
values='duration',
|
| 209 |
+
names='legend_name',
|
| 210 |
+
hover_name='display_duration',
|
| 211 |
+
# The docs claim this is okay.
|
| 212 |
+
# Turns out, this is not okay
|
| 213 |
+
# because Plotly tries to call .append() on whatever you pass to `hover_data`
|
| 214 |
+
# but only if you pass something to `color`
|
| 215 |
+
# so I can't set the color if I want to turn off columns in the hover text
|
| 216 |
+
hover_data={'duration': False, 'legend_name': False},
|
| 217 |
+
#color=display_duration,
|
| 218 |
+
#color_discrete_map={display_duration[0]:'fuschia', display_duration[1]:'rebecca_purple'},
|
| 219 |
+
hole=0.6,
|
| 220 |
+
#title="Believe it or not, a bug in Plotly means I can't change these colors"
|
| 221 |
+
title="I suppose one more donut won't kill me"
|
| 222 |
+
|
| 223 |
+
)
|
| 224 |
+
|
| 225 |
+
fig.update_traces(
|
| 226 |
+
textinfo='none',
|
| 227 |
+
hoverlabel={'font':{'size': 20}},
|
| 228 |
+
# This has no effect for some reason
|
| 229 |
+
# even though `hoverlabel` and `textinfo` work just fine
|
| 230 |
+
marker_colors=['fuschia', 'rebecca_purple'],
|
| 231 |
+
selector=dict(type='pie')
|
| 232 |
+
|
| 233 |
+
)
|
| 234 |
+
|
| 235 |
+
return fig
|
| 236 |
+
|
| 237 |
|
| 238 |
def main():
|
| 239 |
# load the data
|
|
|
|
| 252 |
st.markdown('## Setting the scene')
|
| 253 |
st.markdown("""
|
| 254 |
I make a lot of dashboards, which means I make a lot of the same plots over and over.
|
| 255 |
+
""")
|
| 256 |
+
st.plotly_chart(build_donut_plot())
|
| 257 |
+
|
| 258 |
+
st.markdown("""
|
| 259 |
Desperate for some creative outlet, I wanted to make a new visualization—
|
| 260 |
something I'd never seen before.
|
| 261 |
Inspired by interactive visualizations like
|
|
|
|
| 375 |
'method': 'animate',
|
| 376 |
'args':[None, {
|
| 377 |
'fromcurrent': True,
|
| 378 |
+
'frame': {'duration':4000},
|
| 379 |
+
'transition': {'duration': 2000},
|
| 380 |
}],
|
| 381 |
},
|
| 382 |
{
|
|
|
|
| 408 |
Later on,
|
| 409 |
[a different answer by the same poster](https://community.plotly.com/t/animation-with-slider-not-moving-when-pressing-play/34763)
|
| 410 |
got me out of a jam.
|
| 411 |
+
This `empet` character is pretty much the only one who answers Python posts on Plotly's forums.
|
| 412 |
+
As far as I can tell, that's because they're the only person in the world who understands Plotly's Python library.
|
| 413 |
""")
|
| 414 |
|
| 415 |
st.markdown('## Check out the data!')
|
|
|
|
| 438 |
st.plotly_chart(figures[idx])
|
| 439 |
st.dataframe(trees[idx])
|
| 440 |
st.markdown("""
|
| 441 |
+
This section is mostly just to warn you against making the same foolhardy decision to marry the innermost guts of SciKit-Learn to the sparsely documented world of Plotly animations in Python.
|
| 442 |
|
| 443 |
I'm glad it was challenging, though.
|
| 444 |
I did go into this hoping for something more interesting than a donut plot.
|
| 445 |
Maybe I'll think on the `value` and `gain` fields a bit and come up with a version 2.
|
| 446 |
""")
|
| 447 |
|
| 448 |
+
|
| 449 |
+
|
| 450 |
+
|
| 451 |
if __name__=='__main__':
|
| 452 |
main()
|
| 453 |
|