数据可视化seaborn

# seaborn 这个包是在matplotlib的基础上封装的一个绘图工具包,支持numpy和pandas的统计结果可视化

import numpy as np

import pandas as pd

from scipy import stats

import matplotlib.pyplot as plt

import seaborn as sn

# 可视化单一变量、二维变量,比较数据集中各变量的分布情况
# 正态分布、高斯分布

# loc 概率分布均值  scale 标准差  size样本个数

x1 = np.random.normal(loc=0, scale=1, size=1000)

# bins 直方图 柱子的个数

# kde 是否显示核密度估计图,默认显示,设置False不显示

sn.distplot(x1, bins=100, kde=False)

# rug 默认为False不显示,设置为True 显示观测的小细条(边际毛毯)

sn.distplot(x1, bins=20, kde=False, rug=True)

# 核密度估计图

# hist 设置为False  不会显示条形图

sn.distplot(x1, hist=True, bins=20, kde=True, rug=True)

x2 = np.random.randint(0, 100, 500)

# 直方图+核密度估计图

sn.distplot(x2)

# 直方图+边际小细条

sn.distplot(x2, bins=20, kde=False, rug=True)

# 核密度估计图 + 边际小细条

sn.distplot(x2, hist=False, kde=True, rug=True)

# 核密度估计图

# shade 将范围内进行填充

sn.kdeplot(x2,shade=True)

sn.rugplot(x2)

df_obj1 = pd.DataFrame({
        'x':np.random.randn(500),
        'y':np.random.randn(500)
    })
df_obj2 = pd.DataFrame({
        'x':np.random.randn(500),
        'y':np.random.randint(0,100,500)
})

# 散点图

sn.jointplot(x='x', y='y', data=df_obj1)

# 二维直方图

# kind 图的类型

sn.jointplot(x='x', y='y', data=df_obj1, kind='hex')

# 二维核密度估计图

sn.jointplot(x='x',y='y',data=df_obj1,kind='kde')

# 数据集中的变量间的关系可视化图

dataset = sn.load_dataset('tips')

sn.pairplot(dataset)

# 类别数据可视化

exercise = sn.load_dataset('exercise')

# 类别散布图

sn.stripplot(x='diet',y='pulse',data=exercise)

# 带图例

sn.swarmplot(x='diet',y='pulse',data=exercise,hue='kind')

# 盒子图

sn.boxplot(x='diet',y='pulse',data=exercise,hue='kind')

# 小提琴图

sn.violinplot(x='diet',y='pulse',data=exercise,hue='kind')

# 柱状图

sn.barplot(x='diet',y='pulse',data=exercise,hue='kind')

# 点图

sn.pointplot(x='diet',y='pulse',data=exercise,hue='kind')

# labels= 范围对应的标签

plt.pie(pd.Series(np.random.randint(5000,10000,3)),labels=['M','F','T'],autopct='%.1f%%')

# 将图表保存到本地

plt.savefig('test.png')

plt.show()

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章