简体   繁体   English

如何在Python Pandas直方图中设置特定区间?

[英]How to set specific intervals in Python Pandas histograms?

I have the standard dataset from R called ToothGrowth .我有来自 R 的标准数据集,称为ToothGrowth I have this R code that makes me two histograms of tooth growth in guinea pigs based by the different delivery methods:我有这个 R 代码,它根据不同的交付方式制作了两个豚鼠牙齿生长直方图:

vctooth <- ToothGrowth[1:30, c(3,1)]
ojtooth <- ToothGrowth[31:60, c(3,1)]

hist(vctooth$len, main = 'Length of tooth growth in Guinea Pigs
                          Delivery method: ascorbic acid',
                  xlab = "Length of Growth",
                  ylab="Frequency",
                  ylim=c(0,10),)
hist(ojtooth$len, main = 'Length of tooth growth in Guinea Pigs
                          Delivery method: orange juice',
                  xlab = "Length of Growth",
                  ylab="Frequency",
                  ylim=c(0,10),)

How can I specifically set different intervals and make histogram for the vctooth and ojtooth in Python Pandas? Python Pandas中的vctoothojtooth如何具体设置不同的时间间隔并制作直方图? Down below there is the ToothGrowth dataset:下面是ToothGrowth数据集:

"len","supp","dose"
4.2,"VC",0.5
11.5,"VC",0.5
7.3,"VC",0.5
5.8,"VC",0.5
6.4,"VC",0.5
10,"VC",0.5
11.2,"VC",0.5
11.2,"VC",0.5
5.2,"VC",0.5
7,"VC",0.5
16.5,"VC",1
16.5,"VC",1
15.2,"VC",1
17.3,"VC",1
22.5,"VC",1
17.3,"VC",1
13.6,"VC",1
14.5,"VC",1
18.8,"VC",1
15.5,"VC",1
23.6,"VC",2
18.5,"VC",2
33.9,"VC",2
25.5,"VC",2
26.4,"VC",2
32.5,"VC",2
26.7,"VC",2
21.5,"VC",2
23.3,"VC",2
29.5,"VC",2
15.2,"OJ",0.5
21.5,"OJ",0.5
17.6,"OJ",0.5
9.7,"OJ",0.5
14.5,"OJ",0.5
10,"OJ",0.5
8.2,"OJ",0.5
9.4,"OJ",0.5
16.5,"OJ",0.5
9.7,"OJ",0.5
19.7,"OJ",1
23.3,"OJ",1
23.6,"OJ",1
26.4,"OJ",1
20,"OJ",1
25.2,"OJ",1
25.8,"OJ",1
21.2,"OJ",1
14.5,"OJ",1
27.3,"OJ",1
25.5,"OJ",2
26.4,"OJ",2
22.4,"OJ",2
24.5,"OJ",2
24.8,"OJ",2
30.9,"OJ",2
26.4,"OJ",2
27.3,"OJ",2
29.4,"OJ",2
23,"OJ",2

The output should be like this: output 应该是这样的: 在此处输入图像描述

You can use iloc to set indexes:您可以使用iloc设置索引:

import matplotlib.pyplot as plt 

plt.figure(figsize=(15, 5))

# you can filter the indexes with iloc
plt.hist(df.iloc[:30]['len'])
plt.xlabel("Length of Growth")
plt.title('Length of tooth growth in Guinea Pigs Delivery method: ascorbic acid')
plt.ylim((0, 10))

plt.show()

Output: Output: 图片

Edit for ylabel: plt.ylabel('Frequency')编辑 ylabel: plt.ylabel('Frequency')

You can do it by inputting a list to bins parameter of hist method:您可以通过将列表输入 hist 方法的 bins 参数来实现:

dt.hist(bins=[0, 0.5, 1.0])

For example on some random dataframe:例如在一些随机的 dataframe 上:

pd.DataFrame(random.random(100)).hist(bins=[0, 0.5, 1.0])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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