• python将dataframe按需绘制折线图、柱状图、双坐标图


    import matplotlib.pyplot as plt
    
    # 设置中文显示
    plt.rcParams['font.sans-serif'] = ['SimHei']  # 指定默认字体
    plt.rcParams['axes.unicode_minus'] = False  # 解决负号显示问题
    
    • 1
    • 2
    • 3
    • 4
    • 5

    绘制TI折线图

    在这里插入图片描述

    def draw_TI(df_TI_night, output_TI_png, sector_id, terrain, label):
        """
        
        Args:
            df_TI_night: datafrmae
            output_TI_png: 图片保存路径
            sector_id: 1,2等,任意
            terrain: 地形
            label: day or night
    
        Returns:
    
        """
        # 绘制折线图
        plt.figure(figsize=(10, 6))
        # fig, ax = plt.subplots(figsize=(10, 6))
        colors = ['royalblue', 'red', 'limegreen', 'mediumpurple', 'cyan', 'darkorange']
        for i, category in enumerate(df_TI_night.columns[:6]):
            plt.plot(df_TI_night.index[:29], df_TI_night[category].values[:29], label=f'类别{i}', color=colors[i], marker='.')
    
        plt.xlabel('风速(m/s)', fontsize=11)
        plt.ylabel('TI值', fontsize=11)
        plt.title(f'区域{sector_id}-{terrain}不同类别TI分布({label})')
        plt.legend()
        # 设置x轴刻度间隔为2
        plt.xticks(df_TI_night.index[:29][::2])
        df_max = round(df_TI_night.iloc[:29, :].max().max(), 1)
        if df_max <= 0.3:
            ylim = 0.3
        else:
            ylim = df_max
        # 显示图形
        plt.ylim(0, ylim)
        plt.savefig(output_TI_png)  # 将图片保存为 output.png 文件
    
    • 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

    绘制

    def draw_day_night(normalized_table, output_day_night_png, sector_id, terrain):
        # 假设 normalized_table 是包含了每行的占比的 DataFrame
    
        normalized_table_T = normalized_table.T
        # 绘制折线图
        plt.figure(figsize=(10, 6))
        colors = ['c', 'k']  # 设定标签颜色,将'k'(黑色)用于night对应的类别
        for i, category in enumerate(normalized_table_T.index):
            plt.plot(normalized_table_T.columns, normalized_table_T.loc[category], label=category, color=colors[i],
                     marker='.')
    
        plt.xlabel('风廓线类别', fontsize=11)  # 在这里修改x轴标题
        plt.ylabel('占比(%)', fontsize=11)
        plt.title(f'区域{sector_id}({terrain})不同类别风廓线昼夜分布')
    
        # 显示标签
        for category in normalized_table_T.index:
            for i, value in enumerate(normalized_table_T.loc[category]):
                plt.text(i, value, f'{value:.2f}', ha='center', va='bottom', fontsize=10,
                         color=colors[normalized_table_T.index.get_loc(category)])
        # 显示图例
        plt.legend()
        # 设置y轴范围为10%
        plt.ylim(0, 100)
        plt.savefig(output_day_night_png)  # 将图片保存为 output.png 文件
        plt.close()
    
    • 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
  • 相关阅读:
    上传本地文件到github
    java计算机毕业设计宠物店管理系统MyBatis+系统+LW文档+源码+调试部署
    14.在SpringBoot框架集成MyBatis(mapper、mapperscan、xml和dao分离)
    JOSEF约瑟 10KV高压漏电保护继电器BLD-20 φ100mm 50-500mA 导轨安装
    k8s部署mysql报错‘/var/lib/mysql/‘: Operation not permitted
    Robot Framework移动端自动化测试----02简单的开始
    架构师必修设计模式——结构型模式
    Es6数组
    FRP进阶篇之安全认证
    大模型管理工具Ollama搭建及整合springboot
  • 原文地址:https://blog.csdn.net/weixin_46713695/article/details/132781937