简体   繁体   中英

python matplot.hist - remove gaps between bars

I wish to create a histogram with no gaps between the bars. Tried to add argument bin or range but with no success.

import numpy as np
from matplotlib import pyplot as plt

population = 100
possible_answers = 4
scores = np.random.choice(range(possible_answers),population)
plt.hist(scores)
plt.show()

条形之间有间隙的直方图

You need to use the bins argument to adjust which bins are used.

import numpy as np
from matplotlib import pyplot as plt

population = 100
possible_answers = 4
scores = np.random.choice(range(possible_answers),population)
plt.hist(scores, bins=range(possible_answers+1), ec="k")

plt.show()

在此处输入图片说明

Or, if you want to shift the bins

plt.hist(scores, bins=np.arange(possible_answers+1)-0.5, ec="k")

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM