sagemath / sage

Main repository of SageMath. Now open for Issues and Pull Requests.
https://www.sagemath.org
Other
1.18k stars 409 forks source link

matplotlib Graphics() wrapper #5128

Open jasongrout opened 15 years ago

jasongrout commented 15 years ago

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.

CC: @nilesjohnson

Component: graphics

Keywords: matplotlib

Issue created by migration from https://trac.sagemath.org/ticket/5128

jasongrout commented 15 years ago
comment:1

And the documentation should be updated, of course.

jasongrout commented 15 years ago
comment:2

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?

jasongrout commented 15 years ago
comment:3

Attachment: trac_5128-matplotlib-plot.patch.gz

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

kcrisman commented 14 years ago
comment:4

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?

jasongrout commented 14 years ago
comment:5

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.

jasongrout commented 14 years ago

Attachment: trac_5128-matplotlib-plot.2.patch.gz

apply instead of previous patch.

jasongrout commented 14 years ago

apply instead of previous patches

jasongrout commented 14 years ago
comment:6

Attachment: trac_5128-matplotlib-plot.3.patch.gz

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

jasongrout commented 14 years ago
comment:7

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.

dandrake commented 13 years ago
comment:8

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

9343d2e0-59ba-4406-bd4f-c78e4cf1230e commented 13 years ago

Changed keywords from none to matplotlib

9343d2e0-59ba-4406-bd4f-c78e4cf1230e commented 13 years ago
comment:9

cc me!

9343d2e0-59ba-4406-bd4f-c78e4cf1230e commented 13 years ago
comment:10

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?

jasongrout commented 13 years ago
comment:11

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

kcrisman commented 2 years ago
comment:16

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

This server is not only down, the replacement news.gmane.io is not exactly obvious how to access this one any more. I think that this is the discussion in question: https://discourse.matplotlib.org/t/saving-images-using-pure-matplotlib-in-sage-cuts-off-the-bottom-part-and-produces-corrupt-file/12286/7 or possibly https://discourse.matplotlib.org/t/saving-an-axes-to-draw-in-a-different-figure/12185

See also https://groups.google.com/g/sage-support/c/N2cgEW3QUG4 for a SageTeX point of view.

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

I wonder what the current state of the art is.