- 关于matplotlib库中的add_subplot()函数的用法
- 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个子图,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个子图,4代表右下子图,以此类推(222)和(223)
plt.scatter(x,y)
plt.subplot(222)
plt.subplot(223)
plt.show()
-
plt.subplots()
fig,ax = plt.subplots() 等价于:
fig = plt.figure() ax = fig.add_subplot(1,1,1)
如果想要设置子图的宽度和高度可以在函数内加入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:在每个格子中显示相关系数数值
其他参数具体见: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()函数用法
对字段进行计数并排序(默认降序)
df[‘xxx’].value_counts(ascending=True) #升序
df[‘xxx’].value_counts(normalize=True) #计数占比
注:空值默认不计数的,value_counts()返回的结果是一个Series数组