[Python] What happens when you call a matplotlib continuous colormap with value outside of the...

Discussão em 'Python' iniciado por Stack, Maio 12, 2025.

  1. Stack

    Stack Membro Participativo

    The classical usage for a continuous colorbar such as viridis is to use cm(val) with val bewteen 0 and 1.

    I can't figure out what's returned when you call it with an argument outside of the (0, 1) range, i.e. I'd expect to get the value corresponding to 1, but it's not the case:

    In [34]: viridis = plt.get_cmap("viridis")

    In [35]: viridis(1)
    Out[35]:
    (np.float64(0.26851),
    np.float64(0.009605),
    np.float64(0.335427),
    np.float64(1.0))

    In [36]: viridis(2)
    Out[36]:
    (np.float64(0.269944),
    np.float64(0.014625),
    np.float64(0.341379),
    np.float64(1.0))

    In [37]: viridis(3)
    Out[37]:
    (np.float64(0.271305),
    np.float64(0.019942),
    np.float64(0.347269),
    np.float64(1.0))


    Similarly, what happens when you call a discrete cmap, e.g. tab10, with a float argument outside of the (0, 1) range? If it's inside [0, 1], then I found out that this is treated as int(val * cm.N), but if it's outside this no longer holds.

    Continue reading...

Compartilhe esta Página