- 關(guān)于matplotlib庫(kù)中的add_subplot()函數(shù)的用法
- plt.subplot()
import matplotlib.pyplot as plt
x = [1,2,3,4]
y = [2,4,6,8]
plt.figure()
plt.subplot(221) #22代表2 x 2個(gè)子圖,1代表左上子圖椎侠,也可寫成plt.subplot(2,2,1),更完整的寫法是plt.subplot(nrows=2, ncols=2, index=1)
plt.plot(x,y)
plt.subplot(224)#22代表2 x 2個(gè)子圖慎宾,4代表右下子圖浅悉,以此類推(222)和(223)
plt.scatter(x,y)
plt.subplot(222)
plt.subplot(223)
plt.show()
-
plt.subplots()
fig,ax = plt.subplots() 等價(jià)于:
fig = plt.figure() ax = fig.add_subplot(1,1,1)
如果想要設(shè)置子圖的寬度和高度可以在函數(shù)內(nèi)加入figsize值
fig, ax = plt.subplots(1,3,figsize=(15,7))
可通過ax控制子圖:
fig, ax = plt.subplots(2,3) ax[0,1].plot(x,y) #第一行第二列的子圖 ax[1,1].scatter(x,y) #第二行第二列的子圖
-
熱力圖(heatmap)
sns.heatmap(df.corr(), annot=True, linewidths=0.05, fmt= '.2f',cmap="magma") plt.show()
annot=True:在每個(gè)格子中顯示相關(guān)系數(shù)數(shù)值
其他參數(shù)具體見:https://www.cntofu.com/book/172/docs/30.mdimport matplotlib.pyplot as plt import seaborn as sns import pandas as pd df = pd.read_csv('D:/Womens Clothing E-Commerce Reviews.csv') sns.heatmap(df.corr(),annot = True,fmt = '.2f') plt.show()
df[‘xxx’].value_counts()函數(shù)用法
對(duì)字段進(jìn)行計(jì)數(shù)并排序(默認(rèn)降序)
df[‘xxx’].value_counts(ascending=True) #升序
df[‘xxx’].value_counts(normalize=True) #計(jì)數(shù)占比
注:空值默認(rèn)不計(jì)數(shù)的术健,value_counts()返回的結(jié)果是一個(gè)Series數(shù)組