[Python] Passing colors as a list to matplotlib

Discussão em 'Python' iniciado por Stack, Outubro 6, 2024.

  1. Stack

    Stack Membro Participativo

    I have a plot with x of shape 180 and y of shape 36, 180. So I have 36 lines when I call plot. How do I set the first 30 to blue and the last six to red without calling a loop?

    import numpy as np
    import matplotlib.pyplot as plt


    x = np.linspace(0, 10, 180)
    y = np.random.rand(36, 180)


    colors = ['blue'] * 30 + ['red'] * 6

    plt.plot(x, y.T, color=colors)

    plt.show()


    However this returns the following error:

    ValueError: ['blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'red', 'red', 'red', 'red', 'red', 'red'] is not a valid value for color

    Continue reading...

Compartilhe esta Página