# How to plot a polygon with distinct colors for border and interior ?

I would like to have a green polygon with black border. I can achieve this by superposing two polygons:

sage: polygon([[0,0],[0,1],[2,1],[1,0]],color='limegreen',fill=True)+polygon([[0,0],[0,1],[2,1],[1,0]],color='black',fill=False)


Is it possible to do that with one polygon ?

edit retag close merge delete

This is now http://trac.sagemath.org/ticket/17209. Needs review.

( 2014-10-24 09:42:36 +0200 )edit

Sort by » oldest newest most voted

Matplotlib polygons do have edgecolor as a keyword. Currently the way Sage wraps these you might have to do a little work to get that to work out right. In principle the following should work, but it doesn't seem to:

sage: P = polygon([[0,0],[0,1],[2,1],[1,0]],color='limegreen', thickness=2)
sage: p = P.matplotlib()
sage: p.set_edgecolor('black')
sage: from matplotlib.backends.backend_agg import FigureCanvasAgg
sage: p.set_canvas(FigureCanvasAgg(p))
sage: p.savefig('test.png')


as can be seen by trying

sage: !open test.png


Matplotlib has added a lot of functionality we haven't wrapped properly yet.

more

You can use your own python function:

def rim_polygon(V,border_color='black',**kwargs):
G = polygon(V,**kwargs)
d = kwargs.get('thickness',1)
G += polygon(V,color=border_color,fill=False,thickness=d)
return G

L = [(0,0),(0,1),(2,1),(1,0)]
rim_polygon(L,color='red',alpha=0.5,thickness=4,axes=False)

more

Well, this is essentially still a sum of two polygons..

( 2014-10-23 20:28:26 +0200 )edit

Indeed, but a polygon with distinct border is a sum of two graphic objects.

( 2014-10-24 00:45:40 +0200 )edit