Search code examples
pythoncolorsmatplotlib

Multiple colors in Python


I am plotting a dataframe with 100+ columns and I stacked all of them for each index value, the plot is below: enter image description here

However I wanted the color to be in a shading way, or at least the person can see the trend (from col1 to col100) so that it is clearer and I dont need those super long legends.

Looking forward to any advice!


Solution

  • You can avoid specifying colours by name and instead specify RGB (or RGBA) values. For instance:

    plot(linspace(0,4,10), linspace(0,10,10), lw=10, c=(1.0,1.0,0.90))
    plot(linspace(0,5,10), linspace(0,9,10), lw=10, c=(1.0,1.0,0.80))
    plot(linspace(0,6,10), linspace(0,8,10), lw=10, c=(1.0,1.0,0.70))
    plot(linspace(0,7,10), linspace(0,7,10), lw=10, c=(1.0,1.0,0.60))
    plot(linspace(0,8,10), linspace(0,6,10), lw=10, c=(1.0,1.0,0.50))
    plot(linspace(0,9,10), linspace(0,5,10), lw=10, c=(1.0,1.0,0.40))
    plot(linspace(0,10,10), linspace(0,4,10), lw=10, c=(1.0,1.0,0.30))
    

    Will plot:

    Shades of yellow

    Here the colours are specified as an RGB 3-tuple (e.g. c=(1.0,1.0,0.30)) where the values range between 0 and 1 for red, green and blue components. By updating the values with various gradients you can drift between colours or shades.