Opened 13 years ago
Last modified 6 months 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: |
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)
Change History (19)
comment:1 Changed 13 years ago by
comment:2 Changed 13 years ago by
- 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 13 years ago by
comment:3 Changed 13 years ago by
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 12 years ago by
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
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.
comment:6 Changed 12 years ago by
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 12 years ago by
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 11 years ago by
- 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:10 Changed 11 years ago by
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 11 years ago by
As far as I know, we're stalled waiting for something to happen to matplotlib first.
comment:12 Changed 8 years ago by
- Milestone changed from sage-5.11 to sage-5.12
comment:13 Changed 8 years ago by
- Milestone changed from sage-6.1 to sage-6.2
comment:14 Changed 8 years ago by
- Milestone changed from sage-6.2 to sage-6.3
comment:15 Changed 7 years ago by
- Milestone changed from sage-6.3 to sage-6.4
comment:16 in reply to: ↑ 7 Changed 6 months ago by
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.
And the documentation should be updated, of course.