-1
我不太明白這句話的功能:如何在matplotlib.collections中使用PolyCollection?
return [(xlist[0], 0.)] + list(zip(xlist, ylist)) + [(xlist[-1], 0.)]
[(xlist[0], 0.)]
似乎怪我。 爲什麼我必須在列表的開始和結尾添加y = 0的頂點?這讓我感到困惑。列表(zip(xlist,ylist))對我來說似乎已經足夠了,它已經描述了多邊形的開始和結束。
這段代碼的網頁: http://matplotlib.org/devdocs/gallery/mplot3d/polys3d.html
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.collections import PolyCollection
import matplotlib.pyplot as plt
from matplotlib import colors as mcolors
import numpy as np
# Fixing random state for reproducibility
np.random.seed(19680801)
def cc(arg):
'''
Shorthand to convert 'named' colors to rgba format at 60% opacity.
'''
return mcolors.to_rgba(arg, alpha=0.6)
def polygon_under_graph(xlist, ylist):
'''
Construct the vertex list which defines the polygon filling the space under
the (xlist, ylist) line graph. Assumes the xs are in ascending order.
'''
return [(xlist[0], 0.)] + list(zip(xlist, ylist)) + [(xlist[-1], 0.)]
fig = plt.figure()
ax = fig.gca(projection='3d')
# Make verts a list, verts[i] will be a list of (x,y) pairs defining polygon i
verts = []
# Set up the x sequence
xs = np.linspace(0., 10., 26)
# The ith polygon will appear on the plane y = zs[i]
zs = range(4)
for i in zs:
ys = np.random.rand(len(xs))
verts.append(polygon_under_graph(xs, ys))
poly = PolyCollection(verts, facecolors=[cc('r'), cc('g'), cc('b'), cc('y')])
ax.add_collection3d(poly, zs=zs, zdir='y')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_xlim(0, 10)
ax.set_ylim(-1, 4)
ax.set_zlim(0, 1)
plt.show()
你到底明白了什麼?它創建一個Nx2的多邊形頂點的x,y座標列表。它在列表的開始和結尾處添加y = 0的頂點以定義多邊形的底部。 – tom
這樣的問題很難給出答案。雖然大多數人閱讀這個問題可能會知道問題所在,但他們仍然無法解釋它,因爲你沒有說明你不理解這條線。既然這對你來說可能很難,那麼這個想法就是你告訴人們你所瞭解的東西。這可以讓人們在沒有他們的答案作爲一個完整的Python教程的情況下幫助你。 – ImportanceOfBeingErnest
爲什麼我必須在列表的開始和結尾添加y = 0的頂點?這讓我感到困惑。列表(zip(xlist,ylist))對我來說似乎足夠了,它描述了多邊形的開始和結束。 – peacedomi