Matplotlib 3D plotting: how to import

For 3D plotting to work in Matplotlib, you must have imports in this order BEFORE any other import that imports Matplotlib.

#!/usr/bin/env python
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
  • If your program has multiple entry points, put the above 3 lines of code in both functions.</p>

Else, if you try importing Axes3D in a later function that actually does the 3D plotting, it may give a RuntimeError.