python astype category_python数据分布型图表

f33ef3feda0fb8719cb817febf1567e5.png

数据分布图表系列

正态分布(normal distribution)又称高斯分布(gaussian distribution)

  • 若随机变量X服从一个数学期望值,标准方差是高斯分布;

  • 正态分布的期望值决定了数据分布位置,其标准方差决定了分布的幅度;

  • 因其曲线呈钟形,也称为钟形曲线;

绘制不同数据分布的分布类型图表

  1. 四种不同分布的数据,每个类别的数据总数分布为100个,

  • 图n的数据服从正态分布的数据(normal distribution,均值3,方差1);

  • 图S的数据位在类别n的数据的基础上右倾斜分布(skew-right distribution,Johnson分布的偏斜度2.0和峰度13);

  • 图S的数据位在类别n的数据的基础上尖峰态分布(leptikurtic distribution,Johnson分布的偏斜度2.2和峰度20);

  • 图mm为双峰分布(bimodal distribution:两个峰值的均值分别为2.05和3.95、方差都为0.31);

plotnine包的facet_grid()函数实现4种不同数据分布的按行展示;

结合geomhistogram()函数和geomdensity()函数可以分别实现统计直方图和核密度估计曲线图;

绘制统计直方图

import pandas as pd

from plotnine import *

df=pd.read_csv('d:\python\out\DistributionD.csv')

df['class']=df['class'].astype("category",["n", "s", "k", "mm"])

#统计直方图

histogram_plot=(ggplot(df,aes(x="value",fill="class"))

+geom_histogram(alpha=1,colour="black",bins=30,size=0.2)

+facet_grid('class~.')

+scale_fill_hue(s = 0.90, l = 0.65, h=0.0417,color_space='husl')

+theme_light()

+theme(aspect_ratio =0.25,

dpi=100,

figure_size=(3,4)))

print(histogram_plot)

histogram_plot.save("d:\python\out\histogram_plot.pdf")

核密度估计图

import pandas as pd

from plotnine import *

df=pd.read_csv('d:\python\out\DistributionD.csv')

df['class']=df['class'].astype("category", ["n", "s", "k", "mm"])

#核密度估计图

density_plot=(ggplot(df,aes(x="value",fill="class"))

+geom_density(alpha=1)

+facet_grid('class~.')

+scale_fill_hue(s = 0.90, l = 0.65, h=0.0417,color_space='husl')

+theme_light()

+theme(aspect_ratio =0.25,

dpi=100,

figure_size=(3,4)))

print(density_plot)

density_plot.save("d:\python\out\density_plot.pdf")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值