python二维数据读取对齐_从投影的二维直方图绘制对齐的x,y一维直方图

我需要生成一个类似于this example中所示的图像:

不同之处在于,我用numpy的histogram2d生成了一个二维直方图,并用imshow和gridspec绘制了二维直方图:

如何将二维直方图投影到水平和垂直直方图(或曲线)中,使其看起来像第一幅图像一样对齐?在import numpy as np

import matplotlib.pyplot as plt

import matplotlib.gridspec as gridspec

data = # Uploaded to http://pastebin.com/tjLqM9gQ

# Create a meshgrid of coordinates (0,1,...,N) times (0,1,...,N)

y, x = np.mgrid[:len(data[0, :, 0]), :len(data[0, 0, :])]

# duplicating the grids

xcoord, ycoord = np.array([x] * len(data)), np.array([y] * len(data))

# compute histogram with coordinates as x,y

h, xe, ye = np.histogram2d(

xcoord.ravel(), ycoord.ravel(),

bins=[len(data[0, 0, :]), len(data[0, :, 0])],

weights=stars.ravel())

# Projected histograms inx and y

hx, hy = h.sum(axis=0), h.sum(axis=1)

# Define size of figure

fig = plt.figure(figsize=(20, 15))

gs = gridspec.GridSpec(10, 12)

# Define the positions of the subplots.

ax0 = plt.subplot(gs[6:10, 5:9])

axx = plt.subplot(gs[5:6, 5:9])

axy = plt.subplot(gs[6:10, 9:10])

ax0.imshow(h, cmap=plt.cm.viridis, interpolation='nearest',

origin='lower', vmin=0.)

# Remove tick labels

nullfmt = NullFormatter()

axx.xaxis.set_major_formatter(nullfmt)

axx.yaxis.set_major_formatter(nullfmt)

axy.xaxis.set_major_formatter(nullfmt)

axy.yaxis.set_major_formatter(nullfmt)

# Top plot

axx.plot(hx)

axx.set_xlim(ax0.get_xlim())

# Right plot

axy.plot(hy, range(len(hy)))

axy.set_ylim(ax0.get_ylim())

fig.tight_layout()

plt.savefig('del.png')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值