在 matplotlib (pandas) X 和 Y 轴规范中绘制直方图
Plot a histogram in matplotlib (pandas) X and Y axes specifications
对于六面骰子的滚动,我需要随机模拟该事件 50 次并绘制骰子上每个数字的结果直方图,同时使用 number of bins=6
我尝试了以下方法:
import random
test_data = [0, 0, 0, 0, 0, 0]
n = 50
for i in range(n):
result = random.randint(1, 6)
test_data[result - 1] = test_data[result - 1] + 1
plt.hist(test_data,bins=6)
有没有办法在 x 轴上绘制骰子的数字,并在 y 轴上绘制骰子上每个数字的结果?
对于您正在尝试做的事情,我认为使用条形图更为正确,因为不同的可能结果(X 轴)不是频率。那么,为了您的目的,我认为最好使用字典来做这样的事情:
import random
from matplotlib import pyplot as plt
test_data = {"1":0, "2":0, "3":0, "4":0, "5":0, "6":0}
n = 50
for i in range(n):
result = random.randint(1, 6)
test_data[str(result)] += 1
plt.bar(test_data.keys(), test_data.values())
plt.show()
这应该可以解决问题。希望对您有所帮助!
对于六面骰子的滚动,我需要随机模拟该事件 50 次并绘制骰子上每个数字的结果直方图,同时使用 number of bins=6
我尝试了以下方法:
import random
test_data = [0, 0, 0, 0, 0, 0]
n = 50
for i in range(n):
result = random.randint(1, 6)
test_data[result - 1] = test_data[result - 1] + 1
plt.hist(test_data,bins=6)
有没有办法在 x 轴上绘制骰子的数字,并在 y 轴上绘制骰子上每个数字的结果?
对于您正在尝试做的事情,我认为使用条形图更为正确,因为不同的可能结果(X 轴)不是频率。那么,为了您的目的,我认为最好使用字典来做这样的事情:
import random
from matplotlib import pyplot as plt
test_data = {"1":0, "2":0, "3":0, "4":0, "5":0, "6":0}
n = 50
for i in range(n):
result = random.randint(1, 6)
test_data[str(result)] += 1
plt.bar(test_data.keys(), test_data.values())
plt.show()
这应该可以解决问题。希望对您有所帮助!