Vincent Lee
Vincent Lee

Reputation: 1

Index Error when Restricting the Levels in ax.clabel

I am trying to put labels on a subset of levels in a contour plot at specific locations. A minimal toy code that reproduces the error is

    import numpy as np
    import matplotlib.pyplot as plt

    # Create some data
    X, Y = np.meshgrid(np.linspace(-5, 5, 100), np.linspace(-5, 5, 100))
    Z = np.sin(X) * np.cos(Y)

    # Create a contour plot
    fig, ax = plt.subplots()
    cs = ax.contour(X, Y, Z, levels=np.linspace(-1, 1, 5)) 

    # label positions
    label_positions = [(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]

    # Apply manual labeling
    ax.clabel(cs, levels=cs.levels[2:], inline=False, 
    manual=label_positions[2:])

    # show plot
    plt.show()
    plt.close()

However, I got the following error "list index out of range" even though both the levels and the label positions have the same size. Curiously the error goes away if I use ax.clabel(cs, levels=cs.levels, inline=False, manual=label_positions) instead. How do I fix this error, while only showing labels for a subset of levels?

Upvotes: 0

Views: 31

Answers (0)

Related Questions