peacedomi
peacedomi

Reputation: 11

How to use PolyCollection in matplotlib.collections?

I don't quite understand the function of this statement:

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

[(xlist[0], 0.)] seems weird to me. why do I have to add vertices with y=0 at the beginning and end of the list? This puzzles me. list(zip(xlist, ylist)) seems to be adequate for me, which already depicts the beginning and the end of a polygon.

The webpage of this code: 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()

Upvotes: 0

Views: 2680

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339280

If you're unsure about the purpose of some code you find, the first thing to do might be to try out what happens if you leave it out.

So let's remove the [(xlist[0], 0.)] and [(xlist[-1], 0.)], i.e. the two additional points from the collection and see what happens (in a the 2D case, where it might be easier to see the difference):

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

The two points that you want to leave out, are plotted as scatter to see the difference even more clearly. Given that comparisson, I'm not sure if it is necessary to add some more explanation, I think it speaks for itself.

Upvotes: 2

Related Questions