Ask Your Question

Mystery white border around Graphics object

asked 2012-11-19 02:08:08 +0200

marco gravatar image

updated 2012-11-19 02:21:48 +0200

If you try to show() a 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.





edit retag flag offensive close merge delete


Why do you assume this is a bug? Why not just crop the image to your liking before piping it to other (presumably delicate) software?

benjaminfjones gravatar imagebenjaminfjones ( 2012-11-20 00:17:14 +0200 )edit

I 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.

marco gravatar imagemarco ( 2012-11-20 23:06:13 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2012-11-20 04:41:03 +0200

ppurka gravatar image

updated 2012-11-22 04:03:51 +0200

This 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.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.


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


Thanks, 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?

marco gravatar imagemarco ( 2012-11-22 10:58:54 +0200 )edit

Unfortunately, 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)

ppurka gravatar imageppurka ( 2012-11-22 11:21:36 +0200 )edit

If 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 cause

marco gravatar imagemarco ( 2012-11-22 11:30:47 +0200 )edit

This 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)

marco gravatar imagemarco ( 2012-11-22 13:18:34 +0200 )edit

Great! It looks like there is no "one way" of doing this. Depends on the type of figure we are plotting. :-/

ppurka gravatar imageppurka ( 2012-11-23 02:19:42 +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


Asked: 2012-11-19 02:08:08 +0200

Seen: 2,162 times

Last updated: Nov 22 '12