栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python使用seaborn绘图:直方图displot、密度图、散点图

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python使用seaborn绘图:直方图displot、密度图、散点图

目录
  • 一、直方图distplot()
  • 二、密度图
    • 2.1 单个样本数据分布密度图

转自:https://www.cnblogs.com/Forever77/p/11399523.html

一、直方图distplot()

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib
import pandas as pd

fig = plt.figure(figsize=(12, 5))
ax1 = plt.subplot(121)
rs = np.random.RandomState(10)  # 设定随机数种子
s = pd.Series(rs.randn(100) * 100)
sns.distplot(s, bins=10, hist=True, kde=True, rug=True, norm_hist=False, color='y', label='distplot', axlabel='x')
plt.legend()

ax1 = plt.subplot(122)
sns.distplot(s, rug=True,
             hist_kws={"histtype": "step", "linewidth": 1, "alpha": 1, "color": "g"},  # 设置箱子的风格、线宽、透明度、颜色,风格包括:'bar', 'barstacked', 'step', 'stepfilled'
             kde_kws={"color": "r", "linewidth": 1, "label": "KDE", 'linestyle': '--'},   # 设置密度曲线颜色,线宽,标注、线形
             rug_kws={'color': 'r'})  # 设置数据频率分布颜色
plt.show()

函数及参数介绍

distplot(a, bins=None, hist=True, kde=True, rug=False, fit=None,hist_kws=None, 
	 	 kde_kws=None, rug_kws=None, fit_kws=None, color=None,  vertical=False, 
		 norm_hist=False, axlabel=None, label=None, ax=None)
  • a 数据源
  • bins 箱数
  • hist、kde、rug 是否显示箱数、密度曲线、数据分布,默认显示箱数和密度曲线不显示数据分析
    {hist,kde,rug}_kws 通过字典形式设置箱数、密度曲线、数据分布的各个特征
  • norm_hist 直方图的高度是否显示密度,默认显示计数,如果kde设置为True高度也会显示为密度
  • color 颜色
  • vertical 是否在y轴上显示图标,默认为False即在x轴显示,即竖直显示
  • axlabel 坐标轴标签
  • label 直方图标签
二、密度图 2.1 单个样本数据分布密度图

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib
import warnings
import pandas as pd
matplotlib.use('TkAgg')
warnings.filterwarnings('ignore')

fig = plt.figure(figsize=(12, 5))
ax1 = plt.subplot(111)
rs = np.random.RandomState(10)  # 设定随机数种子
s = pd.Series(rs.randn(100) * 100)

sns.kdeplot(s, shade=False, color='r', vertical=False)  # 是否填充、设置颜色、是否水平
sns.kdeplot(s, bw=0.2, label="bw: 0.2", linestyle='-', linewidth=1.2, alpha=0.5)
sns.kdeplot(s, bw=2, label="bw: 2", linestyle='-', linewidth=1.2, alpha=0.5, shade=True)
sns.rugplot(s, height=0.1, color='k', alpha=0.5)  # 数据分布
plt.legend()
plt.show()
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1012267.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号