[Python] why getting: IndexError: list index out of range error?

Discussão em 'Python' iniciado por Stack, Janeiro 26, 2025.

  1. Stack

    Stack Membro Participativo

    contour = ax1.contour(combined_theta, combined_r, combined_density, levels=[1.0], linewidths=1.5, colors=colors)
    contour_paths = contour.collections[0].get_paths()
    for path in contour_paths:
    vertices = path.vertices
    theta_vals = vertices[:, 0]
    r_vals = vertices[:, 1]
    mask = np.ones_like(combined_theta, dtype=bool)
    print(f"combined_theta: {combined_theta}")
    print(f"combined_r: {combined_r}")
    for i in range(len(combined_theta)):
    for j in range(len(combined_r[0])): # Assuming combined_r is 2D
    if np.any(np.isclose(combined_theta[i, j], theta_vals)) and np.any(np.isclose(combined_r[i, j], r_vals)):
    mask[i, j] = False
    remaining_theta = combined_theta[mask]
    remaining_r = combined_r[mask]
    print(f"Remaining theta_vals (after removing): {remaining_theta}")
    print(f"Remaining r_vals (after removing): {remaining_r}")
    ax1.fill(remaining_theta, remaining_r, color=colors, alpha=0.3, zorder=2)


    although the printed values are fine:

    combined_theta: [[ 1.57079633 1.57079633 1.57079633 ... 1.57079633 1.57079633
    1.57079633]
    [ 1.55443387 1.55443387 1.55443387 ... 1.55443387 1.55443387
    1.55443387]
    [ 1.53807141 1.53807141 1.53807141 ... 1.53807141 1.53807141
    1.53807141]
    ...
    [-1.53807107 -1.53807107 -1.53807107 ... -1.53807107 -1.53807107
    -1.53807107]
    [-1.55443353 -1.55443353 -1.55443353 ... -1.55443353 -1.55443353
    -1.55443353]
    [-1.57079599 -1.57079599 -1.57079599 ... -1.57079599 -1.57079599
    -1.57079599]]
    combined_r: [[1.18999999e+00 1.21394504e+00 1.23837191e+00 ... 2.40235105e+03
    2.45069089e+03 2.50000342e+03]
    [1.18999999e+00 1.21394504e+00 1.23837191e+00 ... 2.40235105e+03
    2.45069089e+03 2.50000342e+03]
    [1.18999999e+00 1.21394504e+00 1.23837191e+00 ... 2.40235105e+03
    2.45069089e+03 2.50000342e+03]
    ...
    [1.18999999e+00 1.21394504e+00 1.23837191e+00 ... 2.40235105e+03
    2.45069089e+03 2.50000342e+03]
    [1.18999999e+00 1.21394504e+00 1.23837191e+00 ... 2.40235105e+03
    2.45069089e+03 2.50000342e+03]
    [1.18999999e+00 1.21394504e+00 1.23837191e+00 ... 2.40235105e+03
    2.45069089e+03 2.50000342e+03]]
    Remaining theta_vals (after removing): [ 1.57079633 1.57079633 1.57079633 ... -1.57079599 -1.57079599
    -1.57079599]
    Remaining r_vals (after removing): [1.18999999e+00 1.21394504e+00 1.23837191e+00 ... 2.40235105e+03
    2.45069089e+03 2.50000342e+03]


    It is very confusing that why it is showing ax1.fill(remaining_theta, remaining_r, color=colors, alpha=0.3, zorder=2) IndexError: list index out of range

    Continue reading...

Compartilhe esta Página