Matplotlib 3-D mesh wiregrid example

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

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 files.

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


Note: You can also use Mayavi for advanced 3-D plotting in Python

3-D mesh wiregrid: minimal working example for the current version of Matplotlib. You can pan, rotate, etc. in the figure window that opens.

from mpl_toolkits.mplot3d import Axes3D # this line must come before the next line!
from matplotlib.pyplot import figure,show
import numpy as np

def plot3():
    x,y = np.meshgrid(np.linspace(0,2*np.pi), np.linspace(0,2*np.pi))

    z = np.sin(x+0.5*y)
    ax = figure().gca(projection='3d')

if __name__ == '__main__':

Leave a Comment