Search code examples
pythonprobabilitytxt

Create a Probability Distribution Plot with set of data in Python


My data is currently a column of voltage values (sample below) and I would like to create a plot as shown below with Probability on the Y-axis and voltages on the X-axis. So far, I have tried the following code, however the plot does not look like the goal. How do I get probabilities on the Y-axis and voltage values on the x-axis in the distribution plot? Many thanks for your help!

Sample Voltage Values: "-0.00183105 -0.0012207 -0.00061035 0 -0.00030518 -0.00091553 -0.00091553 0.00061035 0.0012207 0.00061035 -0.0012207 -0.00213623 -0.00091553 0.00061035 0.0012207 0.00030518 -0.00061035 -0.00061035 "

Attempted Code:

with open(file, 'r') as current:
    for _ in range(9):  # skip the first 12 lines bc logistical info
        next(current)
    print(current.readline(15))
    next(current)
    vc = pd.Series(current).value_counts(normalize=True, sort=False)
    plt.figure(1)
    plt.hist(vc, bins = 20)
    plt.show()

Goal: enter image description here

Image Source: Karimian, S. F., Modarres, M., & Bruck, H. A. (2020). A new method for detecting fatigue crack initiation in aluminum alloy using acoustic emission waveform information entropy. Engineering fracture mechanics, 223, 106771.


Solution

  • Probably, this shall answer your question:

    import numpy as np
    import matplotlib.pyplot as plt
    txt= "-0.00183105 -0.0012207 -0.00061035 0 -0.00030518 -0.00091553 -0.00091553 0.00061035 0.0012207 0.00061035 -0.0012207 -0.00213623 -0.00091553 0.00061035 0.0012207 0.00030518 -0.00061035 -0.00061035 "
    from collections import Counter
    Vs=[]
    for a in txt.split(" "):
        try:
            Vs.append(float(a))
        except:
            print(a)
    counts=Counter(Vs)
    plt.bar(x=counts.keys(),height=[a/sum(counts.values()) for a in counts.values()],width=3e-4)
    

    Sample's result: enter image description here