Suppose I have data [1,2,3, 7,8,9,9, 20,30,40,100,1000] for which I want to draw a histogram with Python. All the bins I care about is [0,5], [5,10], and [10, +∞). How can I do it?
The following wouldn't do it, of course.
import matplotlib.pyplot as plt
data = [1,2,3, 7,8,9,9, 20,30,40,100,1000]
plt.figure()
plt.hist(data, bins=5, color="rebeccapurple")
plt.show()
In case of forcing to show a histogram with customized x-range, you might need to process your data first.
I made a list of ranges and also x_ticklabels to show x-axis with range.
import matplotlib.pyplot as plt
import numpy as np
data = [1,2,3, 7,8,9,9, 20,30,40,100,1000,500,200]
data = np.array(data)
bin_range = [
[0, 5],
[5, 10],
[10, 10000] # enough number to cover range
]
data2plot = np.zeros(len(bin_range))
for idx, (low, high) in enumerate(bin_range):
data2plot[idx] = ((low <= data) & (data < high)).sum()
fig = plt.figure()
ax = fig.add_subplot(111)
ax.bar(range(len(bin_range)), data2plot)
x_labels = [
f"{low}~{high}" for idx, (low, high) in enumerate(bin_range)
]
ax.set_xticks(range(len(bin_range)))
ax.set_xticklabels(x_labels)
plt.show()

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With