Home > Blockchain >  Create scatter plot where the color is based on year without adding extra column
Create scatter plot where the color is based on year without adding extra column

Time:10-01

I have this data in a csv table

TIME X Y
2020-07-03T00:00:00Z 0.08 0.30
2021-07-03T00:00:00Z 0.20 0.12

And i want to create a scatter plot between variable X and Y, and color the dots based on the year of that data as seen in time. I know i could add an extra column with the year in it, but is there a more elegant way of doing this without adding columns?

CodePudding user response:

You can do it without creating an additional column, but it won't be more elegant. enter image description here Then you can use the cmap parameter to specify what colormap you want to apply.

CodePudding user response:

c = pd.to_datetime(df.TIME).dt.year
c = c.tolist()
X = df.X
Y = df.Y
plt.scatter(X,Y,c=c)

enter image description here

Don't have to create a column. Just convert the year to a list and then you can map whatever colour based on the list.

  • Related