7

I have tried an example with PolyCollection from matplotlib tutorials and noticed one strange thing. I couldn't remove this points from axes origin see fig. How do I manage this?

enter image description here

from mpl_toolkits.mplot3d import Axes3D
from matplotlib.collections import PolyCollection
from matplotlib.colors import colorConverter
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.gca(projection='3d')

cc = lambda arg: colorConverter.to_rgba(arg, alpha=0.6)

xs = np.arange(5, 10, 0.4)
verts = []
zs = [0.0, 1.0, 2.0, 3.0]
for z in zs:
    ys = np.random.rand(len(xs))
    ys[0], ys[-1] = 0.1, 0
    verts.append(list(zip(xs, ys)))

poly = PolyCollection(verts, facecolors = [cc('r'), cc('g'), cc('b'),
                                           cc('y')])
poly.set_alpha(0.7)
ax.add_collection3d(poly, zs=zs, zdir='y')

ax.set_xlabel('X')
ax.set_xlim3d(0, 10)
ax.set_ylabel('Y')
ax.set_ylim3d(-1, 4)
ax.set_zlabel('Z')
ax.set_zlim3d(0, 1)

plt.show()
anatoly
  • 327
  • 2
  • 13
  • 1
    A bug with the 3D code, adding the same ploy collection to a 2D axes gives the expected results. – tacaswell Apr 30 '13 at 19:04
  • and how should I do with this bug? – anatoly May 04 '13 at 20:19
  • I've just posted a quick fix to this problem here: http://stackoverflow.com/questions/19492007/odd-line-artefacts-appearing-in-matplotlib-mplot3d-plot-using-polycollection/ – tnorgd Dec 30 '13 at 18:48

1 Answers1

4

This is a bug with the explicit closing feature of PolyCollection.

For now, turn that off, and you'll get what I think is the result you expect:

poly = PolyCollection(verts, facecolors = [cc('r'), cc('g'), cc('b'),
                                           cc('y')], closed=False)

The only problem here is that you shouldn't get the results you expect while running this, because the polygon shouldn't be closed. This is another, related bug with the 3D code. In any case, this only affects the line around the edge, and in your example it barely makes any difference (I originally thought it was correctly not closed until I increased the linewidth).

PolyCollection uses path.Path objects to store the vertexes, and for closed polygons, uses the CLOSEPOLY vertex code, which cleanly closes the path (no overlap in the line).

The 3D projection code for PolyCollections seems to be rather a hack which takes your PolyCollection, extracts the paths, takes the vertexes from those paths, throwing the codes for those vertexes away and assuming they're all real vertex coordinates, and then directly modifies the vertexes on your original PolyCollection to use new paths that have 2D screen projected coordinates with no codes... and regardless of your settings, are closed.

I've filed this as issue #2045.

cge
  • 9,552
  • 3
  • 32
  • 51
  • this does not help, and the problem remains. Any other solution for plotting waterfall plots? – B.Kocis Apr 10 '15 at 07:47
  • 1
    I'm not sure what it is that doesn't help: this is an answer about working around a bug that is now fixed in matplotlib. Current versions should work with or without `closed=False`. What version of matplotlib are you running? – cge Apr 10 '15 at 21:23
  • matplotlib 1.3.1 and the comment was on the fact that the `closed=False` did not solve the problem; the plotted line gets closed, however when using `closed=False` matplotlib closes the polygon with a line connecting the first point to last datapoint. (just to add that I wanted to plot a series of curves - lineplots) – B.Kocis Apr 12 '15 at 22:03
  • That's not what this question and answer is about; this is about (0,0) being added to the data when `closed=True`, which no longer happens. – cge Apr 17 '15 at 19:51