ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 24 Nov 2012 18:28:55 -0600Mystery white border around Graphics objecthttp://ask.sagemath.org/question/9546/mystery-white-border-around-graphics-object/If you try to show() a sage.plot.graphics.Graphics object, you will find something extremely bizarre: there is a white border around the .png image which is 30 pixels wide (on some sides, 31 pixels wide). There is no documentation of this and it seems to be impossible to remove with any documented options to show() or to the settings of a Graphics object.
Does anyone know how to get rid of this bug?
The best thing I came up with was to do
show(MyObject, axes=False, axes_pad=0)
This only reduced the number of pixels but didn't remove the border entirely.
Also note that you can sometimes make the graphics object transparent (though not through show()), but this does not remove the border, it only makes it transparent, which causes even more problems when I pipe this into other software.
**Example:**
polygon([(0,0),(1,0),(1,1),(0,1)],aspect_ratio=1,axes=False,figsize=1,axes_pad=0)
yields:
![example](https://dl.dropbox.com/u/4747602/sage2.png)Sun, 18 Nov 2012 19:08:08 -0600http://ask.sagemath.org/question/9546/mystery-white-border-around-graphics-object/Comment by marco for <p>If you try to show() a sage.plot.graphics.Graphics object, you will find something extremely bizarre: there is a white border around the .png image which is 30 pixels wide (on some sides, 31 pixels wide). There is no documentation of this and it seems to be impossible to remove with any documented options to show() or to the settings of a Graphics object. </p>
<p>Does anyone know how to get rid of this bug?</p>
<p>The best thing I came up with was to do </p>
<pre><code>show(MyObject, axes=False, axes_pad=0)
</code></pre>
<p>This only reduced the number of pixels but didn't remove the border entirely. <br/>
Also note that you can sometimes make the graphics object transparent (though not through show()), but this does not remove the border, it only makes it transparent, which causes even more problems when I pipe this into other software. </p>
<p><strong>Example:</strong></p>
<pre><code>polygon([(0,0),(1,0),(1,1),(0,1)],aspect_ratio=1,axes=False,figsize=1,axes_pad=0)
</code></pre>
<p>yields:</p>
<p><img alt="example" src="https://dl.dropbox.com/u/4747602/sage2.png"/></p>
http://ask.sagemath.org/question/9546/mystery-white-border-around-graphics-object/?comment=18683#post-id-18683I think that every function should have well-defined inputs and outputs. The output of plotting a graphics object is not well-defined. Surely you can see where I am coming from? Imagine if LaTeX created mystery spaces which weren't documented.Tue, 20 Nov 2012 16:06:13 -0600http://ask.sagemath.org/question/9546/mystery-white-border-around-graphics-object/?comment=18683#post-id-18683Comment by benjaminfjones for <p>If you try to show() a sage.plot.graphics.Graphics object, you will find something extremely bizarre: there is a white border around the .png image which is 30 pixels wide (on some sides, 31 pixels wide). There is no documentation of this and it seems to be impossible to remove with any documented options to show() or to the settings of a Graphics object. </p>
<p>Does anyone know how to get rid of this bug?</p>
<p>The best thing I came up with was to do </p>
<pre><code>show(MyObject, axes=False, axes_pad=0)
</code></pre>
<p>This only reduced the number of pixels but didn't remove the border entirely. <br/>
Also note that you can sometimes make the graphics object transparent (though not through show()), but this does not remove the border, it only makes it transparent, which causes even more problems when I pipe this into other software. </p>
<p><strong>Example:</strong></p>
<pre><code>polygon([(0,0),(1,0),(1,1),(0,1)],aspect_ratio=1,axes=False,figsize=1,axes_pad=0)
</code></pre>
<p>yields:</p>
<p><img alt="example" src="https://dl.dropbox.com/u/4747602/sage2.png"/></p>
http://ask.sagemath.org/question/9546/mystery-white-border-around-graphics-object/?comment=18690#post-id-18690Why do you assume this is a bug? Why not just crop the image to your liking before piping it to other (presumably delicate) software?Mon, 19 Nov 2012 17:17:14 -0600http://ask.sagemath.org/question/9546/mystery-white-border-around-graphics-object/?comment=18690#post-id-18690Answer by ppurka for <p>If you try to show() a sage.plot.graphics.Graphics object, you will find something extremely bizarre: there is a white border around the .png image which is 30 pixels wide (on some sides, 31 pixels wide). There is no documentation of this and it seems to be impossible to remove with any documented options to show() or to the settings of a Graphics object. </p>
<p>Does anyone know how to get rid of this bug?</p>
<p>The best thing I came up with was to do </p>
<pre><code>show(MyObject, axes=False, axes_pad=0)
</code></pre>
<p>This only reduced the number of pixels but didn't remove the border entirely. <br/>
Also note that you can sometimes make the graphics object transparent (though not through show()), but this does not remove the border, it only makes it transparent, which causes even more problems when I pipe this into other software. </p>
<p><strong>Example:</strong></p>
<pre><code>polygon([(0,0),(1,0),(1,1),(0,1)],aspect_ratio=1,axes=False,figsize=1,axes_pad=0)
</code></pre>
<p>yields:</p>
<p><img alt="example" src="https://dl.dropbox.com/u/4747602/sage2.png"/></p>
http://ask.sagemath.org/question/9546/mystery-white-border-around-graphics-object/?answer=14284#post-id-14284This seems to be a feature from matplotlib. You can try your best, turning off everything in matplotlib like this:
import matplotlib.pyplot as plt
plt.plot(range(10), range(10))
plt.axis('off')
plt.savefig('/tmp/a.png', bbox_inches='tight')
The outcome of the plot will be the same as the following code in Sage:
list_plot(range(10), plotjoined=True, axes=False)
So there is not much that can be done on Sage side. Eventually, the generation of the figure and the saving is handled by matplotlib.
**Update:**
There is a way to do it in matplotlib.
sage: import matplotlib.pyplot as plt
sage: plt.plot(range(10), range(10), '-o')
sage: plt.subplots_adjust(left=0, right=1, top=1, bottom=0)
sage: plt.axis('off')
sage: plt.savefig('/tmp/a.png', bbox_inches=0)
Mon, 19 Nov 2012 21:41:03 -0600http://ask.sagemath.org/question/9546/mystery-white-border-around-graphics-object/?answer=14284#post-id-14284Comment by ppurka for <p>This seems to be a feature from matplotlib. You can try your best, turning off everything in matplotlib like this:</p>
<pre><code>import matplotlib.pyplot as plt
plt.plot(range(10), range(10))
plt.axis('off')
plt.savefig('/tmp/a.png', bbox_inches='tight')
</code></pre>
<p>The outcome of the plot will be the same as the following code in Sage:</p>
<pre><code>list_plot(range(10), plotjoined=True, axes=False)
</code></pre>
<p>So there is not much that can be done on Sage side. Eventually, the generation of the figure and the saving is handled by matplotlib.</p>
<p><strong>Update:</strong></p>
<p>There is a way to do it in matplotlib.</p>
<pre><code>sage: import matplotlib.pyplot as plt
sage: plt.plot(range(10), range(10), '-o')
sage: plt.subplots_adjust(left=0, right=1, top=1, bottom=0)
sage: plt.axis('off')
sage: plt.savefig('/tmp/a.png', bbox_inches=0)
</code></pre>
http://ask.sagemath.org/question/9546/mystery-white-border-around-graphics-object/?comment=18671#post-id-18671Unfortunately, there is no straightforward way of doing it in Sage. What you can try to do is get the matplotlib object out of Sage, and then use matplotlib commands.
p = list_plot(range(10), plotjoined=True) # this can be any sage 2D graphics command
pm = p.matplotlib(axes=False, axes_pad=0) # this is the matplotlib object
pm.subplots_adjust(left=0, right=1, top=1, bottom=0)
from matplotlib.backends.backend_agg import FigureCanvasAgg
pm.set_canvas(FigureCanvasAgg(pm))
pm.savefig('/tmp/a.png', bbox_inches=0)
Thu, 22 Nov 2012 04:21:36 -0600http://ask.sagemath.org/question/9546/mystery-white-border-around-graphics-object/?comment=18671#post-id-18671Comment by marco for <p>This seems to be a feature from matplotlib. You can try your best, turning off everything in matplotlib like this:</p>
<pre><code>import matplotlib.pyplot as plt
plt.plot(range(10), range(10))
plt.axis('off')
plt.savefig('/tmp/a.png', bbox_inches='tight')
</code></pre>
<p>The outcome of the plot will be the same as the following code in Sage:</p>
<pre><code>list_plot(range(10), plotjoined=True, axes=False)
</code></pre>
<p>So there is not much that can be done on Sage side. Eventually, the generation of the figure and the saving is handled by matplotlib.</p>
<p><strong>Update:</strong></p>
<p>There is a way to do it in matplotlib.</p>
<pre><code>sage: import matplotlib.pyplot as plt
sage: plt.plot(range(10), range(10), '-o')
sage: plt.subplots_adjust(left=0, right=1, top=1, bottom=0)
sage: plt.axis('off')
sage: plt.savefig('/tmp/a.png', bbox_inches=0)
</code></pre>
http://ask.sagemath.org/question/9546/mystery-white-border-around-graphics-object/?comment=18669#post-id-18669This finally works:
p = polygon([(0,0),(1,0),(1,1),(0,1)])# this can be any sage 2D graphics command
pm = p.matplotlib(axes=False, axes_pad=0) # this is the matplotlib object
pm.subplots_adjust(left=0,right=1,top=1,bottom=0)
from matplotlib.backends.backend_agg import FigureCanvasAgg
pm.set_canvas(FigureCanvasAgg(pm))
pm.set_size_inches(5,5)
ax = plt.Axes(pm, [0.,0.,5.,5.])
ax.set_axis_off()
pm.add_axes(ax)
pm.savefig('/tmp/a.png', bbox_inches=0)Thu, 22 Nov 2012 06:18:34 -0600http://ask.sagemath.org/question/9546/mystery-white-border-around-graphics-object/?comment=18669#post-id-18669Comment by ppurka for <p>This seems to be a feature from matplotlib. You can try your best, turning off everything in matplotlib like this:</p>
<pre><code>import matplotlib.pyplot as plt
plt.plot(range(10), range(10))
plt.axis('off')
plt.savefig('/tmp/a.png', bbox_inches='tight')
</code></pre>
<p>The outcome of the plot will be the same as the following code in Sage:</p>
<pre><code>list_plot(range(10), plotjoined=True, axes=False)
</code></pre>
<p>So there is not much that can be done on Sage side. Eventually, the generation of the figure and the saving is handled by matplotlib.</p>
<p><strong>Update:</strong></p>
<p>There is a way to do it in matplotlib.</p>
<pre><code>sage: import matplotlib.pyplot as plt
sage: plt.plot(range(10), range(10), '-o')
sage: plt.subplots_adjust(left=0, right=1, top=1, bottom=0)
sage: plt.axis('off')
sage: plt.savefig('/tmp/a.png', bbox_inches=0)
</code></pre>
http://ask.sagemath.org/question/9546/mystery-white-border-around-graphics-object/?comment=18660#post-id-18660@kcrisman I hope so, but we can not guess what the image dimensions will be. At this point, it simply depends on the kind of plot we are doing. If we can find a common way to do this for *all* plots, then this can be easily enabled through some switch like `no_border=True/False`, which will take care of all matplotlib tweaks needed.Fri, 23 Nov 2012 20:56:02 -0600http://ask.sagemath.org/question/9546/mystery-white-border-around-graphics-object/?comment=18660#post-id-18660Comment by marco for <p>This seems to be a feature from matplotlib. You can try your best, turning off everything in matplotlib like this:</p>
<pre><code>import matplotlib.pyplot as plt
plt.plot(range(10), range(10))
plt.axis('off')
plt.savefig('/tmp/a.png', bbox_inches='tight')
</code></pre>
<p>The outcome of the plot will be the same as the following code in Sage:</p>
<pre><code>list_plot(range(10), plotjoined=True, axes=False)
</code></pre>
<p>So there is not much that can be done on Sage side. Eventually, the generation of the figure and the saving is handled by matplotlib.</p>
<p><strong>Update:</strong></p>
<p>There is a way to do it in matplotlib.</p>
<pre><code>sage: import matplotlib.pyplot as plt
sage: plt.plot(range(10), range(10), '-o')
sage: plt.subplots_adjust(left=0, right=1, top=1, bottom=0)
sage: plt.axis('off')
sage: plt.savefig('/tmp/a.png', bbox_inches=0)
</code></pre>
http://ask.sagemath.org/question/9546/mystery-white-border-around-graphics-object/?comment=18672#post-id-18672Thanks, this is exactly what I am looking for. How would you recommend I use this on a sage graphics object, like a complex_plot or something like that? Thu, 22 Nov 2012 03:58:54 -0600http://ask.sagemath.org/question/9546/mystery-white-border-around-graphics-object/?comment=18672#post-id-18672Comment by kcrisman for <p>This seems to be a feature from matplotlib. You can try your best, turning off everything in matplotlib like this:</p>
<pre><code>import matplotlib.pyplot as plt
plt.plot(range(10), range(10))
plt.axis('off')
plt.savefig('/tmp/a.png', bbox_inches='tight')
</code></pre>
<p>The outcome of the plot will be the same as the following code in Sage:</p>
<pre><code>list_plot(range(10), plotjoined=True, axes=False)
</code></pre>
<p>So there is not much that can be done on Sage side. Eventually, the generation of the figure and the saving is handled by matplotlib.</p>
<p><strong>Update:</strong></p>
<p>There is a way to do it in matplotlib.</p>
<pre><code>sage: import matplotlib.pyplot as plt
sage: plt.plot(range(10), range(10), '-o')
sage: plt.subplots_adjust(left=0, right=1, top=1, bottom=0)
sage: plt.axis('off')
sage: plt.savefig('/tmp/a.png', bbox_inches=0)
</code></pre>
http://ask.sagemath.org/question/9546/mystery-white-border-around-graphics-object/?comment=18663#post-id-18663@ppurka - maybe is there a way to enable this as a very kludgy thing in Sage?Fri, 23 Nov 2012 03:39:32 -0600http://ask.sagemath.org/question/9546/mystery-white-border-around-graphics-object/?comment=18663#post-id-18663Comment by ppurka for <p>This seems to be a feature from matplotlib. You can try your best, turning off everything in matplotlib like this:</p>
<pre><code>import matplotlib.pyplot as plt
plt.plot(range(10), range(10))
plt.axis('off')
plt.savefig('/tmp/a.png', bbox_inches='tight')
</code></pre>
<p>The outcome of the plot will be the same as the following code in Sage:</p>
<pre><code>list_plot(range(10), plotjoined=True, axes=False)
</code></pre>
<p>So there is not much that can be done on Sage side. Eventually, the generation of the figure and the saving is handled by matplotlib.</p>
<p><strong>Update:</strong></p>
<p>There is a way to do it in matplotlib.</p>
<pre><code>sage: import matplotlib.pyplot as plt
sage: plt.plot(range(10), range(10), '-o')
sage: plt.subplots_adjust(left=0, right=1, top=1, bottom=0)
sage: plt.axis('off')
sage: plt.savefig('/tmp/a.png', bbox_inches=0)
</code></pre>
http://ask.sagemath.org/question/9546/mystery-white-border-around-graphics-object/?comment=18667#post-id-18667Great! It looks like there is no "one way" of doing this. Depends on the type of figure we are plotting. :-/Thu, 22 Nov 2012 19:19:42 -0600http://ask.sagemath.org/question/9546/mystery-white-border-around-graphics-object/?comment=18667#post-id-18667Comment by ppurka for <p>This seems to be a feature from matplotlib. You can try your best, turning off everything in matplotlib like this:</p>
<pre><code>import matplotlib.pyplot as plt
plt.plot(range(10), range(10))
plt.axis('off')
plt.savefig('/tmp/a.png', bbox_inches='tight')
</code></pre>
<p>The outcome of the plot will be the same as the following code in Sage:</p>
<pre><code>list_plot(range(10), plotjoined=True, axes=False)
</code></pre>
<p>So there is not much that can be done on Sage side. Eventually, the generation of the figure and the saving is handled by matplotlib.</p>
<p><strong>Update:</strong></p>
<p>There is a way to do it in matplotlib.</p>
<pre><code>sage: import matplotlib.pyplot as plt
sage: plt.plot(range(10), range(10), '-o')
sage: plt.subplots_adjust(left=0, right=1, top=1, bottom=0)
sage: plt.axis('off')
sage: plt.savefig('/tmp/a.png', bbox_inches=0)
</code></pre>
http://ask.sagemath.org/question/9546/mystery-white-border-around-graphics-object/?comment=18654#post-id-18654I have opened [#13751](http://trac.sagemath.org/sage_trac/ticket/13751) to track this. Feel free to post any ideas you get.Sat, 24 Nov 2012 18:28:55 -0600http://ask.sagemath.org/question/9546/mystery-white-border-around-graphics-object/?comment=18654#post-id-18654Comment by marco for <p>This seems to be a feature from matplotlib. You can try your best, turning off everything in matplotlib like this:</p>
<pre><code>import matplotlib.pyplot as plt
plt.plot(range(10), range(10))
plt.axis('off')
plt.savefig('/tmp/a.png', bbox_inches='tight')
</code></pre>
<p>The outcome of the plot will be the same as the following code in Sage:</p>
<pre><code>list_plot(range(10), plotjoined=True, axes=False)
</code></pre>
<p>So there is not much that can be done on Sage side. Eventually, the generation of the figure and the saving is handled by matplotlib.</p>
<p><strong>Update:</strong></p>
<p>There is a way to do it in matplotlib.</p>
<pre><code>sage: import matplotlib.pyplot as plt
sage: plt.plot(range(10), range(10), '-o')
sage: plt.subplots_adjust(left=0, right=1, top=1, bottom=0)
sage: plt.axis('off')
sage: plt.savefig('/tmp/a.png', bbox_inches=0)
</code></pre>
http://ask.sagemath.org/question/9546/mystery-white-border-around-graphics-object/?comment=18659#post-id-18659I think the plot must contain the information of its dimensions, so perhaps we can programmatically do thisSat, 24 Nov 2012 04:55:18 -0600http://ask.sagemath.org/question/9546/mystery-white-border-around-graphics-object/?comment=18659#post-id-18659Comment by marco for <p>This seems to be a feature from matplotlib. You can try your best, turning off everything in matplotlib like this:</p>
<pre><code>import matplotlib.pyplot as plt
plt.plot(range(10), range(10))
plt.axis('off')
plt.savefig('/tmp/a.png', bbox_inches='tight')
</code></pre>
<p>The outcome of the plot will be the same as the following code in Sage:</p>
<pre><code>list_plot(range(10), plotjoined=True, axes=False)
</code></pre>
<p>So there is not much that can be done on Sage side. Eventually, the generation of the figure and the saving is handled by matplotlib.</p>
<p><strong>Update:</strong></p>
<p>There is a way to do it in matplotlib.</p>
<pre><code>sage: import matplotlib.pyplot as plt
sage: plt.plot(range(10), range(10), '-o')
sage: plt.subplots_adjust(left=0, right=1, top=1, bottom=0)
sage: plt.axis('off')
sage: plt.savefig('/tmp/a.png', bbox_inches=0)
</code></pre>
http://ask.sagemath.org/question/9546/mystery-white-border-around-graphics-object/?comment=18670#post-id-18670If you try that with p = polygon([(0,0),(1,0),(1,1),(0,1)]) as a 2d graphics command, the top and bottom border are indeed removed, but the sides are not. I'm looking into the FigureCanvasAgg thingy to see if that is the causeThu, 22 Nov 2012 04:30:47 -0600http://ask.sagemath.org/question/9546/mystery-white-border-around-graphics-object/?comment=18670#post-id-18670