First time here? Check out the FAQ!

Ask Your Question
2

Mystery white border around Graphics object

asked 12 years ago

marco gravatar image

updated 12 years ago

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

Preview: (hide)

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 ( 12 years ago )

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 ( 12 years ago )

1 Answer

Sort by » oldest newest most voted
1

answered 12 years ago

ppurka gravatar image

updated 12 years ago

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)
Preview: (hide)
link

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 ( 12 years ago )
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 ( 12 years ago )

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 ( 12 years ago )
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 ( 12 years ago )

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

ppurka gravatar imageppurka ( 12 years ago )

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: 12 years ago

Seen: 2,435 times

Last updated: Nov 22 '12