【问题标题】:Plot a histogram in matplotlib (pandas) X and Y axes specifications在 matplotlib (pandas) X 和 Y 轴规格中绘制直方图
【发布时间】:2021-10-07 08:44:32
【问题描述】:

对于六面骰子的滚动,我需要随机模拟该事件 50 次,并在使用箱数 = 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 轴上绘制骰子上每个数字的结果?

【问题讨论】:

  • 使用seaborn.countplotbarplot

标签: python pandas matplotlib histogram probability


【解决方案1】:

对于您正在尝试做的事情,我认为使用条形图更正确,因为不同的可能结果(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()

这应该可以解决问题。希望对您有所帮助!

【讨论】:

    猜你喜欢
    • 2021-10-24
    • 2017-11-17
    • 1970-01-01
    • 2013-12-22
    • 2015-01-07
    • 2016-11-20
    • 2015-10-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多