Ask Your Question
2

Mystery white border around Graphics object

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

marco gravatar image

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

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

edit retag flag offensive close merge delete

Comments

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 +0100 )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 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
1

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

ppurka gravatar image

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

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

Comments

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 +0100 )edit
1

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 +0100 )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 +0100 )edit
1

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 +0100 )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 +0100 )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

Stats

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

Seen: 2,367 times

Last updated: Nov 22 '12