Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python plot Large matrix using matplotlib

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!

like image 766
emily Avatar asked Jul 03 '15 23:07

emily


1 Answers

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]

like image 149
emily Avatar answered Oct 15 '22 09:10

emily