直方圖-hist
matplotlib.pyplot.hist(
x, bins=10, range=None, normed=False,
weights=None, cumulative=False, bottom=None,
histtype=u'bar', align=u'mid', orientation=u'vertical',
rwidth=None, log=False, color=None, label=None, stacked=False,
hold=None, **kwargs)
x : (n,) array or sequence of (n,) arrays
這個參數是指定每個bin(箱子)分佈的數據,對應x軸
bins : integer or array_like, optional
這個參數指定bin(箱子)的個數,也就是總共有幾條條狀圖
normed : boolean, optional
這個參數指定密度,也就是每個條狀圖的佔比例比,默認爲1
color : 這個指定條狀圖的顏色
測試程序
import matplotlib.pyplot as plt
x = [1,1,2,3,3,3,4,4,4,4]
plt.hist(x)
plt.show()
橫座標爲x中值的範圍,縱座標(y)爲出現x中每個元素的個數
直方圖-bar
## X爲橫軸座標,y爲柱高度
x = range(5)
y = [22,33,44,12,78]
plt.bar(x,height=y)
plt.show()
## 橫向
'''
orientation 是豎直條還是水平條
height 條形的高度
width 線條的寬度
botton 條形的起始位置
'''
x = range(5)
y = [22,33,44,12,78]
plt.bar(x=0,bottom=x,width=y,height=0.5,orientation="horizontal")
plt.show()
餅圖-pie
import matplotlib.pyplot as plt
# 參數1 類別數量,label 類別標籤 ,explode 舉例圓心距離 autopct 佔比
plt.pie([10,10,10],labels=['1','2','3'],explode=(0.1,0,0),autopct='%1.1f%%')
plt.show()
箱線圖-boxplot
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame(np.random.rand(10,5),columns=['A','B','C','D','E'])
f = df.boxplot(sym = 'o', #異常點形狀
vert = True, # 是否垂直
whis=1.5, # IQR
patch_artist = True, # 上下四分位框是否填充
meanline = False,showmeans = True, # 是否有均值線及其形狀
showbox = True, # 是否顯示箱線
showfliers = True, #是否顯示異常值
notch = False, # 中間箱體是否缺口
return_type='dict') # 返回類型爲字典
plt.title('箱線圖')
plt.rcParams['font.sans-serif'] = 'SimHei' # 設置中文顯示
plt.show()