【数据分析】描述性统计分析 - 直方图

一、什么是直方图

  • 由一批长方形构成,通过长方形的面积或高度来代表对应组在数据中所占的比例。
  • 用长方形的面积代表对应组的频数与组距的比时,则称为频率分布直方图;
  • 当用长方形的高代表对应组的频数时,则称为频数分布直方图
  • 但严格统计意义上的直方图都是指频率分布直方图 ,而且统计意义上的直方图没有纵向刻度

二、绘制直方图

三、Python 绘制直方图

1、使用 pandasnumpymatplotlib绘制直方图

python 复制代码
# Using pandas, numpy, matplotlib to draw histograms
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib

# set the backend of matplotlib to TkAgg
matplotlib.use('TkAgg')

data = pd.DataFrame({
    # Generate 1000 random numbers with a standard normal distribution
    'Values': pd.Series(np.random.randn(1000))
})

# Draw a histogram
data['Values'].plot.hist(bins=30, alpha=0.5, color='lightblue',
                        edgecolor='darkblue')


# set chart title and axis labels
plt.title('Histogram of Values')
plt.xlabel('Value')
plt.ylabel('Frequency')

# display chart
plt.show()

2、使用 pandasnumpymatplotlib绘制直方图

python 复制代码
# Using plotly.express to draw histograms
import plotly.express as px
import pandas as pd
import numpy as np
# data = [1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]

data = pd.DataFrame({
    'Values': pd.Series(np.random.randn(1000))
})
# Nbins defines the number of boxes in the histogram
fig = px.histogram(data, nbins=80,
                   color_discrete_sequence=['blue'],
                   labels={'Values': 'Value'},
                   title='Histogram of Values',
                   # marginal="box"
                   )

fig.update_traces(
    marker=dict(
        color='lightblue',
        opacity=0.75,
        line=dict(
            color='darkblue',
            width=1
        )
    )
)

# display chart
fig.show()

3、使用 pandasnumpydash****和 plotly.express绘制直方图

python 复制代码
# Using dash, plotly.express to draw histogram
import dash
import numpy as np
import pandas as pd
from dash import html, dcc
import plotly.express as px

# create a dash application
app = dash.Dash(__name__)

# create as list
# data = [1, 2, 2, 2, 2, 3, 4, 5, 5, 5, 6, 7, 8, 8, 8, 9, 9, 10, 10, 10, 11, 12, 12, 12, 13, 14, 12, 15, 15, 16, 17, 18, 18, 18, 19, 19, 20, 20, 20]
data = pd.DataFrame({
    'Values': pd.Series(np.random.randn(1000))
})

# use plotly.express to draw histogram
fig = px.histogram(data, nbins=60,
                    color_discrete_sequence=['blue'],
                    labels={'Values': 'Value'},
                    title='Histogram of Values',
                    # marginal="box",
                    )

fig.update_traces(
    marker=dict(
        color='lightblue',
        opacity=0.75,
        line=dict(
            color='darkblue',
            width=1
        )
    )
)

# define application layout
app.layout = html.Div([
    html.H1('Dash Histogram Example'),
    dcc.Graph(figure=fig)
])

# run the application
if __name__ == '__main__':
    app.run_server(debug=True)
相关推荐
SHUIPING_YANG12 分钟前
如何让dify分类器更加精准的分类?
人工智能·分类·数据挖掘
亚马逊云开发者1 小时前
基于 Amazon Q Developer+Remote MCP 访问 Amazon Redshift
数据分析
程序员白话2 小时前
使用kube-prometheus在K8s集群快速部署Prometheus+Grafana
后端·数据可视化
Christo311 小时前
TFS-2018《On the convergence of the sparse possibilistic c-means algorithm》
人工智能·算法·机器学习·数据挖掘
用户Taobaoapi201416 小时前
京东店铺所有商品API技术开发文档
大数据·数据挖掘·数据分析
总有刁民想爱朕ha16 小时前
车牌模拟生成器:Python3.8+Opencv代码实现与商业应用前景(C#、python 开发包SDK)
开发语言·python·数据挖掘
Stestack18 小时前
人工智能常见分类
人工智能·分类·数据挖掘
alsmile20 小时前
什么是组态?不会代码也能搭出工业大屏和数字可视化
低代码·数据可视化
IT研究室21 小时前
大数据毕业设计选题推荐-基于大数据的国家药品采集药品数据可视化分析系统-Spark-Hadoop-Bigdata
大数据·hadoop·信息可视化·spark·毕业设计·数据可视化·bigdata
华科云商xiao徐1 天前
告别IP被封!分布式爬虫的“隐身”与“分身”术
爬虫·数据挖掘·数据分析