当前位置: 移动技术网 > IT编程>脚本编程>Python > 荐 Python可视化matplotlib13-iris鸢尾花数据集|histogram直方图

荐 Python可视化matplotlib13-iris鸢尾花数据集|histogram直方图

2020年07月11日  | 移动技术网IT编程  | 我要评论

直方图(histogram)展示离散型数据分布情况,直观理解为将数据按照一定规律分区间,统计每个区间中落入的数据频数,绘制区间与频数的柱状图即为直方图。

公众号:“pythonic生物人“,分享数据科学,来关注哈!

写作时间:2020-07-09夜,有问题可留言或者我的公众号。

目录

 

1、绘图数据集准备

2、matplotlib.pyplot.hist直方图参数详解

3、参考资料

 4、我的公众号


1、绘图数据集准备

 使用sklearn内置的鸢尾花iris数据集,数据集详细介绍见:Matplotlib-02-iris鸢尾花数据集|scatter散点图

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from pandas import Series,DataFrame
from sklearn import datasets 
iris=datasets.load_iris()
x, y = iris.data, iris.target
pd_iris = pd.DataFrame(np.hstack((x, y.reshape(150, 1))),columns=['sepal length(cm)','sepal width(cm)','petal length(cm)','petal width(cm)','class'] )

选取pd_iris['sepal length(cm)']数据绘制直方图 ,查看数据基本情况:

pd_iris['sepal length(cm)'].head()
0    5.1
1    4.9
2    4.7
3    4.6
4    5.0
Name: sepal length(cm), dtype: float64

pd_iris['sepal length(cm)'].describe()
count    150.000000
mean       5.843333
std        0.828066
min        4.300000
25%        5.100000
50%        5.800000
75%        6.400000
max        7.900000
Name: sepal length(cm), dtype: float64

2、matplotlib.pyplot.hist直方图参数详解

修改对应参数,即可体验对应参数的功能;

大部分参数使用默认值即可。

import palettable
import random
plt.figure(dpi=150)
data=pd_iris['sepal length(cm)']
n, bins, patches=plt.hist(x=data,
                          ##箱子数(bins)设置,以下三种不能同时并存
                          #bins=20,#default: 10
                          #bins=[4,6,8],#分两个箱子,边界分别为[4,6),[6,8]
                          #bins='auto',# 可选'auto', 'fd', 'doane', 'scott', 'stone', 'rice', 'sturges', or 'sqrt'.
                          #选择最合适的bin宽,绘制一个最能反映数据频率分布的直方图 
                         
                          #range=(5,7),#最左边和最右边箱子边界,不指定时,为(x.min(), x.max())
                          #density=True, #默认为False,y轴显示频数;为True y轴显示频率,频率统计结果=该区间频数/(x中总样本数*该区间宽度)
                          #weights=np.random.rand(len(x)),#对x中每一个样本设置权重,这里随机设置了权重
                          cumulative=False,#默认False,是否累加频数或者频率,及后面一个柱子是前面所有柱子的累加
                          bottom=0,#设置箱子y轴方向基线,默认为0,箱子高度=bottom to bottom + hist(x, bins)
                          histtype='bar',#直方图的类型默认为bar{'bar', 'barstacked', 'step', 'stepfilled'}
                          align='mid',#箱子边界值的对齐方式,默认为mid{'left', 'mid', 'right'}
                          orientation='vertical',#箱子水平还是垂直显示,默认垂直显示('vertical'),可选'horizontal'
                          rwidth=1.0,#每个箱子宽度,默认为1,此时显示50%
                          log=False,#y轴数据是否取对数,默认不取对数为False
                          color=palettable.colorbrewer.qualitative.Dark2_7.mpl_colors[3],
                          label='sepal length(cm)',#图例
                          #normed=0,#功能和density一样,二者不能同时使用
                          facecolor='black',#箱子颜色 
                          edgecolor="black",#箱子边框颜色
                          stacked=False,#多组数据是否堆叠
                          alpha=0.5#箱子透明度
                         )
plt.xticks(bins)#x轴刻度设置为箱子边界

for patch in patches:#每个箱子随机设置颜色
    patch.set_facecolor(random.choice(palettable.colorbrewer.qualitative.Dark2_7.mpl_colors))

#直方图三个返回值
print(n)#频数
print(bins)#箱子边界
print(patches)#箱子数

#直方图绘制分布曲线
plt.plot(bins[:10],n,'--',color='#2ca02c')
plt.hist(x=[i+0.1 for i in data],label='new sepal length(cm)',alpha=0.3)
plt.legend()

[ 9. 23. 14. 27. 16. 26. 18. 6. 5. 6.]

[4.3 4.66 5.02 5.38 5.74 6.1 6.46 6.82 7.18 7.54 7.9 ]

<a list of 10 Patch objects> 

  

3、参考资料

https://matplotlib.org/api/_as_gen/matplotlib.pyplot.hist.html?highlight=hist#matplotlib.pyplot.hist

 4、我的公众号

"pythonic生物人",持续分享数据科学和生物信息干货,欢迎来看看。

 

本文地址:https://blog.csdn.net/qq_21478261/article/details/107218300

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网