## Plotting 3D Polygons

I think you’ve almost got it. Is this what you want? from mpl_toolkits.mplot3d import Axes3D from mpl_toolkits.mplot3d.art3d import Poly3DCollection import matplotlib.pyplot as plt fig = plt.figure() ax = Axes3D(fig, auto_add_to_figure=False) fig.add_axes(ax) x = [0,1,1,0] y = [0,0,1,1] z = [0,1,0,1] verts = [list(zip(x,y,z))] ax.add_collection3d(Poly3DCollection(verts)) plt.show() You might also be interested in art3d.pathpatch_2d_to_3d.

## Annotating a 3D scatter plot

Maybe easier via ax.text(…): from matplotlib import pyplot from mpl_toolkits.mplot3d import Axes3D from numpy.random import rand from pylab import figure m=rand(3,3) # m is an array of (x,y,z) coordinate triplets fig = figure() ax = fig.add_subplot(projection=’3d’) for i in range(len(m)): #plot each point + it’s index as text above ax.scatter(m[i,0],m[i,1],m[i,2],color=”b”) ax.text(m[i,0],m[i,1],m[i,2], ‘%s’ % (str(i)), size=20, … Read more

## How to set the ‘equal’ aspect ratio for all axes (x, y, z)

I like some of the previously posted solutions, but they do have the drawback that you need to keep track of the ranges and means over all your data. This could be cumbersome if you have multiple data sets that will be plotted together. To fix this, I made use of the ax.get_[xyz]lim3d() methods and … Read more

## Plotting a 3d cube, a sphere and a vector

It is a little complicated, but you can draw all the objects by the following code: from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt import numpy as np from itertools import product, combinations fig = plt.figure() ax = fig.gca(projection=’3d’) ax.set_aspect(“equal”) # draw cube r = [-1, 1] for s, e in combinations(np.array(list(product(r, r, r))), 2): … Read more

## Plotting implicit equations in 3d

You can trick matplotlib into plotting implicit equations in 3D. Just make a one-level contour plot of the equation for each z value within the desired limits. You can repeat the process along the y and z axes as well for a more solid-looking shape. from mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot as plt import numpy … Read more

## Creating intersecting images with imshow or other function

There might be better ways, but at least you can always make a planar mesh and color it: import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D import numpy as np # create a 21 x 21 vertex mesh xx, yy = np.meshgrid(np.linspace(0,1,21), np.linspace(0,1,21)) # create some dummy data (20 x 20) for the image data … Read more

## Image overlay in 3d plot

I did a 3d surface plot overlay on top of a background image once: If this is similar to what you want, I could try to make a working example out of it. Alternatively, if you just want to display an image in 3d space, you can use a surface plot: from pylab import * … Read more

## How to make a 3D scatter plot

You can use matplotlib for this. matplotlib has a mplot3d module that will do exactly what you want. import matplotlib.pyplot as plt import random fig = plt.figure(figsize=(12, 12)) ax = fig.add_subplot(projection=’3d’) sequence_containing_x_vals = list(range(0, 100)) sequence_containing_y_vals = list(range(0, 100)) sequence_containing_z_vals = list(range(0, 100)) random.shuffle(sequence_containing_x_vals) random.shuffle(sequence_containing_y_vals) random.shuffle(sequence_containing_z_vals) ax.scatter(sequence_containing_x_vals, sequence_containing_y_vals, sequence_containing_z_vals) plt.show() The code above generates a … Read more

## Plot 3d surface with colormap as 4th dimension, function of x,y,z

This answer addresses the 4d surface plot problem. It uses matplotlib’s plot_surface function instead of plot_trisurf. Basically you want to reshape your x, y and z variables into 2d arrays of the same dimension. To add the fourth dimension as a colormap, you must supply another 2d array of the same dimension as your axes … Read more