python - 从指定的 x/y 值填充直方图

标签 python matplotlib histogram astronomy

我有以下直方图:

BPT Diagnostic Diagram

现在,我敢肯定你们中的许多人会向我指出 matplotlib 库和其他资源的方向,但出于某种原因,我想到的以及随后从各种不同来源读取的内容并不完全有效用我的轴直方图。 (如果我错了请纠正我!)

我的问题是:

如何从覆盖在 (2,2,1) 和 (2,2,4) 图中的直方图上的线分割位置填充直方图。

x 直方图(位置 2,2,1)有一个 vline 绘制在 x = -0.222,y 直方图有一个 hline code> 绘制在 y = 0.49 处。

一个示例工作代码,针对来自原始数组的任何数组进行了修改,如下所示:

import numpy as np
import scipy
import matplotlib as mpl
import matplotlib.pyplot as plt
import pylab

X = 0.32, 0.41, 0.45, 0.53, -0.23,  0.34,  0.35, 0.47, 0.48, 0.33, 0.49, -0.10, -0.23,       0.45, 0.19
Y = 0.56, 0.67, 0.49, 0.61,  0.00, -0.02, -0.12, 0.12, 0.23, 0.44, 0.56,  0.13,  0.56, 0.67, 0.28

binsize = 0.1

min_x_data, max_x_data   = np.min(X), np.max(X)
num_x_bins               = np.floor((max_x_data - min_x_data) / binsize)

min_y_data, max_y_data   = np.min(Y), np.max(Y)
num_y_bins               = np.floor((max_y_data - min_y_data) / bin size)

fig = plt.figure(221)

axScatter = fig.add_subplot(223)
axScatter.scatter(X, Y)
axScatter.set_xlim(-2.0, 1.5)
axScatter.set_ylim(-2.0, 2.5)

axHistX = fig.add_subplot(221)
axHistX.set_xlim(-2.0, 1.5)
axHistX.set_ylim(0, 10)

axHistY = fig.add_subplot(224)
axHistY.set_xlim(0, 10)
axHistY.set_ylim(-2.0, 2.5)

axHistX.hist(X, num_x_bins, ec='0.3', fc='none', histtype='step')
axHistY.hist(Y, num_y_bins, ec='0.3', fc='none', histtype='step', orientation='horizontal')

axScatter.axhline(y=0.49, xmin=0, xmax=1, linestyle='-.',c='k')
axScatter.axvline(x=-0.222, ymin=0, ymax=1, linestyle='-.',c='k')
axHistX.axvline(x=-0.222, ymin=0, ymax=1, linestyle='-.',c='k')
axHistY.axhline(y=0.49, xmin=0, xmax=1, linestyle='-.',c='k')

plt.show()

最佳答案

这是我最后一次尝试,但强烈建议你学习 python 的基础知识和 matplotlib。

def step_hist(ax, X, num_x_bins=10,               
              hatch_from= -2, hatch_till=0.5,
              orientation='h'):
    #make histogram by hand.
    hist, edges = np.histogram(X, bins=num_x_bins)
    #generate (x,y) points for a step-plot
    edges = np.repeat(edges, 2)
    hist = np.hstack((0, np.repeat(hist, 2), 0))
    #plot step_hist
    #indices where we want the  plot hatached
    fill_region  =(hatch_from<edges)&(edges<hatch_till) 
    #apply hatching my using fill_between.
    if orientation == 'h':    
        ax.fill_between(edges[fill_region], 
                        hist[fill_region], 0, 
                        color='none', edgecolor='k',
                        hatch='xxx')
        ax.plot(edges, hist, 'k')
    elif orientation == 'v':
        ax.fill_betweenx(edges[fill_region], 
                        hist[fill_region], 0, 
                        color='none', edgecolor='k',
                        hatch='xxx')
        ax.plot(hist, edges, 'k')

a, b = np.random.randn(500), np.random.randn(500)

ax_top = plt.subplot(221)
ax_right = plt.subplot(224)
ax_cent = plt.subplot(223)

ax_cent.scatter(a, b, c='k')
step_hist(ax_top, a)
step_hist(ax_right, a, orientation='v')

resulting picture

关于python - 从指定的 x/y 值填充直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26657987/

相关文章:

python - 为什么这个 3 行程序返回 None 作为输出?

python - 如何防止 Matplotlib 剪掉我的轴标签?

python - 从 matplotlib.patches 中的 CirclePolygon 获取坐标

r - 如何从R中的汇总数据创建直方图?

c++ - 在 C++ 中计算单 channel 直方图的均值和标准差

python - 在 Matplotlib 中绘制多个直方图 - 颜色或并排条

python - django长时间运行进程数据库连接

python - 如何使用 python glob.glob() 打印子目录中的文件名

python - 如何使用列表理解将二维列表中的值匹配到另一个二维列表?

python - 如何在 matplotlib 的注释中设置箭头的起点?