Search code examples
pythonmatplotlibbar-charthistogram

Matplotlib overlay bimodal histograms


I am trying to put three "bimodal/binary histograms" on the same plot, labeled, and with the actual values instead of bins. Not sure if it makes sense... However, I would like to show the only 2 values that are present in each vector x1, x2 and x3.

Here is the code:

import matplotlib.pyplot as plt

x1 = [0.003996747444034554, 0.003996747444034554, 0.003996747458586469, 0.003996747444034554, 0.003996747444034554, 0.003996747444034554, 0.003996747458586469, 0.003996747444034554, 0.003996747444034554, 0.003996747444034554]
x2 = [0.003996536252088845, 0.003996536252088845, 0.003996536252088845, 0.003996536310296506, 0.003996536252088845, 0.003996536252088845, 0.003996536252088845, 0.003996536252088845, 0.003996536252088845, 0.003996536252088845]
x3 = [0.003996643703430891, 0.003996643703430891, 0.003996643703430891, 0.003996643703430891, 0.003996643703430891, 0.003996643761638552, 0.003996643703430891, 0.003996643703430891, 0.003996643703430891, 0.003996643703430891]

fig, axs = plt.subplots(1,3, figsize=([7,4]), sharey = True)
axs[0].hist(x1)
axs[1].hist(x2)
axs[2].hist(x3)
plt.tight_layout()

plt.figure(figsize=([7,4]))
plt.hist([x1,x2,x3])

Figure 1 is the plot of each histogram (x1, x2 and x3): enter image description here

Figure 2 is the result of plotting all at once: enter image description here


Solution

  • The difference within each vector are much smaller than the differences between the vectors' means, therefore both histogram bars for each vector coincide.
    For this particular case you could use a stem plot instead:

    import matplotlib.pyplot as plt
    import pandas as pd
    
    x1 = [0.003996747444034554, 0.003996747444034554, 0.003996747458586469, 0.003996747444034554, 0.003996747444034554, 0.003996747444034554, 0.003996747458586469, 0.003996747444034554, 0.003996747444034554, 0.003996747444034554]
    x2 = [0.003996536252088845, 0.003996536252088845, 0.003996536252088845, 0.003996536310296506, 0.003996536252088845, 0.003996536252088845, 0.003996536252088845, 0.003996536252088845, 0.003996536252088845, 0.003996536252088845]
    x3 = [0.003996643703430891, 0.003996643703430891, 0.003996643703430891, 0.003996643703430891, 0.003996643703430891, 0.003996643761638552, 0.003996643703430891, 0.003996643703430891, 0.003996643703430891, 0.003996643703430891]
    
    g1 = pd.Series(x1).groupby(x1).count()
    plt.stem(g1.index, g1.values, markerfmt='C0o', linefmt='C0-')
    
    g2 = pd.Series(x2).groupby(x2).count()
    plt.stem(g2.index, g2.values, markerfmt='C1o', linefmt='C1-')
    
    g3 = pd.Series(x3).groupby(x3).count()
    plt.stem(g3.index, g3.values, markerfmt='C2o', linefmt='C2-')
    

    enter image description here

    (you'll need to zoom in to see the difference between the two x-positions of each vector)