python数分拾遗02

  • 关于matplotlib库中的add_subplot()函数的用法
  1. 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()

  1. 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.md

    import 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数组

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容