#5448: [with patch, needs work] rework save/show in plot, use Matplotlib's axes
code, upgrade matplotlib
-------------------------+--------------------------------------------------
Reporter: mhansen | Owner: mhansen
Type: enhancement | Status: assigned
Priority: major | Milestone: sage-4.1.2
Component: graphics | Keywords:
Reviewer: | Author: Jason Grout
Merged: |
-------------------------+--------------------------------------------------
Comment(by jason):
I am deleting the following code from the patch because it is not used
right now. If in the future we want to have more control over the size of
the figure, but still want to automatically shrink the plot to fit
everything (labels, etc.) inside of the figure without clipping them, then
the following code will prove useful. I'm pasting the code here so that
it is some sort of permanent record.
This is an extension of an idea in the matplotlib FAQ.
{{{
# Put the following somewhere in your routine that saves the file, after
you've created the FigureCanvas object, but before you've actually saved
the canvas.
#canvas.mpl_connect('draw_event', on_draw)
def on_draw(event):
"""
TODO: write documentation
"""
import matplotlib.transforms as mtransforms
figure=event.canvas.figure
bboxes = []
for ax in figure.axes:
bbox = ax.xaxis.get_label().get_window_extent()
# the figure transform goes from relative coords->pixels and we
# want the inverse of that
bboxi = bbox.inverse_transformed(figure.transFigure)
bboxes.append(bboxi)
bbox = ax.yaxis.get_label().get_window_extent()
bboxi = bbox.inverse_transformed(figure.transFigure)
bboxes.append(bboxi)
for label in (ax.get_xticklabels()+ax.get_yticklabels() \
+ ax.get_xticklabels(minor=True) \
+ax.get_yticklabels(minor=True)):
bbox = label.get_window_extent()
bboxi = bbox.inverse_transformed(figure.transFigure)
bboxes.append(bboxi)
# this is the bbox that bounds all the bboxes, again in relative
# figure coords
bbox = mtransforms.Bbox.union(bboxes)
adjusted=adjust_figure_to_contain_bbox(figure,bbox)
if adjusted:
figure.canvas.draw()
return False
def adjust_figure_to_contain_bbox(fig, bbox):
"""
TODO: write documentation
"""
adjusted=False
if bbox.xmin<0:
fig.subplots_adjust(left=fig.subplotpars.left-bbox.xmin)
adjusted=True
if bbox.ymin<0:
fig.subplots_adjust(bottom=fig.subplotpars.bottom-bbox.ymin)
adjusted=True
if bbox.xmax>1:
fig.subplots_adjust(right=fig.subplotpars.right-(bbox.xmax-1))
adjusted=True
if bbox.ymax>1:
fig.subplots_adjust(top=fig.subplotpars.top-(bbox.ymax-1))
adjusted=True
return adjusted
}}}
--
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/5448#comment:45>
Sage <http://sagemath.org/>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica,
and MATLAB
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sage-trac" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sage-trac?hl=en
-~----------~----~----~----~------~----~------~--~---