在 Seaborn 直方图中绘制一个空箱

数据挖掘 Python 可视化 海运
2021-09-28 05:45:33

我正在浏览The Coding Train 的 YouTube 系列模拟我正在尝试绘制一些过滤后的随机数,但 seaborn 在直方图的中间留下了一个奇怪的间隙。

我的数据是通过收集大于某些函数输出的随机数来过滤的,比如 是的=X2. 我还测试了从 0 到 100 的随机整数绘制输出的图形,并且在直方图中没有得到相同的间隙,所以我认为问题可能出在我的数据上。但是,我也用pygame做了一个图表,没有差距,我也检查了我的数据;从 1 到 100 的数字都表示出来。

代码:

# Choose a function.
# Make randints a, b.
# If b > function(a), return b.
# Repeat and collect b.
# Graph b as histogram.

import collections
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

sample_size = 10**4
dice_max = 100
def function(x): return (x)**2

def main():
    # Debug: Make random data.
    # values = np.random.randint(0, dice_max, sample_size) # Test different data. Problem goes away.

    # Debug: Inspect values then press enter.
    # for i,(k,v) in enumerate(sorted(collections.Counter(values).most_common())): print(i,k,v); input()

    values = [montecarlo(function) for _ in range(sample_size)]
    pg_histograph(collections.Counter(values))
    sns.set()
    plt.show(sns.distplot(values, bins=dice_max, kde=False, norm_hist=False))

def montecarlo(function):
    """Make randints a, b. If b > function(a), return b."""
    y, b = 1, 0
    while not b > y:
        a, b = (np.random.randint(dice_max) for _ in range(2))
        y = function(a)
    return b

def pg_histograph(bins):
    """Make histograph using pygame."""
    import pygame as pg
    window_width = 800
    window_height = 800
    bar_color = pg.Color('black')
    bg_color = pg.Color('white')
    pg.init()
    window = pg.display.set_mode((window_width, window_height))
    window.fill(bg_color)

    bar_width = int(window_width / len(bins))
    max_count = bins.most_common(1)[0][1]
    for bin_, count in bins.items():
        bar_height = round(count / max_count * window_height) # Fit y values to window.
        x = bin_ * bar_width # Fit x values to bar size.
        y = window_height - bar_height # Bars extend downward from x,y, so they need to be shifted up by bar_height.
        pg.draw.rect(window, bar_color, (x, y, bar_width, bar_height))
    pg.display.update()

main()
1个回答

在使用离散数据绘制直方图时,可能会在 Seaborn 中绘制空箱。

可以通过添加来防止绘制空箱discrete=True

sns.histplot(values, bins=dice_max, discrete=True)