matplotlib:分段绘制hist2d

时间:2015-11-13 21:51:32

标签: python matplotlib

我想用matplotlib的a功能绘制存储在数组bhist2d中的大样本。但是,生成H, xedges, yedges, img不能直接用于此数据,因为它使用了太多内存。但它的样本数量只有一半,所以我想做一些像

这样的事情
H_1, xedges_1, yedges_1, img_1 = plt.hist2d(a[:len(a)/2], b[:len(b)/2], bins = 10)

接着是

H_2, xedges_2, yedges_2, img_2 = plt.hist2d(a[len(a)/2:], b[len(b)/2:], bins = 10)

虽然在计算第一组变量后可能会删除数组的前半部分。有没有办法合并这两组变量并生成数据的组合图?

1 个答案:

答案 0 :(得分:1)

如果(并且仅当!)手动指定bin边缘,则直方图将兼容。您可以简单地为两个子集添加每个bin的出现次数,然后您将恢复完整的直方图:

import numpy as np
import matplotlib.pyplot as plt

a=np.random.rand(200)*10
b=np.random.rand(200)*10
binmin=min(a.min(),b.min())
binmax=max(a.max(),b.max())
H_1, xedges_1, yedges_1, img_1 = plt.hist2d(a[:len(a)/2], b[:len(b)/2], bins = np.linspace(binmin,binmax,10+1))
H_2, xedges_2, yedges_2, img_2 = plt.hist2d(a[len(a)/2:], b[len(b)/2:], bins = np.linspace(binmin,binmax,10+1))
H_3, xedges_3, yedges_3, img_3 = plt.hist2d(a, b, bins = np.linspace(binmin,binmax,10+1))

结果:

In [150]: (H_1+H_2==H_3).all()
Out[150]: True

您可以使用plt.pcolor轻松绘制图片。这是hist2d似乎使用的内容,尽管有额外的数据转置:

plt.figure()
plt.pcolor((H_1+H_2).T)

img_3(左)vs (H_1+H_2).T(右):

from H_3 from (H_1+H_2).T

相关问题