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

Question:

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?

Asked By: Qubix

||

Answers:

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

Answered By: Celius Stingher

You can use matplotlib’s built in feature of cmaps for your scatter plot. It comes with an array of choices.

########## Recreating OP's data ############
import matplotlib.pyplot as plt
from math import log
x = [(5, 1), (2, 4), (1,6), (4,7)] 
testList2 = [(elem1, log(elem2)) for elem1, elem2 in x]
############################################

# Set the color intensity, just has to be unique numbers the length of your `x`
color_intensity = range(len(x))

# Use the cmap "Reds_r" to show darker red for lower number in the color intensity
plt.scatter(*zip(*testList2), c=color_intensity, cmap="Reds_r", edgecolors="black", s=100)
plt.show()

Graph:

enter image description here

Answered By: Michael S.