Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

if industry is not None: industrydata = portfoliodf[portfoliodf['Industry I'] ==…

  1. User

    if industry is not None: industry_data = portfolio_df[portfolio_df['Industry I'] == industry] x_data = industry_data['TRADEDATE'].to_numpy() y1_data = industry_data['excess_return_compound'].to_numpy() y2_data = industry_data['allocation_effect_compound'].to_numpy() y3_data = industry_data['selection_effect_compound'].to_numpy() #y4_data = industry_data['interaction_effect_compound'].to_numpy() y4_data = industry_data['Weight Difference'].to_numpy()

            # 创建一个图形和坐标轴
            plt.figure(figsize=(15, 9))
            fig, ax = plt.subplots()
            #fig, ax1 = plt.subplots()
            
            # 绘制折线图
            ax.plot(x_data, y1_data, color='blue', marker='o', label='excess_return_compound')
            ax.plot(x_data, y2_data, color='red', marker='o', label='allocation_effect_compound')
            ax.plot(x_data, y3_data, color='black', marker='o', label='selection_effect_compoundt')
            #ax.plot(x_data, y4_data, color='purple', marker='o', label='interaction_effect_compound')
            
            ax.set_xlabel('TRADEDATE')
            ax.set_ylabel('Excess Return')
            
            # 绘制柱形图
            ax2 = ax.twinx()
            ax2.bar(x_data, y4_data, color='purple', alpha=0.5, label='Weight Diff')
            ax2.set_ylabel('Weight Diff') 
            
            plt.xticks(rotation=45)
            plt.title(f"Excess Return in {industry}")
            #plt.xlabel('TRADEDATE')
            #plt.ylabel('excess_return_compound')
            plt.legend()
            # Show the plot
            left = sht2.range('G3').left
            top = sht2.range('G3').top
            sht2.pictures.add(fig, name='Industry', update=True, left=left, top=top)  我写了一段作图的代码 现在默认的折线图上会给每个数据点配以图例 我想隐藏掉这些图例 只显示光滑的曲线 怎么操作?
    
  2. Assistant

    要隐藏掉图例,可以在绘制折线图时添加参数label='nolegend',示例如下:

    ax.plot(x_data, y1_data, color='blue', marker='o', label='_nolegend_')
    

    这样就会隐藏图例,并且只显示光滑的曲线。

  3. User

    应该是marker这个参数改一下吧?跟label没关系 label是标签啊

  4. Assistant

    非常抱歉,我之前的回答有误。要隐藏图例,你需要将plot函数中的label参数设为一个空字符串或者去掉该参数。示例如下:

    ax.plot(x_data, y1_data, color='blue', marker='o')
    

    这样就会隐藏图例,并且只显示光滑的曲线。

    另外,如果你想将折线图和柱形图合并在一张图中,可以按照下面的方式修改代码:

    fig, ax = plt.subplots()
    ax2 = ax.twinx()
    
    # 绘制折线图
    ax.plot(x_data, y1_data, color='blue', marker='o', label='_nolegend_')
    ax.plot(x_data, y2_data, color='red', marker='o', label='_nolegend_')
    ax.plot(x_data, y3_data, color='black', marker='o', label='_nolegend_')
    
    # 绘制柱形图
    ax2.bar(x_data, y4_data, color='purple', alpha=0.5)
    
    # 设置图例
    ax.legend(['excess_return_compound', 'allocation_effect_compound', 'selection_effect_compound'], loc='upper left')
    ax2.legend(['Weight Diff'], loc='upper right')
    
    # 设置坐标轴标签等
    ax.set_xlabel('TRADEDATE')
    ax.set_ylabel('Excess Return')
    ax2.set_ylabel('Weight Diff')
    
    plt.xticks(rotation=45)
    plt.title(f"Excess Return in {industry}")
    
    # 显示图形
    left = sht2.range('G3').left
    top = sht2.range('G3').top
    sht2.pictures.add(fig, name='Industry', update=True, left=left, top=top)
    

    这样,图例会分别显示在左上和右上角,折线图和柱形图的图例都会显示。

  5. User

    我现在的代码就是折现与柱状图叠合在一起的呀。你可能理解错了 我是希望折线图上不出现数据序列的标记点。 不是要把label拿掉

  6. Assistant

    非常抱歉,我再次理解错误。要隐藏折线图上的数据序列标记点,可以在plot函数中添加参数marker='',示例如下:

    ax.plot(x_data, y1_data, color='blue', marker='', label='excess_return_compound')
    ax.plot(x_data, y2_data, color='red', marker='', label='allocation_effect_compound')
    ax.plot(x_data, y3_data, color='black', marker='', label='selection_effect_compound')
    

    这样就会隐藏折线图上的数据序列标记点,只显示光滑的曲线。同时保留图例,可以在legend函数中设置相应的位置参数,如loc='upper left',示例如下:

    plt.legend(loc='upper left')
    

    这样图例就会显示在左上角。