0

I am using the mplot3d example which uses PolyCollection for stacking XY-plots, http://matplotlib.org/examples/mplot3d/polys3d_demo.html

However, I am seeing some strange line artefacts in the plots.

  • How can I remove the horizontal lines which disappear out of the visible area?
  • Is there a better way for stacking XY plot along a depth direction?

The following script produces this plot,

Odd lines in mplot3d plot

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

zs = []
fig = plt.figure()
ax = fig.gca(projection='3d')
verts = []

# XY data (i.e. "normal line plots")
count = 4
for i in range(count):
    xs, ys = [800.0, 900.0, 1000.0, 1100.],  [0., 1., 1., 0.]
    verts.append(list(zip(xs, ys)))

# Z position (i.e. depth at which the XY plot is drawn)
zs = [0,1,2,3]

colours = plt.cm.Blues(np.linspace(0.2, 0.8, len(zs)))
poly = PolyCollection(verts, facecolors = colours )
ax.add_collection3d(poly, zs=zs, zdir='y')

ax.set_xlabel('X')
ax.set_xlim3d(800,1150)
ax.set_ylabel('Y')
ax.set_ylim3d(0, 4)
ax.set_zlabel('Z')
ax.set_zlim3d(0, 1)
plt.show()
Daniel Farrell
  • 9,316
  • 8
  • 39
  • 62
  • That looks like a bug to me. You should probably add a ticket at the matplotlib bug tracker. – David Zwicker Oct 21 '13 at 10:50
  • The script I posted is a slightly modified version of the example script, basically just changed the data. Why do you think the example works an this doesn't? Definitely a bug? – Daniel Farrell Oct 21 '13 at 11:26
  • The example also shows the bug, when you change the x-coordinates. matplotlib seems to insert an additional point at (0, 0) to the polygons. This looks like a bug to me. – David Zwicker Oct 21 '13 at 11:52
  • I hadn't appreciated that, thanks for pointing it out. I added an issue on github, https://github.com/matplotlib/matplotlib/issues/2533. You don't happen to know an alternative way of z-stacking xy-plots do you? Maybe I need to use gnuplot? – Daniel Farrell Oct 21 '13 at 12:52
  • http://stackoverflow.com/questions/16305256/couldnt-remove-origin-point-in-matplotlib-polycollection – tacaswell Oct 21 '13 at 15:00
  • Thanks @tcaswell. `PolyCollection(... closed=False)` does the trick. – Daniel Farrell Oct 21 '13 at 15:40

1 Answers1

1

If you still have the this is problem, try to manually alter the respective Path objects. You should modify the code of the very last vertex to STOP (code 0):

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

zs = []
fig = plt.figure()
ax = fig.gca(projection='3d')
verts = []

# XY data (i.e. "normal line plots")
count = 4
for i in range(count):
    xs, ys = [800.0, 900.0, 1000.0, 1100.],  [0., 1., 1., 0.]
    verts.append(list(zip(xs, ys)))

# Z position (i.e. depth at which the XY plot is drawn)
zs = [0,1,2,3]

colours = plt.cm.Blues(np.linspace(0.2, 0.8, len(zs)))
poly = PolyCollection(verts, facecolors = colours )

for path in poly.get_paths() :  # There is the fix :
  path.codes[-1] = 0        # we have to manually switch the last point in a path to STOP (code = 0)

ax.add_collection3d(poly, zs=zs, zdir='y')

ax.set_xlabel('X')
ax.set_xlim3d(800,1150)
ax.set_ylabel('Y')
ax.set_ylim3d(0, 4)
ax.set_zlabel('Z')
ax.set_zlim3d(0, 1)
plt.show()

... and here is the result: enter image description here

tnorgd
  • 1,580
  • 2
  • 14
  • 24