可视化库-Matplotlib-盒图(第四天)
生活随笔
收集整理的這篇文章主要介紹了
可视化库-Matplotlib-盒图(第四天)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
盒圖由五個(gè)數(shù)值點(diǎn)組成,最小觀測(cè)值,下四分位數(shù),中位數(shù),上四分位數(shù),最大觀測(cè)值
IQR = Q3 - Q1 Q3表示上四分位數(shù), Q1表示下四分位數(shù),IQR表示盒圖的長(zhǎng)度
最小觀測(cè)值 min =Q1 - 1.5*IQR
最大觀測(cè)值 max=Q3 + 1.5*IQR , 大于最大值或者小于最小值就是離群點(diǎn)
1. 畫(huà)出一個(gè)盒圖 plt.boxplot(tang_array, notch=False, sym='o', vert=True) # tang_array表示輸入的列表, notch表示盒圖的樣子,sym表示偏離值的表示方法, vert表示豎著,還是橫著
import matplotlib.pyplot as plt
import numpy as np
# 構(gòu)造正態(tài)分布的列表數(shù)組
tang_array = [np.random.normal(0, std, 100) for std in [0.1, 0.2, 0.3, 0.4]]
fig = plt.figure(figsize=(8, 6))
plt.boxplot(tang_array, notch=False, sym='o', vert=True)
plt.xticks([x+1 for x in range(len(tang_array))], ['x1', 'x2', 'x3', 'x4'])
plt.title('box plot')
plt.xlabel('x')
plt.show()
2 設(shè)置盒圖的線條顏色
import matplotlib.pyplot as plt
import numpy as np
# 構(gòu)造正態(tài)分布的列表數(shù)組
tang_array = [np.random.normal(0, std, 100) for std in [0.1, 0.2, 0.3, 0.4]]
fig = plt.figure(figsize=(8, 6))
bplt = plt.boxplot(tang_array, notch=False, sym='o', vert=True)
for compnent in bplt.keys():
for line in bplt[compnent]:
line.set_color('red')
plt.xticks([x+1 for x in range(len(tang_array))], ['x1', 'x2', 'x3', 'x4'])
plt.title('box plot')
plt.xlabel('x')
plt.show()
3.對(duì)盒圖進(jìn)行填充操作設(shè)置pacth_artist=True 對(duì)盒圖面進(jìn)行填充bplt['boxes'].set_facecolor('r')
tang_array = [np.random.uniform(0, std, 100) for std in [0.1, 0.2, 0.3, 0.4]]
bar_labels = ['x1', 'x2', 'x3', 'x4']
fig = plt.figure()
plt.xticks([x+1 for x in range(len(tang_array))], bar_labels)
bplt = plt.boxplot(tang_array, notch=False, sym='o', vert=True, patch_artist=True)
colors = ['pink', 'lightblue', 'lightgreen']
for pacthes, color in zip(bplt['boxes'], colors):
pacthes.set_facecolor(color)
plt.show()
4. 設(shè)置小提琴圖
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(12, 5))
tang_data = [np.random.normal(0, std, 100) for std in range(1, 4)]
axes[0].violinplot(tang_data, showmeans=False, showmedians=True)
axes[0].set_title('violin plot')
axes[1].boxplot(tang_data)
axes[1].set_title('box plot')
for ax in axes:
# 對(duì)y軸加上網(wǎng)格
ax.yaxis.grid(True)
ax.set_xticks([y+1 for y in range(len(tang_data))])
# 對(duì)每個(gè)圖加上x(chóng)ticks操作
plt.setp(axes, xticks=[y+1 for y in range(len(tang_data))], xticklabels=['x1', 'x2', 'x3'])
plt.show()
總結(jié)
以上是生活随笔為你收集整理的可视化库-Matplotlib-盒图(第四天)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 包书皮方法(书皮的包法教给大家)
- 下一篇: 上班迟到英语(在学校怎么用英语说)