用总值而不是计数绘制2dhistogram [英] plotting 2dhistogram with sum value rather than count

查看:31
本文介绍了用总值而不是计数绘制2dhistogram的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

论坛上的初学者用户.请帮助.我有一个数据集:x,y坐标,每个x,y都有一个值.我想绘制一个二维直方图,用色标显示每个 bin 中的值的总和.matplotlib hexbin很简单.我可以做这个.例如:

Beginner user on the forum. Help please. I have a data set: x, y coordinates, each x, y has a value. I want to plot a 2d histogram displaying the sum of the values in each bin with color scale. matplotlib hexbin is straight forward. I can do this. eg:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LogNorm

xpos = np.random.rand(0,10)
ypos = np.random.rand(0,10)
plt.hexbin(x = xpos, y = ypos, C=mass, cmap= plt.cm.jet, gridsize=100, reduce_C_function=sum, bins="log")  
cb = plt.colorbar()
cb.ax.set_ylabel('log (sum value in each bin)')
plt.xlabel('Xpos')
plt.ylabel('Ypos')
plt.show()

但是,我正在努力使用 histogram2d 或 matplotlib hist2d 制作类似的图.我认为我必须以某种方式结合binned_statistic_2d和histogram2d.如果我将上面的plt.hexbin行替换为此,没问题:

However, I'm struggling to make a similar plot with histogram2d or matplotlib hist2d. I think i have to combine binned_statistic_2d and histogram2d somehow. No problem if I replace plt.hexbin line above to this:

plt.hist2d(x = xpos, y = ypos, bins = 50, norm = LogNorm())

有什么线索吗?我在论坛上看过,但似乎找不到有效的代码.

Any clue? I have look on the forum but can't seem to find a working code.

推荐答案

您可以在绘图之前计算要在分箱 2D 图中显示的值,然后显示为 imshow 图.

You could calculate the values to show in the binned 2D plot prior to plotting and then show as an imshow plot.

如果您乐于使用熊猫,一种选择是根据要切割( pandas.cut )的x和y数据对质量数据进行分组.然后应用总和(.sum())并进行堆叠以获取数据透视表.

If you're happy to use pandas, one option would be to group the mass data accordings to cut (pandas.cut) x and y data. Then apply the sum (.sum()) and unstack to obtain a pivot table.

df.mass.groupby([pd.cut(df.x, bins=xbins, include_lowest=True), 
                 pd.cut(df.y, bins=ybins, include_lowest=True)]) \
               .sum().unstack(fill_value=0)

这是一个完整的示例:

import numpy as np; np.random.seed(1)
import pandas as pd
import matplotlib.pyplot as plt
import  matplotlib.colors

xpos = np.random.randint(0,10, size=50)
ypos = np.random.randint(0,10, size=50)
mass = np.random.randint(0,75, size=50)

df = pd.DataFrame({"x":xpos, "y":ypos, "mass":mass})

xbins = range(10)
ybins = range(10)
su = df.mass.groupby([pd.cut(df.x, bins=xbins, include_lowest=True), 
                     pd.cut(df.y, bins=ybins, include_lowest=True)]) \
            .sum().unstack(fill_value=0)
print su
im = plt.imshow(su.values, norm=matplotlib.colors.LogNorm(1,300))
plt.xticks(range(len(su.index)), su.index, rotation=90)
plt.yticks(range(len(su.columns)), su.columns)
plt.colorbar(im)
plt.show()

这篇关于用总值而不是计数绘制2dhistogram的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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