I have a large pandas dataframe that I want to create a plot - here is a simplified example:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
colors = iter(cm.rainbow(np.linspace(0, 1, 4)))
sample = pd.DataFrame({'X': [1,2,3,1,2,3,1,2,1,2,3],
'Y': [1,1,1,2,2,2,3,3,4,4,4]})
I want to create a color coded line plot, with color changing based on X column values (X values are always repeats the same numbers, but the length is not always the same) - the default plot is :
plt.plot(sample['X'], sample['Y'], linestyle = '-')
but I want to automate the process, so everytime X column restarts to have a new color - this is the result that I want to have
plt.plot(sample['X'][0:3], sample['Y'][0:3], linestyle = '-', color = next(colors))
plt.plot(sample['X'][3:6], sample['Y'][3:6], linestyle = '-', color = next(colors))
plt.plot(sample['X'][6:8], sample['Y'][6:8], linestyle = '-', color = next(colors))
plt.plot(sample['X'][8:], sample['Y'][8:], linestyle = '-', color = next(colors))
any suggestion on how to achieve this?

