【问题标题】:Remove data above threshold in histogram在直方图中删除高于阈值的数据
【发布时间】:2018-12-21 00:05:53
【问题描述】:

我的数据显示在带有以下代码的直方图中: 角度=数据[列[3]]

num_bins = 23
avg_samples_per_bin = 200

# len(data['steering'])/num_bins
hist, bins = np.histogram(data['steering'], num_bins)
width = 0.7 * (bins[1] - bins[0])
center = (bins[:-1] + bins[1:]) * 0.5
plt.bar(center, hist, align='center', width=width)
plt.plot((np.min(angles), np.max(angles)), (avg_samples_per_bin, avg_samples_per_bin), 'k-')

显示以下内容:

我正在寻找一个可以删除行上所有数据的函数。或者换句话说,每个 bin 中的数据不能超过 200。

有没有一种巧妙的方法来做到这一点?

【问题讨论】:

    标签: python numpy matplotlib


    【解决方案1】:

    您可以选择低于某个阈值的值来屏蔽您的数组。例如:

    import numpy as np
    import matplotlib.pyplot as plt
    
    fig, (ax1, ax2) = plt.subplots(1,2)
    ax1.set_title("Some data")
    ax2.set_title("Masked data < 80")
    
    np.random.seed(10)
    data = np.random.randn(1000)
    
    num_bins = 23
    avg_samples_per_bin = 200
    
    hist, bins = np.histogram(data, num_bins)
    width = 0.7 * (bins[1] - bins[0])
    center = (bins[:-1] + bins[1:]) * 0.5
    ax1.bar(center, hist, align='center', width=width)
    
    threshold = 80
    mask = hist < threshold
    
    new_center = center[mask]
    new_hist = hist[mask]
    
    ax2.bar(new_center, new_hist, align="center", width=width)
    
    plt.show()
    

    这给出了:

    【讨论】:

      猜你喜欢
      • 2019-11-28
      • 1970-01-01
      • 2021-12-14
      • 1970-01-01
      • 2021-09-01
      • 1970-01-01
      • 2017-01-18
      • 2014-08-08
      • 1970-01-01
      相关资源
      最近更新 更多