python - How to use PolyCollection in matplotlib.collections? -
i don't quite understand function of statement:
return [(xlist[0], 0.)] + list(zip(xlist, ylist)) + [(xlist[-1], 0.)]
[(xlist[0], 0.)]
seems weird me. why have add vertices y=0 @ beginning , end of list? puzzles me. list(zip(xlist, ylist)) seems adequate me, depicts beginning , end of polygon.
the webpage of code: http://matplotlib.org/devdocs/gallery/mplot3d/polys3d.html
from mpl_toolkits.mplot3d import axes3d matplotlib.collections import polycollection import matplotlib.pyplot plt matplotlib import colors mcolors import numpy np # fixing random state reproducibility np.random.seed(19680801) def cc(arg): ''' shorthand convert 'named' colors rgba format @ 60% opacity. ''' return mcolors.to_rgba(arg, alpha=0.6) def polygon_under_graph(xlist, ylist): ''' construct vertex list defines polygon filling space under (xlist, ylist) line graph. assumes xs in ascending order. ''' return [(xlist[0], 0.)] + list(zip(xlist, ylist)) + [(xlist[-1], 0.)] fig = plt.figure() ax = fig.gca(projection='3d') # make verts list, verts[i] list of (x,y) pairs defining polygon verts = [] # set x sequence xs = np.linspace(0., 10., 26) # ith polygon appear on plane y = zs[i] zs = range(4) 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()
if you're unsure purpose of code find, first thing might try out happens if leave out.
so let's remove [(xlist[0], 0.)]
, [(xlist[-1], 0.)]
, i.e. 2 additional points collection , see happens (in 2d case, might easier see difference):
from matplotlib.collections import polycollection import matplotlib.pyplot plt import numpy 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)) ### 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()
the 2 points want leave out, plotted scatter see difference more clearly. given comparisson, i'm not sure if necessary add more explanation, think speaks itself.
Comments
Post a Comment