add_collection3d and PatchCollection

I’m trying to display several rectangles (from matplotlib.patches) in 3D. I thought it would be possible to use the add_collection3d method similar to the demo on the examples page:

http://matplotlib.sourceforge.net/mpl_toolkits/mplot3d/tutorial.html

However, when I call this method on a PatchCollection, I get an error at line 295 in art3d.py.

It expects the collection to have offsets, but offsets is None (by default) when I create the collection.

/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/mpl_toolkits/mplot3d/art3d.pyc in set_3d_properties(self, zs, zdir)

293

294 def set_3d_properties(self, zs, zdir):

–> 295 xs, ys = zip(*self.get_offsets())

296 self._offsets3d = juggle_axes(xs, ys, zs, zdir)

297 self._facecolor3d = self.get_facecolor()

ValueError: need more than 0 values to unpack

I am relatively new to Matplotlib, so I don’t know if this is a bug or it is just my own misunderstanding.

Here’s a simple example where I try it on a single rectangle and it fails with the above error message.

----start example

from mpl_toolkits.mplot3d import Axes3D

from matplotlib.collections import PolyCollection,PatchCollection

from matplotlib.colors import colorConverter

import matplotlib.pyplot as plt

from matplotlib.patches import Rectangle

import numpy as np

fig = plt.figure()

ax = fig.gca(projection=‘3d’)

rec = Rectangle((5,2),2,2)

recs = [rec]

col = PatchCollection(recs)

ax.add_collection3d(col, zs=[0.6], zdir=‘z’)

----end example

Graham

Graham,

Good catch, it is a bug. It is similar to one I encountered recently but have not gotten a response for.

The issue is that Patch3DCollection seems to be assuming that .set_offset() has been called on the original PatchCollection object, thereby setting the internal offsets. And, even if you do manually call .set_offsets(), the rest of the mplot3d code assumes that Patch3DCollection has .set_sort_zpos(), but it doesn’t.

Offsets seem to be hardly used in 2d plotting, and the Patch3DCollection object code appears to have not been kept up to date with the rest of matplotlib and mplot3d. Can anyone shed some insight into this?

Ben Root

···

On Mon, Aug 16, 2010 at 5:20 PM, Graham Taylor <essex_green@…878…32…> wrote:

I’m trying to display several rectangles (from matplotlib.patches) in 3D. I thought it would be possible to use the add_collection3d method similar to the demo on the examples page:

http://matplotlib.sourceforge.net/mpl_toolkits/mplot3d/tutorial.html

However, when I call this method on a PatchCollection, I get an error at line 295 in art3d.py.

It expects the collection to have offsets, but offsets is None (by default) when I create the collection.

/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/mpl_toolkits/mplot3d/art3d.pyc in set_3d_properties(self, zs, zdir)

293

294 def set_3d_properties(self, zs, zdir):

→ 295 xs, ys = zip(*self.get_offsets())

296 self._offsets3d = juggle_axes(xs, ys, zs, zdir)

297 self._facecolor3d = self.get_facecolor()

ValueError: need more than 0 values to unpack

I am relatively new to Matplotlib, so I don’t know if this is a bug or it is just my own misunderstanding.

Here’s a simple example where I try it on a single rectangle and it fails with the above error message.

----start example

from mpl_toolkits.mplot3d import Axes3D

from matplotlib.collections import PolyCollection,PatchCollection

from matplotlib.colors import colorConverter

import matplotlib.pyplot as plt

from matplotlib.patches import Rectangle

import numpy as np

fig = plt.figure()

ax = fig.gca(projection=‘3d’)

rec = Rectangle((5,2),2,2)

recs = [rec]

col = PatchCollection(recs)

ax.add_collection3d(col, zs=[0.6], zdir=‘z’)

----end example

Graham