• python画图技巧——画完一个Excel如何很方便的换另一个Excel来画或者画多个Excel综合处理的数据,以及换线条样式和颜色


    在另一个py文件负责读取:

    df1=pd.read_excel('liu-last.xlsx')
    df2=pd.read_excel('chen-last.xlsx')
    
    • 1
    • 2

    可以有n多个df,
    画的时候。只需要调整这一个就可以了

    df3=read_excel.df3
    
    • 1

    线条颜色和样式

    color = ['#e6194B', '#3cb44b', '#ffe119', '#4363d8',
                 '#f58231', '#911eb4', '#42d4f4', '#f032e6',
                 '#bfef45', '#fabed4', '#469990', '#dcbeff',
                 '#9A6324', '#fffac8', '#800000', '#aaffc3',
                 '#808000', '#ffd8b1', '#000075', '#a9a9a9',
                 '#ffffff', '#000000']
    lineStyle = ['-', '--']
    #o代表 下面的linstyle对应的线条样式,设置0是第一个,1是第二个
    o = 0
    #color中的数字 代表 上面第几个0 红色,1 绿色,2黄色,3 蓝色
    plt.plot(x, y1, linewidth=1.8, c=color[3], linestyle=lineStyle[o])
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    最终代码

    读 Excel的代码:

    # -*- coding:utf-8 -*-
    
    
    import pandas as pd
    #解决数据输出时列名不对齐的问题
    pd.set_option('display.unicode.east_asian_width', True)
    df1=pd.read_excel('liu-last.xlsx')
    df2=pd.read_excel('chen-last.xlsx')
    
    #这个函数 直接返回上面的series对象,然后直接把series对象转列表用来画图
    def get_weiyizhi(df):
        #返回series,索引就是唯一值,值就是唯一值出现的次数
        count = df['编码'].value_counts()
        return count
    def get_total(df):
        #返回总列数,作为计算频率使用
        total = len(df)
        return total
    
    # for key,value in dict.items():
    #     print("类别:%-10s 次数:%-8s 比例:%.4s%%" % (key, value, '{:.2%}'.format(value/total)))
    
    
    
    def get_time(df):
        #返回第三列,也就是  时间对应的那一列
        min = df.iloc[0:668, 3]
        return min
    def get_code(df):
        #返回第一列,也就是编码对应的那一列
        code=df.iloc[0:668,1]
        return  code
    #根据指定条件来筛选Excel的值,支持 & 等逻辑组合
    df3=df2.loc[(df2['编码']<10)]
    print(df3)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    画图的代码

    # -*- coding:utf-8 -*-
    #上面那句话 不加 容易报错
    import matplotlib.pyplot as plt
    import read_excel
    df3=read_excel.df3
    
    x = read_excel.get_time(df3)
    y1 = read_excel.get_code(df3)
    y2 = [24, 97, 202, 225]
    y3 = [110, 138, 177, 205]
    y4 = [95, 68, 83, 105]
    y5 = [72, 74, 76, 67]
    plt.title('编码列表时间序列图')  # 折线图标题
    plt.rcParams['font.sans-serif'] = ['SimHei']  # 显示汉字
    plt.xlabel('时间/分钟')  # x轴标题
    plt.ylabel('编码类别')  # y轴标题
    
    color = ['#e6194B', '#3cb44b', '#ffe119', '#4363d8',
                 '#f58231', '#911eb4', '#42d4f4', '#f032e6',
                 '#bfef45', '#fabed4', '#469990', '#dcbeff',
                 '#9A6324', '#fffac8', '#800000', '#aaffc3',
                 '#808000', '#ffd8b1', '#000075', '#a9a9a9',
                 '#ffffff', '#000000']
    lineStyle = ['-', '--']
    #o代表 下面的linstyle对应的线条样式,设置0是第一个,1是第二个
    o = 0
    #color中的数字 代表 上面第几个0 红色,1 绿色,2黄色,3 蓝色
    plt.plot(x, y1, linewidth=1.8, c=color[3], linestyle=lineStyle[o])
    #plt.plot(x, y1, marker='o', markersize=1)  # 绘制折线图,添加数据点,设置点的大小
    # plt.plot(x, y2, marker='o', markersize=3)
    # plt.plot(x, y3, marker='o', markersize=3)
    # plt.plot(x, y4, marker='o', markersize=3)
    # plt.plot(x, y5, marker='o', markersize=3)
    #设置是否显示折线图的每个点对应的数值的,我不想显示,这里注释掉
    # for a, b in zip(x, y1):
    #     plt.text(a, b, b, ha='center', va='bottom', fontsize=10)  # 设置数据标签位置及大小
    # for a, b in zip(x, y2):
    #     plt.text(a, b, b, ha='center', va='bottom', fontsize=10)
    # for a, b in zip(x, y3):
    #     plt.text(a, b, b, ha='center', va='bottom', fontsize=10)
    # for a, b in zip(x, y4):
    #     plt.text(a, b, b, ha='center', va='bottom', fontsize=10)
    # for a, b in zip(x, y5):
    #     plt.text(a, b, b, ha='center', va='bottom', fontsize=10)
    plt.legend(['陈老师'])
    #plt.legend(['方案一', '方案二', '方案三', '方案四', '方案五'])  # 设置折线名称
    #dpi代表:Dots Per Inch,每英寸点数,sci论文要求300或600
    plt.savefig('zhexiantu.png',dpi=600)
    plt.show()  # 显示折线图
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
  • 相关阅读:
    msssql子查询列传行(stuff() 与for xml path())
    10.3 校招 实习 内推 面经
    49 多个 classloader 加载的同类限定名的Class 在 jhat 中显示不全
    【有源码】基于uniapp的农场管理小程序springboot基于微信小程序的农场检测系统(源码 调试 lw 开题报告ppt)
    【C++笔记】第三篇 关键字和标识符
    零基础自学黑客【网络安全】啃完这些足够了
    ORB-SLAM2 ---- Tracking::TrackWithMotionModel函数
    Vulnhub实战-DC9
    Ubuntu16.04配置NTP时间同步
    vue elementui的select组件实现滑到底部分页请求后端接口
  • 原文地址:https://blog.csdn.net/qq_41517071/article/details/128136830