Beyond data scientist: 3d plots in Python with examples

Yuchen Z.
6 min readJul 18, 2019

Python is known to be good for data visualization. There are many tools in Python enabling it to do so: matplotlib, pygal, Seaborn, Plotly, etc. Among these, matplotlib is probably the most widely used one. On one hand, it offers a lot more flexibility; on the other hand, it is also very low-level and may not the most straightforward to use. There are a lot of articles explaining how to do 2d plotting with matplotlib already. In this post, we will focus more on plotting in 3d.

You can simply read through this as it. But to get the most out of these examples here, it is recommended that you open up a notebook and try them out.

Get started

Here are some necessary dependencies we will need:

  • matplotlib.pyplot of cause
  • mpl_toolkits.mplot3d for creating the 3d projection
  • numpy for manipulating data

We add %matplotlib inline so that we can display the plots inline in the notebook. This saves us from having to call all the time. Alternatively, you could also use %matplotlib notebook to enter interactive mode, which allows you to simply click and drag to change the viewpoint.

We also change the default plot size here using rcParams. There are many more configurations that you can play with if you are interested. Assuming you are using a Jupyter notebook, then the size 12.8, 9.6 is perfect (by some definition 🤓).

from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams["figure.figsize"] = 12.8, 9.6

To begin with, let’s create a 3d axes. We pass projection='3d' to plt.exes, which returns an Axes3DSubplot object here. This is the empty canvas that we'll be painting on.

ax = plt.axes(projection='3d')

Laplacian of Gaussian

In this blog post, we’ll be using Laplacian of Gaussian (LoG), which is a filter that is often used for edge detection in computer vision. We won’t cover much detail about using the filter itself. However, for a given point x and y, its value is derived with the following formula:

Yuchen Z.

Engineer @ Meta & Hopper