zoukankan      html  css  js  c++  java
  • matplotlib模块

    matplotlib模块

    画图

    条形图

    import matplotlib.pyplot as plt # 默认支持英文,不支持中文
    from matplotlib.font_manager import FontProperties
    font = FontProperties(fname='D:msyh.ttc')
    classes = ['3班','4班','5班','6班']
    students = [50,60,55,67]
    
    ind = range(len(classes))
    
    # plt.bar(ind,students,color='r')
    # plt.xticks(ind,classes,fontproperties= font)
    # plt.show()
    plt.style.use('ggplot')
    plt.bar(ind,students,color='darkblue')
    plt.xticks(ind,classes,fontproperties= font)
    plt.xlabel('班级',fontproperties=font,fontsize=15)
    plt.ylabel('人数',fontproperties= font,fontsize=15)
    plt.title('班级-人数',fontproperties=font,fontsize=20)
    plt.show()
    

    直方图

    import matplotlib.pyplot as plt
    import numpy as np
    from matplotlib.font_manager import FontProperties
    # %matplotlib inline  # 如果没有这个,不会显示图片,有这个会显示图片(只针对jupyter)
    font = FontProperties(fname='D:msyh.ttc')
    mu1,mu2,sigma = 50,100,10
    x1 = mu1+sigma*np.random.randn(100000)  # 符合正太分布的随机数据
    x2 = mu2+sigma*np.random.randn(100000)
    # plt.hist(x1,bins=50)# 每50个数据一根柱子
    # plt.show()
    fig = plt.figure()
     
    ax1 = fig.add_subplot(1,2,1) # 一行2列 第一个
    ax1.hist(x1,bins=50,color='yellow')
    ax1.set_title('黄色的',fontproperties=font)
    
    ax2 = fig.add_subplot(1,2,2)  # 一行2列 第二个
    ax2.hist(x2,bins=100,color='green')
    ax2.set_title('绿色的',fontproperties=font)
    
    fig.suptitle('大标题',fontproperties=font,fontsize=20)
    
    plt.show()
    

    折线图

    import matplotlib.pyplot as plt
    import numpy as np
    from matplotlib.font_manager import FontProperties
    # %matplotlib inline  # 如果没有这个,不会显示图片,有这个会显示图片(只针对jupyter)
    font = FontProperties(fname='D:msyh.ttc')
    x1 = np.random.randn(1,40).cumsum()
    x2 = np.random.randn(1,40).cumsum()
    x3 = np.random.randn(1,40).cumsum()
    x4 = np.random.randn(1,40).cumsum()
    
    plt.plot(x1,marker='o',color='r',label='红线',linestyle='--')
    plt.plot(x2,marker='*',color='y',label='黄线',linestyle='-.')
    plt.plot(x3,marker='s',color='green',label='绿色',linestyle=':')
    plt.plot(x4,marker='x',color='b',label='蓝色',linestyle='-')
    
    
    plt.legend(prop=font)  # label的字体的需要在这里换
    plt.show()
    

    散点图加直线图

    import matplotlib.pyplot as plt
    import numpy as np
    from matplotlib.font_manager import FontProperties
    # %matplotlib inline  # 如果没有这个,不会显示图片,有这个会显示图片(只针对jupyter)
    font = FontProperties(fname='D:msyh.ttc')
    y = x1**2
    plt.scatter(x1,y,s=100)
    plt.show()
    
  • 相关阅读:
    Taro 自定义组件样式不生效及解决方案
    Taro 实现多行文本溢出省略效果
    taro路由传参this.$router.params获取不到值
    taro版本切换及报错解决
    fabricjs实现图片加备注文字并下载效果
    html2canvas实现DOM导出为图片(入门)
    [Vue warn]: Error in render: "TypeError: Cannot read property 'length' of null"
    svn识别不出修改的文件
    prefer-object-spread
    JAVA学习(一)——基本语法
  • 原文地址:https://www.cnblogs.com/aden668/p/11377959.html
Copyright © 2011-2022 走看看