问题描述
我已经尝试过matplotlib教程中的PolyCollection示例,并注意到了一件奇怪的事情.我无法从轴原点中删除这些点,请参见图.我该如何管理?
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?
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()
推荐答案
这是一个带有PolyCollection显式关闭功能的错误.
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)
这里唯一的问题是你不应该在运行时得到你期望的结果,因为多边形不应该被封闭.这是另一个,与 3D 代码相关的错误.无论如何,这只会影响边缘的线条,而在您的示例中,它几乎没有任何区别(我本来以为直到我增加线宽才正确关闭它).
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使用path.Path对象存储顶点,对于封闭的多边形,使用CLOSEPOLY顶点代码,该代码可以清晰地封闭路径(在行中不重叠).
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).
PolyCollections 的 3D 投影代码似乎是一个黑客,它获取您的 PolyCollection,提取路径,从这些路径中获取顶点,将这些顶点的代码扔掉并假设它们都是真实的顶点坐标,然后直接修改原始PolyCollection上的顶点,以使用具有2D屏幕投影 坐标且没有代码的新路径,并且无论设置如何,都将关闭.
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.
我已将此作为问题提交#2045.
I've filed this as issue #2045.
这篇关于无法删除matplotlib polycollection中的原点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!