python matplot.hist - 删除栏之间的间隙

时间:2022-06-03 14:57:37

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

我希望创建一个直方图,条形之间没有间隙。试图添加参数bin或范围,但没有成功。

from matplotlib import pyplot as plt
import numpy as np

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

histogram with gaps between the bars

直方图与条之间的间隙

1 个解决方案

#1


0  

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

您需要使用bin参数来调整使用哪些bin。

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()

python matplot.hist  - 删除栏之间的间隙

Or, if you want to shift the bins

或者,如果你想移动垃圾箱

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

python matplot.hist  - 删除栏之间的间隙

#1


0  

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

您需要使用bin参数来调整使用哪些bin。

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()

python matplot.hist  - 删除栏之间的间隙

Or, if you want to shift the bins

或者,如果你想移动垃圾箱

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

python matplot.hist  - 删除栏之间的间隙