0

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?

0

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.