Home > OS >  Merge multiple dataset to create one histogram in Python
Merge multiple dataset to create one histogram in Python

Time:07-05

There are three pairs representing the start and stop point of three horizontal lines. How to create a histogram that shows the overlapping segment counts? Assuming bin=[0.0, 0.1, 0.2, ... 3.0] enter image description here

The histogram function from matplotlib treats these pairs as three different datasets.

import matplotlib.pyplot as plt
import numpy as np
a = (1.5, 2.8)
b = (1.9, 3.0)
c = (1.6, 2.1)

pair_list = [a,b,c]
pair_data = []
for pair in pair_list:
    pair_data.append(np.arange(pair[0],pair[1],0.1))

plt.hist(pair_data)

plt.show()

enter image description here

CodePudding user response:

As Alex said, changing append to extend does the trick. Just make sure to get the right amount of bins to get the result you want (default is 10).

import matplotlib.pyplot as plt
import numpy as np
a = (1.5, 2.8)
b = (1.9, 3.0)
c = (1.6, 2.1)

pair_list = [a,b,c]
pair_data = []
for pair in pair_list:
    pair_data.extend(np.arange(pair[0],pair[1],0.1))

plt.hist(pair_data,bins=int((np.max(pair_data)-np.min(pair_data))*10) 1)
# (max-min)/binsize   1 gives you bins of width binsize
plt.show()
  • Related