Issue
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]
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()
Solution
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()
Answered By - Leander
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.