Python:从Numpy矩阵创建2D直方图 [英] Python: Creating a 2D histogram from a numpy matrix

查看:374
本文介绍了Python:从Numpy矩阵创建2D直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手.

I'm new to python.

我有一个numpy矩阵,尺寸为42x42,其值在0-996范围内.我想使用此数据创建2D直方图.我一直在看教程,但是它们似乎都显示了如何根据随机数据而非numpy矩阵创建2D直方图.

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.

我有一个numpy矩阵M,其中包含所有值(如上所述).最后,我希望它看起来像这样:

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:

很显然,我的数据会有所不同,所以我的情节应该看起来有所不同.有人可以帮我吗?

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

出于我的目的,下面的 Hooked 正是使用matshow的示例,正​​是我想要的.

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

推荐答案

如果您有来自计数的原始数据,则可以使用plt.hexbin为您创建图(恕我直言,这比正方形格子更好):改编自 hexbin :

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()

如果您已经提到矩阵中已经有Z值,则只需使用plt.imshowplt.matshow:

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')

这篇关于Python:从Numpy矩阵创建2D直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆