Opened 12 years ago

Last modified 7 years ago

#5128 needs_work enhancement

[with patch, needs work] matplotlib Graphics() wrapper

Reported by: jason Owned by: was
Priority: major Milestone: sage-6.4
Component: graphics Keywords: matplotlib, Graphics
Cc: niles Merged in:
Authors: Reviewers:
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Status badges

Description

This provides an easy way to make a matplotlib image and combine it with other Graphics() objects:

class Matplotlib_Primitive(GraphicPrimitive):
    """
    Primitive class that initializes the
    matrix_plot graphics type 
    """
    def __init__(self, artist, options=None):
        self.artist = artist
        GraphicPrimitive.__init__(self, options)        

    def get_minmax_data(self):
        """
        Returns a dictionary with the bounding box data.
                
        EXAMPLES:
            sage: m = matrix_plot(matrix([[1,3,5,1],[2,4,5,6],[1,3,5,7]]))[0]
            sage: list(sorted(m.get_minmax_data().items()))
            [('xmax', 4), ('xmin', 0), ('ymax', 3), ('ymin', 0)]

        """
        return dict(zip(['xmin', 'xmax', 'ymax', 'ymin'], self.artist.get_extent()))

    def _allowed_options(self):
        return {}

    def _repr_(self):
        return "MatrixPlot defined by a %s x %s data grid"%(self.xy_array_row, self.xy_array_col)

    def _render_on_subplot(self, subplot):
        subplot.add_artist(self.artist)

def matplotlib_plot(mat):
    from sage.plot.plot import Graphics
    g = Graphics()
    g.add_primitive(Matplotlib_Primitive(mat))
    return g

Example use:

A=random_matrix(RDF,100)
A.numpy()
import pylab
import numpy
B=A.numpy().astype(float)
im = pylab.imshow(B/numpy.max(B),  origin='upper',alpha=0.6)
matplotlib_plot(im)+polygon([[0,10],[10,0],[20,40]])

This just needs to be put in a file in the plot/ directory and an entry added to all.py.

Attachments (3)

trac_5128-matplotlib-plot.patch (4.5 KB) - added by jason 12 years ago.
trac_5128-matplotlib-plot.2.patch (3.8 KB) - added by jason 12 years ago.
apply instead of previous patch.
trac_5128-matplotlib-plot.3.patch (5.0 KB) - added by jason 12 years ago.
apply instead of previous patches

Download all attachments as: .zip

Change History (18)

comment:1 Changed 12 years ago by jason

And the documentation should be updated, of course.

comment:2 Changed 12 years ago by jason

  • Summary changed from matplotlib Graphics() wrapper to [with patch, needs work] matplotlib Graphics() wrapper

Something doesn't work in the above patch. I need to figure out how to get the extents of any matplotlib image passed in. Is there an easy way to that information?

Changed 12 years ago by jason

comment:3 Changed 12 years ago by jason

This makes things a little better, but not much. Currently, you can add an axes object. Note that the doctests involving hist are wrong.

comment:4 follow-up: Changed 12 years ago by kcrisman

What still needs to be done on this? It would be really good to get this functionality in now that #5448 is merged, so that other tickets could use it. Am I correct that currently there is no way to create a Sage graphics object from a mpl one - the process is one direction only?

comment:5 in reply to: ↑ 4 Changed 12 years ago by jason

Replying to kcrisman:

What still needs to be done on this? It would be really good to get this functionality in now that #5448 is merged, so that other tickets could use it. Am I correct that currently there is no way to create a Sage graphics object from a mpl one - the process is one direction only?

That's correct. This ticket is the other direction.

Changed 12 years ago by jason

apply instead of previous patch.

Changed 12 years ago by jason

apply instead of previous patches

comment:6 Changed 12 years ago by jason

I've attached another iteration. I've also posted to the matplotlib users mailing list about the problems in the above patch.

comment:7 Changed 12 years ago by jason

The matplotlib thread is here: http://thread.gmane.org/gmane.comp.python.matplotlib.general/19547

It sounds like we'll probably have to wait until someone (one of us??) volunteers to help on the matplotlib side.

comment:8 Changed 10 years ago by ddrake

  • Report Upstream set to N/A

On sage-support, Karl-Dieter asked me to comment about an issue this ticket might solve. That thread concerns using SageTeX to plot matplotlib graphics -- if this ticket does indeed allow one to easily convert mpl objects to Sage graphics objects, then yes, it will solve the problem in that thread.

(Note, though, that I think I described a much more general solution there -- albeit one that will require some setup code every time you want to use it.)

comment:9 Changed 10 years ago by niles

  • Cc niles added
  • Keywords matplotlib Graphics added

cc me!

comment:10 Changed 10 years ago by niles

This is related to #10656, which asks for functionality to convert a GraphicsArray() object to a Graphics() object.

By the way, is this ticket still waiting for something to happen with matplotlib, or is it in working order?

comment:11 Changed 10 years ago by jason

As far as I know, we're stalled waiting for something to happen to matplotlib first.

comment:12 Changed 8 years ago by jdemeyer

  • Milestone changed from sage-5.11 to sage-5.12

comment:13 Changed 7 years ago by vbraun_spam

  • Milestone changed from sage-6.1 to sage-6.2

comment:14 Changed 7 years ago by vbraun_spam

  • Milestone changed from sage-6.2 to sage-6.3

comment:15 Changed 7 years ago by vbraun_spam

  • Milestone changed from sage-6.3 to sage-6.4
Note: See TracTickets for help on using tickets.