matplotlib: disregard outliers when plotting

后端 未结 4 754
广开言路
广开言路 2020-12-04 13:13

I\'m plotting some data from various tests. Sometimes in a test I happen to have one outlier (say 0.1), while all other values are three orders of magnitude smaller.

<
4条回答
  •  失恋的感觉
    2020-12-04 14:09

    I think using pandas quantile is useful and much more flexible.

    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    
    fig = plt.figure()
    ax1 = fig.add_subplot(121)
    ax2 = fig.add_subplot(122)
    
    pd_series = pd.Series(np.random.normal(size=300)) 
    pd_series_adjusted = pd_series[pd_series.between(pd_series.quantile(.05), pd_series.quantile(.95))] 
    
    ax1.boxplot(pd_series)
    ax1.set_title('Original')
    
    ax2.boxplot(pd_series_adjusted)
    ax2.set_title('Adjusted')
    
    plt.show()
    

提交回复
热议问题