Skip to content

Commit ba5bdb2

Browse files
committed
fixed controls behavior
1 parent fc3bf0e commit ba5bdb2

File tree

1 file changed

+50
-25
lines changed

1 file changed

+50
-25
lines changed

python/visualizing-mri-volume-slices.md

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,17 @@ vol = io.imread("https://s3.amazonaws.com/assets.datacamp.com/blog_assets/attent
5050
volume = vol.T
5151
r, c = volume[0].shape
5252

53-
# Visualize data
53+
# Define frames
5454
import plotly.graph_objects as go
55+
nb_frames = 68
5556

56-
nb_frames = 10
57-
58-
# Define frames
5957
fig = go.Figure(frames=[go.Frame(data=go.Surface(
6058
z=(6.7 - k * 0.1) * np.ones((r, c)),
6159
surfacecolor=np.flipud(volume[67 - k]),
6260
cmin=0, cmax=200
63-
))
61+
),
62+
name=str(k) # you need to name the frame for the animation to behave properly
63+
)
6464
for k in range(nb_frames)])
6565

6666
# Add data to be displayed before animation starts
@@ -72,17 +72,31 @@ fig.add_trace(go.Surface(
7272
colorbar=dict(thickness=20, ticklen=4)
7373
))
7474

75-
# Sliders
76-
sliders=[
77-
dict(
78-
steps=[dict(method='animate',
79-
args= [None, dict(fromcurrent=True, mode='immediate', transition=dict(duration=0))
80-
],
81-
label='{:d}'.format(k+1))
82-
for k in range(nb_frames)],
83-
transition= dict(duration=0),
84-
)
85-
]
75+
76+
def frame_args(duration):
77+
return {
78+
"frame": {"duration": duration},
79+
"mode": "immediate",
80+
"fromcurrent": True,
81+
"transition": {"duration": duration, "easing": "linear"},
82+
}
83+
84+
sliders = [
85+
{
86+
"pad": {"b": 10, "t": 60},
87+
"len": 0.9,
88+
"x": 0.1,
89+
"y": 0,
90+
"steps": [
91+
{
92+
"args": [[f.name], frame_args(0)],
93+
"label": str(k),
94+
"method": "animate",
95+
}
96+
for k, f in enumerate(fig.frames)
97+
],
98+
}
99+
]
86100

87101
# Layout
88102
fig.update_layout(
@@ -93,11 +107,26 @@ fig.update_layout(
93107
zaxis=dict(range=[-0.1, 6.8], autorange=False),
94108
aspectratio=dict(x=1, y=1, z=1),
95109
),
96-
updatemenus=[
97-
dict(type='buttons',
98-
buttons=[dict(label='Play',
99-
method='animate',
100-
args=[None])])
110+
updatemenus = [
111+
{
112+
"buttons": [
113+
{
114+
"args": [None, frame_args(50)],
115+
"label": "▶", # play symbol
116+
"method": "animate",
117+
},
118+
{
119+
"args": [[None], frame_args(0)],
120+
"label": "◼", # pause symbol
121+
"method": "animate",
122+
},
123+
],
124+
"direction": "left",
125+
"pad": {"r": 10, "t": 70},
126+
"type": "buttons",
127+
"x": 0.1,
128+
"y": 0,
129+
}
101130
],
102131
sliders=sliders
103132
)
@@ -112,10 +141,6 @@ Here's where you can find her:
112141
- Her [Twitter](https://twitter.com/mathinpython) under the handle `@mathinpython`
113142
- Her [GitHub Page](https://github.com/empet) with Username `empet`
114143

115-
```python
116-
from scipy import stats
117-
stats.scoreatpercentile(volume, 99.5)
118-
```
119144

120145
#### Reference
121146
For additional information and help setting up a slider in an animation, see https://plot.ly/python/gapminder-example/. For more documentation on creating animations with Plotly, see https://plot.ly/python/#animations.

0 commit comments

Comments
 (0)