Ask Your Question

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

asked 2014-10-23 10:37:12 -0500

FrédéricC gravatar image

updated 2015-01-13 13:38:39 -0500

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 flag offensive close merge delete


This is now Needs review.

FrédéricC gravatar imageFrédéricC ( 2014-10-24 02:42:36 -0500 )edit

2 answers

Sort by » oldest newest most voted

answered 2014-10-23 14:41:49 -0500

kcrisman gravatar image

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.

edit flag offensive delete link more

answered 2014-10-23 13:06:29 -0500

ndomes gravatar image

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)]
edit flag offensive delete link more


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

FrédéricC gravatar imageFrédéricC ( 2014-10-23 13:28:26 -0500 )edit

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

ndomes gravatar imagendomes ( 2014-10-23 17:45:40 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2014-10-23 10:37:12 -0500

Seen: 2,094 times

Last updated: Oct 23 '14