我正在尝试创建自己的3D多边形图版本,如Matplotlib网站上所示:

http://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html#polygon-plots

我的版本在这里:

http://hastebin.com/laqekeceru.py

示例数据:

http://hastebin.com/vocagogihu.coffee

来自两个不同视点的图像输出在这里:




如您在图像中所见,图形的基线稳定地开始向上倾斜。

我尝试了教程版本,但效果很好,但数据量明显减少。

这是matplotlib中的错误吗?
是我的代码吗?

我在用着:


Windows 7的
Python 2.6
numpy == 1.8.0
matplotlib == 1.3.1(最新版本)


提前致谢。

最佳答案

这与多边形有关,与3d无关。

PolyCollection是形成闭合多边形的点的集合。 “爬行”基线实际上是多边形的一侧,即从每个多边形的最后一个点到第一个点的隐含线。

为了说明这一点,请参见:

import matplotlib.pyplot as plt
from matplotlib.collections import PolyCollection

# some sample data
data = [(0,.5), (1,.7), (2,.3), (3,.6), (4,.2)]

fig = plt.figure()
ax = fig.add_subplot(111)

# draw the polygon
p = PolyCollection([data], facecolor=(1,0,0,.5), edgecolor='none')
ax.add_collection(p)

# draw the line
d = np.array(data)
ax.plot(*zip(*data), color='k', linewidth=2)


这个简单的例子给出:



要解决此问题,您将需要在多边形的末端添加零:

import matplotlib.pyplot as plt
from matplotlib.collections import PolyCollection

# some sample data
data = [(0,.5), (1,.7), (2,.3), (3,.6), (4,.2)]

fig = plt.figure()
ax = fig.add_subplot(111)

# draw the polygon
# pad the data:
data.insert(0, (data[0][0],0))
data.append((data[-1][0], 0))

p = PolyCollection([data], facecolor=(1,0,0,.5), edgecolor='none')
ax.add_collection(p)

# draw the line, note that the padded points are not drawn
d = np.array(data)
ax.plot(*zip(*data[1:-1]), color='k', linewidth=2)


现在,倾斜的线消失了:



如果需要在多边形的边缘放置曲线,则需要单独绘制它(因为多边形的边缘是您不想显示的东西)。

关于python - matplotlib基线中的3d多边形图倾斜,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25044549/

10-14 18:11
查看更多