Python數(shù)據(jù)可視化(四):頻率直方圖繪制

使用seaborn包繪制直方圖

# 導(dǎo)入所需的python包
# libraries & dataset
import seaborn as sns
import matplotlib.pyplot as plt

# set a grey background (use sns.set_theme() if seaborn version 0.11.0 or above) 
sns.set(style="darkgrid")
# 加載示例數(shù)據(jù)
df = sns.load_dataset("iris")

df.head()
sepal_length sepal_width petal_length petal_width species
0 5.1 3.5 1.4 0.2 setosa
1 4.9 3.0 1.4 0.2 setosa
2 4.7 3.2 1.3 0.2 setosa
3 4.6 3.1 1.5 0.2 setosa
4 5.0 3.6 1.4 0.2 setosa
# 使用distplot函數(shù)繪制基礎(chǔ)直方圖
sns.distplot(df["sepal_length"],hist=True, kde=False)
plt.show()
image.png
# 設(shè)置kde=True添加密度曲線
sns.distplot(df["sepal_length"],hist=True, kde=True)
plt.show()
image.png
# 設(shè)置bins參數(shù)更改bin的個數(shù)
sns.distplot(df["sepal_length"],hist=True, kde=True, bins=30)
plt.show()
image.png
# 設(shè)置rug=True參數(shù)添加軸須線
sns.distplot(df["sepal_length"], kde=True, rug=True)
# in the next version of the distplot function, one would have to write:
# sns.distplot(data=df, x="sepal_length", kde=True, rug=True) # note that 'kind' is 'hist' by default
plt.show()
image.png
# 自定義軸須線的顏色和寬度
sns.distplot(df["sepal_length"],
             kde=True,
             rug=True,
             rug_kws={"color": "r", "alpha": 0.3, "linewidth": 2, "height":0.2})
# in the next version of the distplot function, one would have to write:
# sns.distplot(data=df, x="sepal_length", kde=True, rug=True, rug_kws={"color": "r", "alpha":0.3, "linewidth": 2, "height":0.2 })
plt.show()
image.png
# 自定義密度曲線的顏色和寬度
sns.distplot(df["sepal_length"],
             kde=True,
             kde_kws={"color": "g", "alpha": 0.3, "linewidth": 5, "shade": True})
# in the next version of the distplot function, one would have to write:
# sns.distplot(data=df, x="sepal_length", kde=True, kde_kws={"color": "g", "alpha": 0.3, "linewidth": 5, "shade": True})
plt.show()
image.png
# 繪制多個變量的直方圖
sns.distplot(df["sepal_length"], color="skyblue", label="Sepal Length", kde=True)
sns.distplot(df["sepal_width"], color="red", label="Sepal Width", kde=True)

plt.legend() 
plt.show()
image.png
# 分面展示多個直方圖
# 設(shè)置畫板
fig, axs = plt.subplots(2, 2, figsize=(7, 7))

# 分別繪制多個直方圖
sns.distplot(df["sepal_length"], kde=True, color="skyblue", ax=axs[0, 0])
sns.distplot(df["sepal_width"], kde=True, color="olive", ax=axs[0, 1])
sns.distplot(df["petal_length"], kde=True, color="gold", ax=axs[1, 0])
sns.distplot(df["petal_width"], kde=True, color="teal", ax=axs[1, 1])

plt.show()
image.png
# 使用jointplot函數(shù)繪制邊際直方圖

# Custom the inside plot: options are: “scatter” | “reg” | “resid” | “kde” | “hex”
sns.jointplot(x=df["sepal_length"], y=df["sepal_width"], kind='scatter')
sns.jointplot(x=df["sepal_length"], y=df["sepal_width"], kind='hex')
sns.jointplot(x=df["sepal_length"], y=df["sepal_width"], kind='kde')

plt.show()
image.png
image.png
image.png
# 自定義顏色
# Then you can pass arguments to each type:
sns.jointplot(x=df["sepal_length"], y=df["sepal_width"], kind='scatter', s=200, color='m', edgecolor="skyblue", linewidth=2)

# Custom the color
sns.set(style="white", color_codes=True)
sns.jointplot(x=df["sepal_length"], y=df["sepal_width"], kind='kde', color="skyblue")

plt.show()
image.png
image.png
# 自定義直方圖的bins數(shù)
# Custom the histogram:
sns.jointplot(x=df["sepal_length"], y=df["sepal_width"], kind='hex', marginal_kws=dict(bins=30, color="r"))

plt.show()
image.png
# 添加箱線圖

# creating a figure composed of two matplotlib.Axes objects (ax_box and ax_hist)
f, (ax_box, ax_hist) = plt.subplots(2, sharex=True, gridspec_kw={"height_ratios": (.15, .85)})

# assigning a graph to each ax
sns.boxplot(df["sepal_length"], ax=ax_box)
sns.distplot(df.sepal_length, ax=ax_hist)

# Remove x axis name for the boxplot
ax_box.set(xlabel='')
plt.show()
image.png

參考來源:https://www.python-graph-gallery.com/histogram/

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容