#196 Select one color with matplotlib

This section aims to describe how to set a color with the matplotlib library of python. These techniques also work for most of the python libraries, since most of them are build on top of matplotlib. Note that another page is dedicated to the choice and utilisation of color palettes.

  •  

     

     

    The most common way to call a color is by its name. It is highly advised to avoid the basic ‘blue’, ‘red’, ‘green’… that are quite ugly. There are so many other nice possibilities. See a figure found here that shows all the possibilities. (Thanks to the matplotlib documentation for their work).

    
    # library & dataset
    from matplotlib import pyplot as plt
    import pandas as pd
    import numpy as np
    df=pd.DataFrame({'x': range(1,101), 'y': np.random.randn(100)*15+range(1,101), 'z': (np.random.randn(100)*15+range(1,101))*2 })
    
    # plot
    plt.plot( 'x', 'y', data=df, marker='o', color='mediumvioletred')
    
    
  •  

    In case you are really lazy, a few colors can be called by their abbreviations:

    b: blue
    g: green
    r: red
    c: cyan
    m: magenta
    y: yellow
    k: black
    w: white

     

     

    
    # library
    from matplotlib import pyplot as plt
    import pandas as pd
    import numpy as np
    df=pd.DataFrame({'x': range(1,101), 'y': np.random.randn(100)*15+range(1,101), 'z': (np.random.randn(100)*15+range(1,101))*2 })
    
    # Plot
    plt.plot( 'x', 'y', data=df, marker='o', color='c')
    plt.show()
    

     

  •  

     

    You can call absolutely every existing color using their Hex code: the code we use in the html language for web site mostly. Here is a really good tool that allows you to find the color you need!

     

     

     

     

     

     

    
    # library & dataset
    from matplotlib import pyplot as plt
    import pandas as pd
    import numpy as np
    df=pd.DataFrame({'x': range(1,101), 'y': np.random.randn(100)*15+range(1,101), 'z': (np.random.randn(100)*15+range(1,101))*2 })
    
    # plot 
    plt.plot( 'x', 'y', data=df, marker='o', color='#8f9805')
    plt.show()
    
    
    
  •  

     

    RGB means Red Green Blue. It allows to pick up a color specifying its quantity of red, green, and blue (the primary color). Note that a 4th number is provided: it is the transparency. This feature can be really useful. It makes the chart more pretty and allows to fight against over plotting.

     

     

     

     

     

    
    # library & dataset
    from matplotlib import pyplot as plt
    import pandas as pd
    import numpy as np
    df=pd.DataFrame({'x': range(1,101), 'y': np.random.randn(100)*15+range(1,101), 'z': (np.random.randn(100)*15+range(1,101))*2 })
    
    # plot
    plt.plot( 'x', 'y', data=df, marker='o', color=(0.9, 0.2, 0.5, 0.2))
    
    
  •  

     

     

    Want to make a black and white plot? It is possible to provide a number between 0 and 1. It will give a color between black(0) and white (1). Useful for scientific publication or printing.

     

     

     

     

     

    
    plt.plot( 'x', 'y', data=df, marker='o', color='0.9')
    
    
  •  

     

     

    Whatever color you use, you can add an argument allowing to add some transparency. This will probably make your plot more pretty, and can also be used against over plotting. Note that 0 is completely transparent, and 1 gives a complete opacity.

     

     

     

     

    
    plt.plot( 'x', 'y', data=df, marker='o', color='blue', alpha=0.3)
    
    

Leave a Reply

Your email address will not be published.