Ask Your Question
0

matplotlib don't work??

asked 2011-05-15 09:38:31 +0100

Sagud gravatar image

updated 2015-01-24 13:59:02 +0100

FrédéricC gravatar image
import matplotlib.pyplot as plt
A = matrix([[1,2,3],[4,5,6],[7,8,9]])
plt.matshow(A)

What am I missing?

edit retag flag offensive close merge delete

Comments

what happened to the original question?!

Kelvin Li gravatar imageKelvin Li ( 2011-05-22 14:31:25 +0100 )edit

I've restored it (more or less) from the revision history. @Sagud, if you have a new question (about animate?) please ask a new question rather than changing an old one.

DSM gravatar imageDSM ( 2011-05-22 21:36:11 +0100 )edit

1 Answer

Sort by » oldest newest most voted
4

answered 2011-05-15 10:26:16 +0100

DSM gravatar image

Sage matrices aren't the same as numpy matrices.

sage: A = matrix([[1,2,3],[4,5,6],[7,8,9]])
sage: type(A)
<type 'sage.matrix.matrix_integer_dense.Matrix_integer_dense'>
sage: parent(A)
Full MatrixSpace of 3 by 3 dense matrices over Integer Ring
sage: import numpy
sage: B = numpy.matrix([[1,2,3],[4,5,6],[7,8,9]])
sage: type(B)
<class 'numpy.matrixlib.defmatrix.matrix'>

In this case, though, it's easy enough to convert the Sage matrix into something that numpy will like (an array, not a matrix, but that shouldn't matter here):

sage: A.numpy()
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
sage: type(A.numpy())
<type 'numpy.ndarray'>
sage: plt.matshow(A.numpy())
<matplotlib.image.AxesImage object at 0x10dad0090>
edit flag offensive delete link more

Comments

I can't figure out what this is supposed to look like. Is it any different from matrix_plot(A)?

kcrisman gravatar imagekcrisman ( 2011-05-16 11:23:19 +0100 )edit

@kcrisman: by default, unlike matrix_plot, you really only get the squares. No axes or ticks. [Not sure if there are things you can tweak or not.]

DSM gravatar imageDSM ( 2011-05-17 13:06:49 +0100 )edit

You can tweak everything in matplotlib. That is how we generate all these graphics - we use spy and imshow for dense and sparse matrices, it looks like.

kcrisman gravatar imagekcrisman ( 2011-05-17 17:00:17 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2011-05-15 09:38:31 +0100

Seen: 611 times

Last updated: May 22 '11