Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

a matrix plot is wrong when matrix entries are not representable as floats. #1481

Open
jasongrout opened this issue Dec 12, 2007 · 13 comments
Open

Comments

@jasongrout
Copy link
Member

sage: m=matrix(ZZ,1,[16]); m
[16]
sage: matrix_plot(m^10).show()
---------------------------------------------------------------------------
<type 'exceptions.OverflowError'>         Traceback (most recent call last)

/home/grout/.sage/<ipython console> in <module>()

/home/grout/sage/local/lib/python2.5/site-packages/sage/plot/plot.py in show(self, xmin, xmax, ymin, ymax, figsize, filename, dpi, axes, axes_label, frame, fontsize, **args)
    654         if filename is None:
    655             filename = sage.misc.misc.tmp_filename() + '.png'
--> 656         self.save(filename, xmin, xmax, ymin, ymax, figsize, dpi=dpi, axes=axes,frame=frame, fontsize=fontsize)
    657         os.system('%s %s 2>/dev/null 1>/dev/null &'%(sage.misc.viewer.browser(), filename))
    658

/home/grout/sage/local/lib/python2.5/site-packages/sage/plot/plot.py in save(self, filename, xmin, xmax, ymin, ymax, figsize, figure, sub, savenow, dpi, axes, axes_label, fontsize, frame, verify)
    766             if isinstance(g, GraphicPrimitive_MatrixPlot):
    767                 matrixplot = True
--> 768             g._render_on_subplot(subplot)
    769
    770         #adjust the xy limits and draw the axes:

/home/grout/sage/local/lib/python2.5/site-packages/sage/plot/plot.py in _render_on_subplot(self, subplot)
   1102             print "The possible color maps include: %s"%possibilities
   1103             raise RuntimeError, "Color map %s not known"%cmap
-> 1104         subplot.imshow(self.xy_data_array, cmap=cmap, interpolation='nearest')
   1105
   1106 # Below is the base class that is used to make 'field plots'.

/home/grout/sage/local/lib/python2.5/site-packages/matplotlib/axes.py in imshow(self, X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, **kwargs)
   4053                        filterrad=filterrad, **kwargs)
   4054
-> 4055         im.set_data(X)
   4056         im.set_alpha(alpha)
   4057         self._set_artist_props(im)

/home/grout/sage/local/lib/python2.5/site-packages/matplotlib/image.py in set_data(self, A, shape)
    224             or X.shape[2] > 4
    225             or X.shape[2] < 3):
--> 226             cm.ScalarMappable.set_array(self, X)
    227         else:
    228             self._A = X

/home/grout/sage/local/lib/python2.5/site-packages/matplotlib/cm.py in set_array(self, A)
     65             self._A = A.astype(nx.Float32)
     66         else:
---> 67             self._A = A.astype(nx.Int16)
     68
     69     def get_array(self):

/home/grout/sage/local/lib/python2.5/site-packages/numpy/core/ma.py in astype(self, tc)
   1148     def astype (self, tc):
   1149         "return self as array of given type."
-> 1150         d = self._data.astype(tc)
   1151         return array(d, mask=self._mask)
   1152

<type 'exceptions.OverflowError'>: long int too large to convert to int

CC: @kcrisman

Component: graphics

Author: William Stein

Reviewer: Karl-Dieter Crisman

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

@jasongrout
Copy link
Member Author

comment:2

This seems to work in 2.9.3:

sage: m=matrix(ZZ,1,[16]); m
[16]
sage: matrix_plot(m^100).show()
sage: 

I do get this warning:

sage: matrix_plot(m^1000).show()
Warning: invalid value encountered in multiply

but the image shows up (a giant black square, just like it should).

@sagetrac-mabshoff
Copy link
Mannequin

sagetrac-mabshoff mannequin commented Jan 19, 2008

comment:3

Ok, but we should still add a doctest to catch this behavior. It can be #long, but I will reopen this for now until the doctest is added [which is standard requirement to close bugs these days :)]

Cheers,

Michael

@sagetrac-mabshoff sagetrac-mabshoff mannequin changed the title showing a matrix plot blows up [resolved, but needs doctest] showing a matrix plot blows up Jan 19, 2008
@sagetrac-mabshoff sagetrac-mabshoff mannequin reopened this Jan 19, 2008
@jasongrout
Copy link
Member Author

comment:4

Thanks for reopening this. The following code displays the wrong plot:

a=matrix(2,[16^1000,0,0,-16^1000]);
matrix_plot(a)

What should be displayed is the same as the plot:

a=matrix(2,[16,0,0,-16]);
matrix_plot(a)

So the matrix plot has gone from blowing up to just being wrong. Whether this is worse or better is left as an exercise for the reader.

@jasongrout jasongrout changed the title [resolved, but needs doctest] showing a matrix plot blows up a matrix plot is wrong when matrix entries are not representable as floats. Jan 28, 2008
@jasongrout
Copy link
Member Author

comment:5

I think this problem is a matplotlib problem and has to do with not dealing with inf or -inf in the matrix. We could send vmin and vmax parameters to the imshow command to scale the matrix manually if we see an infinity in the matrix, or we could raise an exception, or we could report the bug back up to matplotlib (if it is indeed a matplotlib issue).

@williamstein
Copy link
Contributor

this should completely deal with the dense case.

@jasongrout
Copy link
Member Author

Author: William Stein

@jasongrout
Copy link
Member Author

comment:6

Attachment: trac_1481-part1.patch.gz

@jasongrout
Copy link
Member Author

comment:9

Whoever referees this patch should make sure it works well with the norm, vmin, and vmax parameters in matrix_plot

@jasongrout
Copy link
Member Author

comment:10

On the surface, it doesn't appear that the patch works with norm, vmin, and vmax, probably because the patch was written before we added those parameters.

@kcrisman
Copy link
Member

Reviewer: Karl-Dieter Crisman

@kcrisman
Copy link
Member

comment:11

Does not apply to 4.7.alpha1 in any case. Needs work - though, impressively, only one hunk failed. Not bad for a patch over a year old.

@sagetrac-dsm
Copy link
Mannequin

sagetrac-dsm mannequin commented May 25, 2012

comment:12

The original patch doesn't seem to work at the moment (the numpy array created has dtype=object, so the integers stay as integers instead of becoming floats, so there are no infs to work around). I have a variant which works, and modified it to handle vmin and vmax, but I'm not sure what to do with norm: should the norm be applied before or after the vmin/vmax?

@jdemeyer jdemeyer modified the milestones: sage-5.11, sage-5.12 Aug 13, 2013
@sagetrac-vbraun-spam sagetrac-vbraun-spam mannequin modified the milestones: sage-6.1, sage-6.2 Jan 30, 2014
@sagetrac-vbraun-spam sagetrac-vbraun-spam mannequin modified the milestones: sage-6.2, sage-6.3 May 6, 2014
@sagetrac-vbraun-spam sagetrac-vbraun-spam mannequin modified the milestones: sage-6.3, sage-6.4 Aug 10, 2014
@mkoeppe mkoeppe removed this from the sage-6.4 milestone Dec 29, 2022
@AishwaryaSatpute1
Copy link

can I Work on this issue ???

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants