#!/usr/bin/env python # coding: utf-8 # # ipyvolume: 3D plotting in the notebook # # ## https://github.com/maartenbreddels/ipyvolume # # IPyvolume is a Python library to visualize 3d volumes and glyphs (e.g. 3d scatter plots), in the Jupyter notebook, with minimal configuration and effort. It is currently pre-1.0, so use at own risk. IPyvolume’s volshow is to 3d arrays what matplotlib’s imshow is to 2d arrays. # # - MIT Licensed # # By Maarten Breddels # # **Installation:** # # ```bash # conda install -c conda-forge ipyvolume # ``` # # 3-D Scatter Plots # In[ ]: import ipyvolume as ipv import numpy as np import ipywidgets as widgets # In[ ]: x, y, z = np.random.random((3, 10000)) ipv.figure() scatter = ipv.scatter(x, y, z, size=1, marker="sphere") ipv.show() # In[ ]: ipv.save('standalone.html') # In[ ]: get_ipython().system('open standalone.html') # In[ ]: x, y, z, u, v, w = np.random.random((6, 1000))*2-1 selected = np.random.randint(0, 1000, 100) fig = ipv.figure() quiver = ipv.quiver(x, y, z, u, v, w, size=5, size_selected=8, selected=selected) ipv.show() # In[ ]: size = widgets.FloatSlider(min=0, max=30, step=0.1) size_selected = widgets.FloatSlider(min=0, max=30, step=0.1) color = widgets.ColorPicker() color_selected = widgets.ColorPicker() widgets.jslink((quiver, 'size'), (size, 'value')) widgets.jslink((quiver, 'size_selected'), (size_selected, 'value')) widgets.jslink((quiver, 'color'), (color, 'value')) widgets.jslink((quiver, 'color_selected'), (color_selected, 'value')) widgets.VBox([size, size_selected, color, color_selected]) # # Animations # In[ ]: # create 2d grids: x, y, and r u = np.linspace(-10, 10, 25) x, y = np.meshgrid(u, u) r = np.sqrt(x**2+y**2) print("x,y and z are of shape", x.shape) # and turn them into 1d x = x.flatten() y = y.flatten() r = r.flatten() print("and flattened of shape", x.shape) # In[ ]: # create a sequence of 15 time elements time = np.linspace(0, np.pi*2, 15) z = np.array([(np.cos(r + t) * np.exp(-r/5)) for t in time]) print("z is of shape", z.shape) # In[ ]: # draw the scatter plot, and add controls with animate_glyphs ipv.figure() s = ipv.scatter(x, z, y, marker="sphere") ipv.animation_control(s, interval=200) ipv.ylim(-3,3) ipv.show() # In[ ]: # Now also include, color, which containts rgb values color = np.array([[np.cos(r + t), 1-np.abs(z[i]), 0.1+z[i]*0] for i, t in enumerate(time)]) size = (z+1) print("color is of shape", color.shape) # In[ ]: color = np.transpose(color, (0, 2, 1)) # flip the last axes # In[ ]: ipv.figure() s = ipv.scatter(x, z, y, color=color, size=size, marker="sphere") ipv.animation_control(s, interval=200) ipv.ylim(-3, 3) ipv.show() # In[ ]: