ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 24 Oct 2014 09:42:36 +0200How to plot a polygon with distinct colors for border and interior ?https://ask.sagemath.org/question/24600/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 ?Thu, 23 Oct 2014 17:37:12 +0200https://ask.sagemath.org/question/24600/how-to-plot-a-polygon-with-distinct-colors-for-border-and-interior/Comment by FrédéricC for <p>I would like to have a green polygon with black border. I can achieve this by superposing two polygons:</p>
<pre><code>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)
</code></pre>
<p>Is it possible to do that with one polygon ?</p>
https://ask.sagemath.org/question/24600/how-to-plot-a-polygon-with-distinct-colors-for-border-and-interior/?comment=24614#post-id-24614This is now http://trac.sagemath.org/ticket/17209. Needs review.Fri, 24 Oct 2014 09:42:36 +0200https://ask.sagemath.org/question/24600/how-to-plot-a-polygon-with-distinct-colors-for-border-and-interior/?comment=24614#post-id-24614Answer by ndomes for <p>I would like to have a green polygon with black border. I can achieve this by superposing two polygons:</p>
<pre><code>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)
</code></pre>
<p>Is it possible to do that with one polygon ?</p>
https://ask.sagemath.org/question/24600/how-to-plot-a-polygon-with-distinct-colors-for-border-and-interior/?answer=24604#post-id-24604You 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) Thu, 23 Oct 2014 20:06:29 +0200https://ask.sagemath.org/question/24600/how-to-plot-a-polygon-with-distinct-colors-for-border-and-interior/?answer=24604#post-id-24604Comment by ndomes for <p>You can use your own python function:</p>
<pre><code>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)
</code></pre>
https://ask.sagemath.org/question/24600/how-to-plot-a-polygon-with-distinct-colors-for-border-and-interior/?comment=24612#post-id-24612Indeed, but a polygon with distinct border is a sum of two graphic objects.Fri, 24 Oct 2014 00:45:40 +0200https://ask.sagemath.org/question/24600/how-to-plot-a-polygon-with-distinct-colors-for-border-and-interior/?comment=24612#post-id-24612Comment by FrédéricC for <p>You can use your own python function:</p>
<pre><code>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)
</code></pre>
https://ask.sagemath.org/question/24600/how-to-plot-a-polygon-with-distinct-colors-for-border-and-interior/?comment=24605#post-id-24605Well, this is essentially still a sum of two polygons..Thu, 23 Oct 2014 20:28:26 +0200https://ask.sagemath.org/question/24600/how-to-plot-a-polygon-with-distinct-colors-for-border-and-interior/?comment=24605#post-id-24605Answer by kcrisman for <p>I would like to have a green polygon with black border. I can achieve this by superposing two polygons:</p>
<pre><code>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)
</code></pre>
<p>Is it possible to do that with one polygon ?</p>
https://ask.sagemath.org/question/24600/how-to-plot-a-polygon-with-distinct-colors-for-border-and-interior/?answer=24608#post-id-24608Matplotlib polygons [do have `edgecolor`](http://matplotlib.org/api/patches_api.html#matplotlib.patches.Polygon) 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.Thu, 23 Oct 2014 21:41:49 +0200https://ask.sagemath.org/question/24600/how-to-plot-a-polygon-with-distinct-colors-for-border-and-interior/?answer=24608#post-id-24608