简体   繁体   English

Python plot使用matplotlib的大矩阵

[英]Python plot Large matrix using matplotlib

I am trying to plot a matrix with 2000 columns and 200000 rows. 我试图绘制一个2000列和200000行的矩阵。 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? 但是,当更多行添加到my_matrix时,图形变得非常窄,因为行数多于列,因此在放大时会失去精度。我可以使矩阵图形可滚动吗? 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. 我还尝试调用savefig('filename',dpi = 300)以保存图像而不会损失太多精度,但是当矩阵很大时它会抛出MemoryError。 Many thanks! 非常感谢!

I ended up taking a combination of @tcaswell and @lesnikow's suggestions. 我最终结合了@tcaswell和@ lesnikow的建议。

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] 如果您的矩阵水平长,请像这样翻转索引my_matrix [:,nums]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM