Skip to content

Instantly share code, notes, and snippets.

@jackparmer
Last active September 24, 2019 04:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jackparmer/1b261bf530a41b164ea40d4d6dabb225 to your computer and use it in GitHub Desktop.
Save jackparmer/1b261bf530a41b164ea40d4d6dabb225 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
import dash
import dash_core_components as dcc
import dash_html_components as html
import numpy as np
import plotly.graph_objects as go
from scipy import ndimage
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
# Generate nicely looking random 3D-field
np.random.seed(0)
l = 30
X, Y, Z = np.mgrid[:l, :l, :l]
vol = np.zeros((l, l, l))
pts = (l * np.random.rand(3, 15)).astype(np.int)
vol[tuple(indices for indices in pts)] = 1
vol = ndimage.gaussian_filter(vol, 4)
vol /= vol.max()
fig = go.Figure(data=go.Volume(
x=X.flatten(), y=Y.flatten(), z=Z.flatten(),
value=vol.flatten(),
isomin=0.2,
isomax=0.7,
opacity=0.1,
surface_count=25,
slices_z=dict(fill=1, show=True, locations=[10.0]),
surface=dict(fill=1),
caps= dict(x_show=False, y_show=False, z_show=False), # no caps
))
fig.layout = dict(
uirevision=True,
height=600
)
camera = dict(eye=dict(x=2.5, y=0., z=0.8))
fig.update_layout(scene_camera=camera)
app.layout = html.Div([
html.Div([
html.Div([
html.P(children='volume fill'),
dcc.Slider(id='fill', min=0, max=1, step=0.1, value=1),
html.P(children='z slice position'),
dcc.Slider(id='z_slider', min=0, max=29.0, step=0.1, value=10.0),
dcc.Graph(id='volume', figure=fig)
], className="six columns"),
html.Div([
dcc.Graph(id='contour', figure={})
], className="six columns"),
], className="row")
])
@app.callback(
[dash.dependencies.Output('volume', 'figure'),
dash.dependencies.Output('contour', 'figure')],
[dash.dependencies.Input('z_slider', 'value'),
dash.dependencies.Input('fill', 'value')])
def move_slice_v(slider_position, fill):
fig['data'][0]['slices_z']['locations'] = [slider_position]
fig['data'][0]['surface']['fill'] = fill
index = int(slider_position/29.0 * 29.0)
contour_data = vol[:,:,index]
contour = go.Figure(
data=go.Contour(z = contour_data, connectgaps=False),
layout=go.Layout(
height=500, width=500,
title='z-plane: z={0}'.format(slider_position),
yaxis=go.YAxis(autorange="reversed")
)
)
return fig, contour
if __name__ == '__main__':
app.run_server(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment