python numpy 生成矩阵,Python:从Numpy矩阵创建2D直方图

I'm new to python.

I have a numpy matrix, of dimensions 42x42, with values in the range 0-996. I want to create a 2D histogram using this data. I've been looking at tutorials, but they all seem to show how to create 2D histograms from random data and not a numpy matrix.

So far, I have imported:

import numpy as np

import matplotlib.pyplot as plt

from matplotlib import colors

I'm not sure if these are correct imports, I'm just trying to pick up what I can from tutorials I see.

I have the numpy matrix M with all of the values in it (as described above). In the end, i want it to look something like this:

bb5503d5ceecacfc5c6384147776d78f.png

obviously, my data will be different, so my plot should look different. Can anyone give me a hand?

Edit: For my purposes, Hooked's example below, using matshow, is exactly what I'm looking for.

解决方案

If you have the raw data from the counts, you could use plt.hexbin to create the plots for you (IMHO this is better than a square lattice): Adapted from the example of hexbin:

import numpy as np

import matplotlib.pyplot as plt

n = 100000

x = np.random.standard_normal(n)

y = 2.0 + 3.0 * x + 4.0 * np.random.standard_normal(n)

plt.hexbin(x,y)

plt.show()

hERT1.png

If you already have the Z-values in a matrix as you mention, just use plt.imshow or plt.matshow:

XB = np.linspace(-1,1,20)

YB = np.linspace(-1,1,20)

X,Y = np.meshgrid(XB,YB)

Z = np.exp(-(X**2+Y**2))

plt.imshow(Z,interpolation='none')

101f2d991861063ea69f5abf39cad847.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值