How to create an interactive 3D chart and share it easily with anyone

This is a short tutorial on a great Plotly feature.



By Olga Chernytska, Senior Machine Learning Engineer

3D charts have one huge disadvantage. When creating a 3D chart, you can visualize data from different angles/scales, or use an interactive library for that, so you’ll able to understand data quite well. However, when created, these charts are hard to share.

People usually convert 3D charts into a single static image or a set of images taken from different angles and with different scales. With such an approach some important information may be lost, or hard to understand, so we need a better option. And it does exist.

In this short tutorial, I’ll show you how to create an interactive 3D chart and easily share it which anyone – no matter, they are data scientists, developers, managers, or even your non-technical friends that don’t have any coding environment installed. And the shared chart will be fully interactive as well.

 

How it works

 
Probably, you’ve heard about plotly, an open-source library that allows creating interactive charts in Jupyter Notebooks. I’ve used this library for a while and also shared the charts as static images until my college showed me a great plotly feature:

You can save your interactive charts as HTML files – and when opened in a browser, they are fully interactive.

 

Demo

 
Let’s create a visualization of a simple 3D function – parabola. First, we calculate function values on a grid (x,y) with numpy.meshgrid. Then we use plotly to plot the surface and save it as HTML file. Below is a code for that (based on documentation).

import numpy as np
import plotly.graph_objects as go

def parabola_3d(x, y):
    z = x ** 2 + y ** 2
    return z

x = np.linspace(-1, 1, 50)
y = np.linspace(-1, 1, 50)
xv, yv = np.meshgrid(x, y)
z = parabola_3d(xv, yv)

fig = go.Figure(data=[go.Surface(z=z, x=x, y=y)])
fig.write_html("file.html")


And here is how the result looks like. This HTML file can be opened in any browser, embedded on the website, and sent via messengers. The best thing here is that to open this file – neither Jupyter Notebook nor plotly library is needed.

Image 1. How HTML file looks when opened in browser.

 

Download this HTML file and try it yourself.

 

Installation and More

 
I remember plotly being an overcomplicated library with very limited functionality, but within last years it grew up in something really cool within last years. For instance, there are 13 types of 3D charts available, and dozens of maps, statistics, scientific and financial 2D charts.

Image 2. Examples of 3D charts available in plotly. Source

 

If you are interested and want to try plotly, here is documentation on how to install it and build your first chart. Worth trying for sure!

 
Bio: Olga Chernytska is a Senior Machine Learning Engineer in a large Eastern European outsourcing company; was involved in various data science projects for top US, European and Asian companies; main specialization and interest is Deep Computer Vision.

Original. Reposted with permission.

Related: