代码之家  ›  专栏  ›  技术社区  ›  Kevin

在一个图上绘制多个散点图/折线图时,如何取消不同图的连接?

  •  1
  • Kevin  · 技术社区  · 5 月前

    我在一个图形和轴上绘制多条线和散点图。我的代码设置了一个名为total_steel_area的变量,然后遍历另一个称为phi_x__h的变量的一组值。然后,它根据这些变量计算x和y值,并将其放入列表中。然后绘制这些值。然后,代码移动到total_steel_area的下一个值并重复。输出图如下所示。

    对角线将一组x、y值的最后一个值连接到下一组的第一个值。我的问题是如何删除这条连接线?

    我的代码如下:

    phi_N_bh = []
    phi_M_bh2 = []
    fig, ax = plt.subplots(dpi=100, figsize=(8,4))
    
    for total_steel_area in np.arange(0.01,0.06,0.01):
    
        for phi_x__h in np.arange(0.1,2,0.1):
            phi_N__bh, phi_M__bh2 = calculate_phi_N__bh_and_phi_M__bh2(phi_x__h, lamb, alpha_cc, eta, f_ck, E_s, varepsilon_cu2, phi_d__h, phi_d2__h, f_yk, total_steel_area/2, total_steel_area/2)
            phi_N_bh.append(phi_N__bh)
            phi_M_bh2.append(phi_M__bh2)
    
        ax.plot(phi_M_bh2, phi_N_bh, c='b')
        ax.scatter(phi_M_bh2, phi_N_bh, c='b', s=10)
            
    ax.set_title('Column Design Chart for Rectangular Column with Symmetrical Compression and Tension Reinforcement')   
    ax.set_xlabel('M/bh²')
    ax.set_ylabel('N/bh')
    ax.text(1-0.1, 1-0.1, f'f_ck = {f_ck}, f_yk = {f_yk}', horizontalalignment='center',
        verticalalignment='center', transform=ax.transAxes)
    ax.text(1-0.1, 1-0.2, f'd/h = {phi_d__h}, d2/h = {phi_d2__h}', horizontalalignment='center',
        verticalalignment='center', transform=ax.transAxes) 
    ax.set_ylim(0)
    ax.set_xlim(0)
    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)
    ax.legend()
    

    Graph showing multiple plots and unwanted connecting line

    1 回复  |  直到 5 月前
        1
  •  0
  •   mozway    5 月前

    您应该在外循环的每个步骤初始化/重置列表:

    for total_steel_area in np.arange(0.01,0.06,0.01):
        phi_N_bh = []
        phi_M_bh2 = []
    
        for phi_x__h in np.arange(0.1,2,0.1):
            phi_N__bh, phi_M__bh2 = calculate_phi_N__bh_and_phi_M__bh2(phi_x__h, lamb, alpha_cc, eta, f_ck, E_s, varepsilon_cu2, phi_d__h, phi_d2__h, f_yk, total_steel_area/2, total_steel_area/2)
            phi_N_bh.append(phi_N__bh)
            phi_M_bh2.append(phi_M__bh2)
    
        ax.plot(phi_M_bh2, phi_N_bh, c='b')
        ax.scatter(phi_M_bh2, phi_N_bh, c='b', s=10)     
    

    可能有一种矢量方法来计算你的值,但如果没有可重复的函数示例,这很难猜测。

    输出:

    enter image description here