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

    1、基本要点

    # 导入模块
    from matplotlib import pyplot as plt
    
    # x轴数据
    x = range(2, 26, 2)
    # y轴数据
    y = [15, 13, 14.5, 17, 20, 25, 26, 26, 27, 22, 18, 15]
    
    # 绘图
    plt.plot(x, y)
    
    # 展示图片
    plt.show()

    注意: x轴和y轴是可迭代对象,x轴和y轴的数值必须一一对应, x和y中的值必须是数子

    2、设置图行大小

    plt.figure(figsize=(宽, 高), dpi=80)
    # 注意:设置图形大小,要在绘图的前面

    3、保存图片

    plt.savefig('./名称.png')
    # 注意: 
    # 保存图片要在,绘图之后
    # 可以保存为矢量图(svg)

    4、调整x轴或y轴上的刻度

    plt.xticks(可迭代对象)
    plt.yticks(可迭代对象)
    # 注意:
    # 调整x轴或y轴的刻度,要放在保存或展示图片的前面

    例子

    # 导入模块
    from matplotlib import pyplot as plt
    
    # x轴数据
    x = range(2, 26, 2)
    # y轴数据
    y = [15, 13, 14.5, 17, 20, 25, 26, 26, 27, 22, 18, 15]
    
    # 设置图形大小
    plt.figure(figsize=(20, 8), dpi=80)
    
    # 绘图
    plt.plot(x, y)
    
    # 调整x轴的刻度
    plt.xticks(x)
    # 调整y轴的刻度
    plt.yticks(range(min(y), max(y) + 1))
    
    # 保存图片
    # plt.savefig('./test.svg')
    # 展示图片
    plt.show()

    5、调整x轴或y轴的刻度并显示标签

    plt.xticks(ticks, labels, rotation=45)
    # 注意
    # ticks和labels的数据类型最好是列表,方便调整刻度
    # ticks和labels即数字和字符串要一一对应,步长也要一致
    # rotation调整标签的旋转

    例子

    import random
    from matplotlib import pyplot as plt
    
    y = [random.randint(20, 35) for i in range(120)]
    
    x = range(120)
    
    # 设置图形大小
    plt.figure(figsize=(20, 8), dpi=80)
    
    # 绘图
    plt.plot(x, y)
    
    # 设置x轴刻度
    x = list(x)
    x_labels = ['10H:{}M'.format(i) for i in range(60)]
    x_labels += ['11H:{}M'.format(i) for i in range(60)]
    plt.xticks(x[::3], x_labels[::3], rotation=45)
    # 设置y轴刻度
    plt.yticks(range(min(y), max(y) + 1))
    
    # 展示图片
    plt.show()

    6、显示中文

    a、查看字体

    linux/mac查看支持的字体
    fc-list     # 查看支持的字体
    fc-list :lang=Zhang     # 查看支持的中文(冒号前面加空格)
    
    windows查看支持的字体
    Win+R, 输入fonts
    获取字体路径:
    推荐使用git bash
    查看字体
    cd 'C:WindowsFonts'
    ls -l 
    一般使用  msyh.ttc

    b、设置

    修改matplotlib的默认字体
    1.matplotlib.rc,具体看源码(windows/linux),不是百分百成功
    2.matplotlib下的font_manager(windwos/linux/mac),百分百成功

    # 导入模块
    from matplotlib import font_manager
    # 设置显示中文
    my_font = font_manager.FontProperties(fname='C:WindowsFontsmsyh.ttc')
    # 添加fontproperties=my_font的属性
    plt.xticks(x[::3], x_labels[::3], rotation=45, fontproperties=my_font)
    
    # 注意: 设置中文显示时
    # 只有plt.legend, 使用prop
    # 其它使用fontproperties

    例子

    import random
    from matplotlib import pyplot as plt
    from matplotlib import font_manager
    
    
    # 设置显示中文
    my_font = font_manager.FontProperties(fname='C:WindowsFontsmsyh.ttc')
    
    y = [random.randint(20, 35) for i in range(120)]
    
    x = range(120)
    
    # 设置图形大小
    plt.figure(figsize=(20, 8), dpi=80)
    
    # 绘图
    plt.plot(x, y)
    
    
    # 设置x轴刻度,显示标签(中文)
    x = list(x)
    x_labels = ['10时:{}分'.format(i) for i in range(60)]
    x_labels += ['11时:{}分'.format(i) for i in range(60)]
    plt.xticks(x[::3], x_labels[::3], rotation=45, fontproperties=my_font)
    # 设置y轴刻度
    plt.yticks(range(min(y), max(y) + 1))
    
    # 展示图片
    plt.show()

    7、设置描述信息

    # 1. 设置x轴的label
    plt.xlabel()
    # 2. 设置y轴的label
    plt.ylabel()
    # 3. 设置title
    plt.title()
    
    # 注意
    # 设置描述信息要在保存、展示的前面
    # 显示中文用fontproperties

    例子

    # 设置中文字体
    my_font = font_manager.FontProperties(fname='C:WindowsFontsmsyh.ttc')
    # 描述信息
    plt.xlabel('时间', fontproperties=my_font)
    plt.ylabel('温度 单位(℃)', fontproperties=my_font)
    plt.title('10点到12点每分钟的温度变化', fontproperties=my_font)

    8、添加网格

    plt.grid(alpha=0.5)
    # alpha网格的alpha值(0~1)
    # 在展示图片和保存图片前

    9、添加图例

    # 中文显示
    my_font = font_manager.FontProperties(fname='C:WindowsFontsmsyh.ttc')
    # 绘图
    plt.plot(x, y, label='该折线的描述名称')
    # 添加图例
    plt.legend(prop=my_font, loc='')
    
    # 注意: loc参数设置图例的位置,具体看源码,一般默认即可
    #   best
    #   upper right
    #   upper left
    #   lower left
    #   lower right
    #   right
    #   center left
    #   center right
    #   lower center
    #   upper center
    #   center

    10、自定义绘图的风格

    plt.plot(color='', linestyle='', linewidth= , alpha= )
    # color 线条颜色
        # 16进制的颜色代码
    # linestyle 线条风格
        # - 实线
        # -- 虚线
        # -. 点划线
        # : 点虚线
    # linewidth 线条粗细
    # alpha 透明度
    # 注意:这些风格对网格(grid())同样有效 

    例子

    # -*- encoding: utf-8 -*-
    
    from matplotlib import pyplot as plt
    from matplotlib import font_manager
    
    #
    # 设置中文字体
    my_font = font_manager.FontProperties(fname='C:WindowsFontsmsyh.ttc')
    
    y1 = [1, 0, 1, 1, 2, 4, 3, 2, 3, 4, 4, 5, 6, 5, 4, 3, 3, 1, 1, 1]
    y2 = [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, y1, label='自己', linestyle=':')
    plt.plot(x, y2, label='敌人', color='#00FF00')
    
    # 设置x轴y刻度
    x = list(x)
    x_labels = ['{}岁'.format(i) for i in x]
    plt.xticks(x, x_labels, fontproperties=my_font)
    plt.yticks(range(0, 7))
    # 设置描述信息
    plt.xlabel('年龄', fontproperties=my_font)
    plt.ylabel('人数', fontproperties=my_font)
    plt.title('11岁到30岁每年交的女朋友', fontproperties=my_font)
    
    # 绘制网格
    plt.grid(alpha=0.8, linestyle=':', color='#DC143C')
    
    # 添加图例
    plt.legend(prop=my_font)
    
    plt.show()
  • 相关阅读:
    android性能优化之布局优化
    android性能调优之traceview的使用
    android性能优化
    EditText光标居上
    ExecutorService的submit(Runnable x)和execute(Runnable x) 两个方法的本质区别
    android 静默安装
    android 内存优化
    image-webpack-loader在mac或ubuntu报错
    git重命名文件和文件夹
    js判断设备类型
  • 原文地址:https://www.cnblogs.com/wt7018/p/11940892.html
Copyright © 2011-2022 走看看