欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python 中的 plt.hist 函數(shù)基本用法詳解

 更新時(shí)間:2024年04月30日 11:15:21   作者:武帝為此  
plt.hist 函數(shù)用于繪制直方圖,直方圖是一種用來表示數(shù)據(jù)分布的圖形,它將數(shù)據(jù)分成若干個(gè)區(qū)間,然后統(tǒng)計(jì)每個(gè)區(qū)間中數(shù)據(jù)的數(shù)量,最終以柱狀圖的形式展示出來,這篇文章主要介紹了Python 中的 plt.hist 函數(shù)基本用法詳解,需要的朋友可以參考下

前言

plt.hist 函數(shù)用于繪制直方圖。直方圖是一種用來表示數(shù)據(jù)分布的圖形,它將數(shù)據(jù)分成若干個(gè)區(qū)間,然后統(tǒng)計(jì)每個(gè)區(qū)間中數(shù)據(jù)的數(shù)量,最終以柱狀圖的形式展示出來。

什么是直方圖?

直方圖主要用于可視化數(shù)據(jù)的分布情況。它將數(shù)據(jù)劃分為一系列的區(qū)間(也稱為箱子或柱子),然后計(jì)算每個(gè)區(qū)間內(nèi)數(shù)據(jù)點(diǎn)的數(shù)量。這些數(shù)量通常用柱狀圖表示,柱子的高度表示該區(qū)間內(nèi)數(shù)據(jù)點(diǎn)的數(shù)量。

plt.hist 函數(shù)的基本用法

import matplotlib.pyplot as plt
data = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]
plt.hist(data, bins=5)  # bins 參數(shù)表示要將數(shù)據(jù)分成多少個(gè)區(qū)間
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.title('Histogram Example')
plt.show()

在這個(gè)示例中,我們傳遞了一個(gè)名為 data 的數(shù)據(jù)列表給 plt.hist 函數(shù),并指定了 bins 參數(shù)為 5,表示將數(shù)據(jù)分成 5 個(gè)區(qū)間。使用 plt.xlabelplt.ylabelplt.title 函數(shù)來添加橫軸標(biāo)簽、縱軸標(biāo)簽和標(biāo)題。

plt.hist 的常用參數(shù)

  • bins:用于指定數(shù)據(jù)分成的區(qū)間數(shù)量,可以是一個(gè)整數(shù),也可以是一個(gè)區(qū)間列表。如果不指定該參數(shù),matplotlib 會(huì)自動(dòng)選擇合適的區(qū)間數(shù)量。
  • range:用于指定數(shù)據(jù)的取值范圍,以元組的形式傳遞,例如 (0, 10) 表示只考慮數(shù)據(jù)在 0 到 10 之間的部分。
  • density:如果設(shè)置為 True,則直方圖的面積將歸一化為 1,這樣可以將直方圖視為概率密度函數(shù)。
  • color:用于設(shè)置直方圖的顏色。
  • alpha:用于設(shè)置直方圖的透明度。
  • edgecolor:用于設(shè)置直方圖柱子的邊緣顏色。
  • cumulative:如果設(shè)置為 True,則繪制累積直方圖,顯示每個(gè)區(qū)間內(nèi)數(shù)據(jù)點(diǎn)的累積數(shù)量。

示例代碼

基本直方圖

import matplotlib.pyplot as plt
data = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]
plt.hist(data, bins=5, edgecolor='black')
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.title('Basic Histogram')
plt.show()

歸一化直方圖

import matplotlib.pyplot as plt
data = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]
plt.hist(data, bins=5, density=True, alpha=0.6, color='g', edgecolor='black')
plt.xlabel('Value')
plt.ylabel('Probability Density')
plt.title('Normalized Histogram')
plt.show()

累積直方圖

import matplotlib.pyplot as plt
data = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]
plt.hist(data, bins=5, cumulative=True, edgecolor='black', color='r')
plt.xlabel('Value')
plt.ylabel('Cumulative Frequency')
plt.title('Cumulative Histogram')
plt.show()

到此這篇關(guān)于Python 中的 plt.hist 函數(shù)詳解的文章就介紹到這了,更多相關(guān)Python plt.hist 函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論