本文介绍了使用PolyCollection的matplotlib mplot3d图中出现的奇数行伪影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用mplot3d示例,该示例使用PolyCollection堆叠XY曲线, http://matplotlib.org/examples/mplot3d/polys3d_demo.html

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.

  • 如何删除消失在可见区域之外的水平线?
  • 是否有更好的方法来沿深度方向叠加XY图?

以下脚本生成了该图,

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()

推荐答案

如果仍然存在此问题,请尝试手动更改相应的Path对象.您应该将最后一个顶点的代码修改为STOP(代码0):

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:

这篇关于使用PolyCollection的matplotlib mplot3d图中出现的奇数行伪影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 23:46