【问题标题】:Histogram fill from a specified x/y value从指定的 x/y 值填充直方图
【发布时间】:2014-10-30 16:12:02
【问题描述】:

我有以下直方图:

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

我的问题是这样的:

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

x 直方图(位置 2、2、1)的 vline 绘制在 x = -0.222 处,y 直方图的 hline 绘制在 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()

【问题讨论】:

  • 请提供您的代码的最小工作示例。
  • 有点困难,因为我的数据是 csv 文件,而且 BPT 图本身非常复杂。任何让我入门的通用/通用/适应性示例都可以。
  • 如果您可以简单地制作随机数据(我假设您使用的是 numpy,所以np.random 会这样做)并提供代码。它让您更加更容易为您提供帮助,否则人们将不得不花费 20 分钟来让子图看起来与您的大致相似,然后才能编写阴影部分。
  • 好的。我现在就这样做。

标签: python matplotlib histogram astronomy


【解决方案1】:

所以这是我的最后一次尝试,但强烈建议您学习 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')

【讨论】:

  • 我能否问一下您是如何指定孵化开始/结束的位置,以及如果我想要 hat="//////" 这将如何实现?非常感谢您的回答。
  • 认真看代码,很简单。影线是 fill_between 函数的参数,影线区域由 fill_region 定义。
  • 抱歉,我在最后看到了舱口 arg。
  • 另外我在实现它时也遇到了问题。即 AttributeError: 'AxesSubplot' 对象没有属性 'step_hist'
  • @MichaelRoberts 这对我来说使用你的测试数据集很好,我不明白你为什么认为它没有。 tillsten 提供了一个完全有效的答案,他们本可以添加几句话来解释它的作用,但是您期望它们贯穿整个代码给您的期望是错误的。
猜你喜欢
  • 2021-10-24
  • 1970-01-01
  • 2015-12-09
  • 1970-01-01
  • 2021-06-13
  • 1970-01-01
  • 2017-12-30
  • 2013-04-16
  • 2017-03-31
相关资源
最近更新 更多