Search code examples
pythonmatplotlibstatisticsvisualization

How to plot 1D array using python to get 25th, 50th and 75th percentiles


Hello I have an array

x = [9.904725629894529e-06, 1.2634955055546016e-05, 1.5201896530925296e-05, 2.9845547032891773e-05, 3.49247275153175e-05, 3.4970289561897516e-05, 4.780302697326988e-05, 4.802399416803382e-05, 4.810681639355607e-05, 4.843798524234444e-05, 4.859635737375356e-05, 5.9152505855308846e-05, 5.9193022025283426e-05, 5.9363908803788945e-05, 5.9468671679496765e-05, 6.630286952713504e-05, 7.005851512076333e-05, 7.014916627667844e-05, 8.021695248316973e-05, 8.989680645754561e-05, 9.008277265820652e-05, 9.028125350596383e-05, 9.037737618200481e-05, 9.04681728570722e-05, 9.083149052457884e-05, 0.00021005164308007807, 0.00021028853370808065, 0.00021039179409854114, 0.00021039319108240306, 0.00021107416250742972, 0.00021134318376425654, 0.00021135005226824433, 0.00021196113084442914, 0.00021199103503022343, 0.0002120915160048753, 0.0002126666222466156, 0.000213045728742145, 0.00021316968195606023, 0.00021321520034689456, 0.00021339277736842632, 0.00021368247689679265, 0.0002137374976882711, 0.00021400606783572584, 0.00021451167413033545, 0.00021501131413970143]

I want to plot this in python to find 25th, 50th and 75th percentile value.

I used plt.plot(x) plt.show() But the distribution doesn't look in a way like we can distinguish the numbers.


Solution

  • You can use numpy to get percentiles for data. This following is from the documentation for percentile.

    import numpy as np
    
    # 1D array
    arr = [20, 2, 7, 1, 34]
    print("arr : ", arr)
    print("50th percentile of arr : ",
          np.percentile(arr, 50))
    print("25th percentile of arr : ",
          np.percentile(arr, 25))
    print("75th percentile of arr : ",
          np.percentile(arr, 75))
    

    Output:

    arr :  [20, 2, 7, 1, 34]
    50th percentile of arr :  7.0
    25th percentile of arr :  2.0
    75th percentile of arr :  20.0
    

    to apply this to your code this should show you the percentiles nicely

    import matplotlib.pyplot as plt
    import numpy as np
    
    x = [
        9.904725629894529e-06,
        1.2634955055546016e-05,
        1.5201896530925296e-05,
        2.9845547032891773e-05,
        3.49247275153175e-05,
        3.4970289561897516e-05,
        4.780302697326988e-05,
        4.802399416803382e-05,
        4.810681639355607e-05,
        4.843798524234444e-05,
        4.859635737375356e-05,
        5.9152505855308846e-05,
        5.9193022025283426e-05,
        5.9363908803788945e-05,
        5.9468671679496765e-05,
        6.630286952713504e-05,
        7.005851512076333e-05,
        7.014916627667844e-05,
        8.021695248316973e-05,
        8.989680645754561e-05,
        9.008277265820652e-05,
        9.028125350596383e-05,
        9.037737618200481e-05,
        9.04681728570722e-05,
        9.083149052457884e-05,
        0.00021005164308007807,
        0.00021028853370808065,
        0.00021039179409854114,
        0.00021039319108240306,
        0.00021107416250742972,
        0.00021134318376425654,
        0.00021135005226824433,
        0.00021196113084442914,
        0.00021199103503022343,
        0.0002120915160048753,
        0.0002126666222466156,
        0.000213045728742145,
        0.00021316968195606023,
        0.00021321520034689456,
        0.00021339277736842632,
        0.00021368247689679265,
        0.0002137374976882711,
        0.00021400606783572584,
        0.00021451167413033545,
        0.00021501131413970143,
    ]
    
    percentile_25 = np.percentile(x, 25)
    percentile_50 = np.percentile(x, 50)
    percentile_75 = np.percentile(x, 75)
    
    plt.figure("plot1")
    plt.plot(x)
    plt.axhline(percentile_25, color="C1", label="25")
    plt.axhline(percentile_50, color="C2", label="50")
    plt.axhline(percentile_75, color="C3", label="75")
    plt.legend(loc="best", title="percentiles")
    plt.xlabel("x_axis_label")
    plt.ylabel("y_axis_label")
    plt.title("stack title")
    plt.show()
    

    output graph:

    enter image description here