import pandas as pd
import matplotlib.pyplot as plt
import pyodbc

# Setup connection to the database
conn_str = r"DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\Users\LIQI\Documents\microcapstrategy.accdb"
try:
    conn = pyodbc.connect(conn_str)
            
    # 执行查询
    query = "SELECT TradingDay, SecuCode, TurnoverRateRM, YearVolatilityByDay, Prc2bk FROM Portfolio"
    df = pd.read_sql(query, conn)
    # 关闭连接
finally:
    conn.close()
    
    # 将相关列转换为float类型
    df['TurnoverRateRM'] = df['TurnoverRateRM'].astype(float)
    df['YearVolatilityByDay'] = df['YearVolatilityByDay'].astype(float)
    df['Prc2bk'] = df['Prc2bk'].astype(float)
            
            
    # 统计不同tradingdate对应的每个stock ticker的因子值
    statistics = df.groupby('TradingDay').agg({'TurnoverRateRM': ['mean', 'median', lambda x: x.quantile(0.1), lambda x: x.quantile(0.25), \
                lambda x: x.quantile(0.75), lambda x: x.quantile(0.9)],'YearVolatilityByDay': ['mean', 'median', lambda x: x.quantile(0.1),\
                lambda x: x.quantile(0.25), lambda x: x.quantile(0.75), lambda x: x.quantile(0.9)],'Prc2bk': ['mean', 'median',\
                lambda x: x.quantile(0.1), lambda x: x.quantile(0.25), lambda x: x.quantile(0.75),lambda x: x.quantile(0.9)]})

    # 绘制时间序列折线图
    dates = statistics.index.tolist()

    plt.figure(figsize=(12, 6))

    # 绘制因子A的时间序列折线图
    plt.subplot(3, 1, 1)
    plt.plot(dates, statistics[('TurnoverRateRM', 'mean')], label='Mean')
    plt.plot(dates, statistics[('TurnoverRateRM', 'median')], label='Median')
    plt.plot(dates, statistics[('TurnoverRateRM', ''<lambda_0>'')], label='10% Quantile')
    plt.plot(dates, statistics[('TurnoverRateRM', ''<lambda_1>'')], label='25% Quantile')
    plt.plot(dates, statistics[('TurnoverRateRM', ''<lambda_2>'')], label='75% Quantile')
    plt.plot(dates, statistics[('TurnoverRateRM', ''<lambda_3>'')], label='90% Quantile')
    plt.ylabel('近月换手率')
    plt.legend()

    # 绘制因子B的时间序列折线图
    plt.subplot(3, 1, 2)
    plt.plot(dates, statistics[('YearVolatilityByDay', 'mean')], label='Mean')
    plt.plot(dates, statistics[('YearVolatilityByDay', 'median')], label='Median')
    plt.plot(dates, statistics[('YearVolatilityByDay', ''<lambda_0>'')], label='10% Quantile')
    plt.plot(dates, statistics[('YearVolatilityByDay', ''<lambda_1>'')], label='25% Quantile')
    plt.plot(dates, statistics[('YearVolatilityByDay', ''<lambda_2>'')], label='75% Quantile')
    plt.plot(dates, statistics[('YearVolatilityByDay', ''<lambda_3>'')], label='90% Quantile')
    plt.ylabel('年化波动率')
    plt.legend()

    # 绘制因子C的时间序列折线图
    plt.subplot(3, 1, 3)
    plt.plot(dates, statistics[('Prc2bk', 'mean')], label='Mean')
    plt.plot(dates, statistics[('Prc2bk', 'median')], label='Median')
    plt.plot(dates, statistics[('Prc2bk', ''<lambda_0>'')], label='10% Quantile')
    plt.plot(dates, statistics[('Prc2bk', ''<lambda_1>'')], label='25% Quantile')
    plt.plot(dates, statistics[('Prc2bk', ''<lambda_2>'')], label='75% Quantile')
    plt.plot(dates, statistics[('Prc2bk', ''<lambda_3>'')], label='90% Quantile')
    plt.ylabel('改良市净率')
    plt.legend()

    plt.xlabel('建仓日')
    plt.tight_layout()
    plt.show()   我写了一段代码 但是运行出错了 帮我看看怎么改 我把错误信息贴给你