2017-08-14 28 views
-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() 
+0

你到底明白了什麼?它創建一個Nx2的多邊形頂點的x,y座標列表。它在列表的開始和結尾處添加y = 0的頂點以定義多邊形的底部。 – tom

+0

這樣的問題很難給出答案。雖然大多數人閱讀這個問題可能會知道問題所在,但他們仍然無法解釋它,因爲你沒有說明你不理解這條線。既然這對你來說可能很難,那麼這個想法就是你告訴人們你所瞭解的東西。這可以讓人們在沒有他們的答案作爲一個完整的Python教程的情況下幫助你。 – ImportanceOfBeingErnest

+0

爲什麼我必須在列表的開始和結尾添加y = 0的頂點?這讓我感到困惑。列表(zip(xlist,ylist))對我來說似乎足夠了,它描述了多邊形的開始和結束。 – peacedomi

回答

1

如果你不確定的一些代碼,你會發現的目的,要做的第一件事可能是嘗試,如果你離開它會發生什麼。

讓我們刪除[(xlist[0], 0.)][(xlist[-1], 0.)],即從集合中另外兩個點,看看會發生什麼(在二維情況下,它可能會更容易看出差別):

from matplotlib.collections import PolyCollection 
import matplotlib.pyplot as plt 
import numpy as np; np.random.seed(19680801) 


def polygon_under_graph(xlist, ylist): 
    return [(xlist[0], 0.)] + list(zip(xlist, ylist)) + [(xlist[-1], 0.)] 

def polygon_under_graph_without_endpoints(xlist, ylist): 
    return list(zip(xlist, ylist)) 


fig, (ax,ax2) = plt.subplots(nrows=2, figsize=(5,5), sharex=True, sharey=True) 

xs = np.linspace(0., 10., 26) 
ys = np.random.rand(len(xs)) 

### with endpoints ### 
verts = [] 
verts.append(polygon_under_graph(xs, ys)) 

poly = PolyCollection(verts, facecolors="limegreen") 
ax.add_collection(poly) 

### without endpoints ### 
verts2 = [] 
verts2.append(polygon_under_graph_without_endpoints(xs, ys)) 

poly2 = PolyCollection(verts2, facecolors="limegreen") 
ax2.add_collection(poly2) 

### limits, title, annotation ### 
ax.set_xlim(-1, 11) 
ax.set_ylim(-.5, 1.5) 

ax.set_title("with endpoints") 
ax2.set_title("without endpoints") 

ax.scatter([xs[0],xs[-1]],[0.,0.], label="endpoints") 
ax2.scatter([xs[0],xs[-1]],[0.,0.], label="endpoints") 

ax2.legend() 
plt.show() 

enter image description here

您想要忽略的兩點被繪製爲散點圖,以便更清楚地看到區別。鑑於這種比較,我不確定是否有必要增加一些解釋,我認爲這是自己說明的。