Opened 14 years ago

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

Reported by: Owned by: jason was major sage-6.4 graphics matplotlib, Graphics niles N/A

### 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):

def matplotlib_plot(mat):
from sage.plot.plot import Graphics
g = Graphics()
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.

### comment:1 Changed 14 years ago by jason

And the documentation should be updated, of course.

### comment:2 Changed 14 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?

### comment:3 Changed 13 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: ↓ 5 Changed 13 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 13 years ago by jason

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.

### comment:6 Changed 13 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 follow-up: ↓ 16 Changed 13 years ago by jason

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

### comment:8 Changed 12 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.)

cc me!

### comment:10 Changed 12 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 12 years ago by jason

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

### comment:12 Changed 9 years ago by jdemeyer

• Milestone changed from sage-5.11 to sage-5.12

### comment:13 Changed 9 years ago by vbraun_spam

• Milestone changed from sage-6.1 to sage-6.2

### comment:14 Changed 8 years ago by vbraun_spam

• Milestone changed from sage-6.2 to sage-6.3

### comment:15 Changed 8 years ago by vbraun_spam

• Milestone changed from sage-6.3 to sage-6.4