Ask Your Question
2

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

asked 2014-10-23 17:37:12 +0200

FrédéricC gravatar image

updated 2015-01-13 20:38:39 +0200

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

Comments

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

FrédéricC gravatar imageFrédéricC ( 2014-10-24 09:42:36 +0200 )edit

2 Answers

Sort by » oldest newest most voted
1

answered 2014-10-23 21:41:49 +0200

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
1

answered 2014-10-23 20:06:29 +0200

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)]
rim_polygon(L,color='red',alpha=0.5,thickness=4,axes=False)
edit flag offensive delete link more

Comments

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

FrédéricC gravatar imageFrédéricC ( 2014-10-23 20:28:26 +0200 )edit

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

ndomes gravatar imagendomes ( 2014-10-24 00:45:40 +0200 )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

Stats

Asked: 2014-10-23 17:37:12 +0200

Seen: 3,667 times

Last updated: Oct 23 '14