本文介绍了如何在python代码中添加特定的bin宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手,请多多包涵.

I am new in python so bear with me.

我编写了一个代码来在python中生成随机数,然后对其进行了绘制,但不知道如何将bin宽度放入代码中!我的 bin 宽度应该是 0.1

I wrote a code to generate random numbers in python, then plotted it but did not know how to put the bin width in the code! my bin width should be 0.1

这是我的代码:

import matplotlib.pyplot as plt

import random

data = [random.randint(1,100) for _ in range(10000)]

plt.hist(data)

plt.show()

推荐答案

没有办法直接设置直方图的 bin 宽度.但这不是一个大问题,因为您可以计算出垃圾箱以匹配所需的垃圾箱宽度.

There is no way to directly set the bin width of a histogram plot. But this is not a big problem since you can compute the bins to match the desired bin width.

你可以例如在数据的最小值和最大值之间创建一个数组,步长为 0.1.此数组可用作直方图的 bin.

You may e.g. create an array between the minimum and maximum value of your data and a step size of 0.1. This array can be used as bins for the histogram.

import matplotlib.pyplot as plt
import numpy as np

data = np.random.randn(10000)*10

bins = np.arange(data.min(), data.max()+.1, 0.1)

plt.hist(data, bins=bins)

plt.show()

这篇关于如何在python代码中添加特定的bin宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 02:29