Search code examples
pythonpython-3.xmatplotlibplotscatter-plot

Plot each element in a list of tuples with different colours, based on its position in list


If you have a list of data points :

x = [(5, 1), (2, 4), (1,6), (4,7)] 

how can you scatter plot these pairs such that each point has a different shade of a color? For example (5,1) could have a dark red, for being the first, (2,4) a slightly whiter red, and so on, until the last point?


Solution

  • You can set the color palette and manipulate the individual dots using hue. For exapmle:

    import seaborn as sns
    import matplotlib.pyplot as plt
    data = [(5, 1), (2, 4), (1,6), (4,7)] 
    
    cm = sns.color_palette('rocket',len(data))
    plot = sns.scatterplot(x=[x[0] for x in data],y=[x[1] for x in data],hue=cm,legend=False,palette='rocket')
    

    If you wish for a specific color progression, I suggest you then sort the data array previously.

    Returns:

    enter image description here