【发布时间】:2017-03-31 05:27:52
【问题描述】:
我需要生成一张类似于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')
【问题讨论】:
-
不能有多对水平和垂直直方图可以产生给定的二维直方图矩阵吗?
-
不确定我是否关注尼克。我需要将 2D 直方图 投影 到 x,y 轴。这是,堆叠所有列中的值(即:对给定列或 x 值的所有 bin 值求和,并对所有列重复),以生成水平直方图(x 轴),以及要生成的所有行中的值垂直直方图(y 轴)。
-
看来你已经有了解决方案("sum all bin values for a given column or x value, and repeat for all columns"),那么问题到底出在哪里?
-
您是在问如何投影数据,即
hx, hy = h.sum(axis=0), h.sum(axis=1)或如何绘制它? -
是的,我在问如何执行该总和,以便结果是每个维度的一维直方图。
hx, hy = h.sum(axis=0), h.sum(axis=1)没有给出预期的结果。
标签: python numpy matplotlib histogram