zoukankan      html  css  js  c++  java
  • matplotlib之折线图

    1、案例一

    # coding=utf-8
    from matplotlib import pyplot as plt
    import random
    
    # 设置字体相关
    from matplotlib import font_manager
    my_font = font_manager.FontProperties(fname="D:\study\python-数据分析\阿里汉仪智能黑体.ttf")
    
    y = [1, 0, 1, 1, 2, 4, 3, 2, 3, 4, 4, 5, 6, 5, 4, 3, 3, 1, 1, 1]
    x = range(11,31)
    
    # 设置图形大小
    plt.figure(figsize=(20, 8), dpi=80)
    
    # 绘制图形 plt.plot(x, y)
    # 设置x轴刻度 _xtick_labels = ["{}岁".format(i) for i in x] plt.xticks(x, _xtick_labels,fontproperties=my_font) plt.yticks(range(0, 9)) # 标注最大值 plt.annotate('最大值', xy=(23, 6), xytext=(23, 7), arrowprops=dict(facecolor='black', shrink=0.05),fontproperties=my_font ) # 标注最小值 plt.annotate('最小值', xy=(12, 0), xytext=(12, 1), arrowprops=dict(facecolor='black', shrink=0.05),fontproperties=my_font ) # 绘制网格 alpha网格透明度 plt.grid(alpha=0.5) # 展示 plt.show()

    2、案例二

    # coding=utf-8
    from matplotlib import pyplot as plt
    import random
    
    # 设置字体相关
    from matplotlib import font_manager
    my_font = font_manager.FontProperties(fname="D:\study\python-数据分析\阿里汉仪智能黑体.ttf")
    
    x = range(0, 120)
    y = [random.randint(20, 35) for i in range(120)]
    
    plt.figure(figsize=(20, 8), dpi=80)
    # 调整x轴的刻度
    _x = list(x)
    _xtick_labels = ["10点{}分".format(i) for i in range(60)]
    _xtick_labels += ["11点{}分".format(i) for i in range(60)]
    
    # 取步长,数字和字符串一一对应,数据长度一样
    # rotation旋转度数
    # fontproperties设置字体
    plt.xticks(_x[::3], _xtick_labels[::3], rotation=45, fontproperties=my_font)
    plt.plot(x, y)
    
    # 添加描述信息
    plt.xlabel("时间", fontproperties=my_font)
    plt.ylabel("温度 单位(°C)", fontproperties=my_font)
    
    # 添加标题
    plt.title("气温变化图", fontproperties=my_font)
    plt.show()

    3、案例三

    # coding=utf-8
    '''
     绘制折线图Demo
    '''
    from matplotlib import pyplot as plt
    
    # x轴(axis)为24小时
    x = range(0, 24, 2)
    # y轴为13个数据
    y = [15, 13, 5, 17, 20, 25, 26, 26, 24, 22, 18, 15]
    
    # 设置图片大小
    plt.figure(figsize=(20, 8), dpi=80)
    
    # 保存图像
    # plt.savefig("./气候图.png")
    
    # 设置x轴的刻度
    # plt.xticks(x)
    plt.xticks(range(0, 24, 2))
    
    # xtick_lables = [i/2 for i in range(0, 49)]
    # plt.xticks(_xtick_lables[::3])
    
    # 设置y轴的刻度
    plt.yticks(range(min(y), max(y), 2))
    # 绘图
    plt.plot(x, y)
    # 展示图像
    plt.show()

    4、案例四---绘制多个直线图

    # coding=utf-8
    from matplotlib import pyplot as plt
    import random
    
    # 设置字体相关
    from matplotlib import font_manager
    my_font = font_manager.FontProperties(fname="D:\study\python-数据分析\阿里汉仪智能黑体.ttf")
    
    y_1 = [1,0,1,1,2,4,3,2,3,4,4,5,6,5,4,3,3,1,1,1]
    y_2 = [1,0,3,1,2,2,3,3,2,1 ,2,1,1,1,1,1,1,1,1,1]
    
    x = range(11,31)
    
    #设置图形大小
    plt.figure(figsize=(20,8),dpi=80)
    
    plt.plot(x,y_1,label="自己",color="#F08080")
    plt.plot(x,y_2,label="同桌",color="#DB7093",linestyle="--")
    
    #设置x轴刻度
    _xtick_labels = ["{}岁".format(i) for i in x]
    plt.xticks(x,_xtick_labels,fontproperties=my_font)
    # plt.yticks(range(0,9))
    
    #绘制网格
    plt.grid(alpha=0.4,linestyle=':')
    
    #添加图例
    plt.legend(prop=my_font,loc="upper left")
    
    plt.grid(alpha=0.5)
    #展示
    plt.show()
  • 相关阅读:
    Redis下载地址
    form序列化
    隐藏GET请求URL参数
    IntelliJ IDEA中显示方法说明快键键
    Myeclipse2014在线安装SVN插件
    Spring Cloud Gateway 集成 Sentinel 网关限流(2)
    Spring Cloud Gateway 集成 Sentinel 网关限流(1)
    Spring Cloud Gateway 集成 Nacos 实现请求负载
    微服务网关之Spring Cloud Gateway
    Nacos 的安装
  • 原文地址:https://www.cnblogs.com/ywjfx/p/10820977.html
Copyright © 2011-2022 走看看