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

import matplotlib.pyplot as plt

# 设置中文显示
plt.rcParams['font.sans-serif'] = ['SimHei']  # 指定默认字体
plt.rcParams['axes.unicode_minus'] = False  # 解决负号显示问题

绘制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 文件

绘制

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()
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值