9

I am trying to plot a matrix with 2000 columns and 200000 rows. I can test plot and test export the matrix figure fine when the matrix is small using

matshow(my_matrix)
show()

However, when more rows are added to my_matrix, the figure becomes very narrow as there are way more rows than columns, thus losing the precision when zooming in. Can I make matrix figure scrollable? If not, how can I visualize such matrix without losing precision?

I also tried to call savefig('filename', dpi=300) in order to save the image without losing too much precision, but it throws MemoryError when the matrix is big. Many thanks!

5
  • 2
    Maybe too simple for your needs, but have you tried splitting up your large matrix into parts, and then calling matshow on each of those?
    – lesnikow
    Commented Jul 3, 2015 at 23:31
  • 7
    ax.set_aspect('auto') will fix the aspect problem, but your pixels will be rectangles not squares. I have done things with 2k x 150k, but it is starting to push the limits of what mpl can do (there are some hard-coded 32 bit integers in the c++ rasterization code).
    – tacaswell
    Commented Jul 3, 2015 at 23:36
  • @tcaswell matshow() returns a AxesImage. I believe the set_aspect() function is only accessible to Axes object. I don't know how to get the Axes object from AxesImage.
    – emily
    Commented Jul 6, 2015 at 17:34
  • I ended up taking a combination of your suggestions. Get current axes by calling ax = plt.gca() then ax.set_aspect('auto'), I also split the matrix into smaller matrices.
    – emily
    Commented Jul 6, 2015 at 19:59
  • Hi @emily - consider adding an answer to your own question so that people in the future with the same problem can find it easily. Commented Jul 9, 2015 at 7:14

1 Answer 1

1

I ended up taking a combination of @tcaswell and @lesnikow's suggestions.

Get current axes in order to set auto aspect ratio properly, I also split the matrix into smaller matrices:

    import matplotlib.pylab as plt

    for j in range(lower_bound_on_rows, upper_bound_on_rows): nums.append(j)
    partial_matrix = my_matrix[nums, :] 

    plt.matshow(partial_matrix, fignum=100)
    plt.gca().set_aspect('auto')
    plt.savefig('filename.png', dpi=600)

My matrix is long vertically, so I sliced by rows and preserved all columns in the smaller matrices. If your matrix is long horizontally, flip the index like this my_matrix[:, nums]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.