turtle_in_mind
turtle_in_mind

Reputation: 1152

scatter plot with different colors and labels

I have a pandas dataframe. It looks like this: enter image description here

Im trying to create scatter plot with different colors for each point. I tried:

df.plot.scatter(x='x',y='y',c=df.colors.tolist(), legend=True)

I get the scatter plot allright. But im not able to show label that "Financials" is associated with color '#4ce068'.

How do i do that here? Thanks

I tried:

df.plot.scatter(x='x',y='y',c=df.colors.tolist(),label=df.key.unique.tolist())

This almost works but the fact there there are too many labels and the colors associate with the label is hard to see.

I would like to have the key shows with associated color preferably on top of the chart i.e next to title. Is that possible?

My dataframe in dictionary format: 

{'key': {0: 'Financials',
1: 'Consumer Discretionary',
2: 'Industrials',
3: 'Communication Services',
4: 'Communication Services',
5: 'Consumer Discretionary',
6: 'Health Care',
7: 'Information Technology',
8: 'Consumer Discretionary',
9: 'Information Technology'},
'x': {0: 1630000.0,
 1: 495800000.0,
 2: 562790000.0,
 3: 690910000.0,
 4: 690910000.0,
 5: 753090000.0,
 6: 947680000.0,
 7: 1010000000.0,
 8: 1090830000.0,
 9: 1193600000.0},
 'y': {0: 0.02549175,
 1: 0.0383163,
 2: -0.09842154,
 3: 0.03876266,
 4: 0.03596279,
 5: 0.01897367,
 6: 0.06159238,
 7: 0.0291209,
 8: 0.003931255,
 9: -0.007134976},
'colors': {0: '#4ce068',
 1: '#ecef4a',
 2: '#ff3c83',
 3: '#ff4d52',
 4: '#ff4d52',
 5: '#ecef4a',
 6: '#00f1d1',
 7: '#d9d9d9',
 8: '#ecef4a',
 9: '#d9d9d9'}}

Upvotes: 2

Views: 3490

Answers (2)

rpanai
rpanai

Reputation: 13437

If you don't mind using plotly you can try

import pandas as pd
import plotly.express as px

px.scatter(df, 'x', 'y', color="key", color_discrete_sequence=df["colors"].tolist())

enter image description here

But given your choice of colors just px.scatter(df, 'x', 'y', color="key") will look better

enter image description here

Upvotes: 3

ignoring_gravity
ignoring_gravity

Reputation: 10476

Here's one way to do it:

fig, ax = plt.subplots()
for i in df.colors.unique():
    df_color = df[df.colors==i]
    df_color.plot.scatter(x='x',y='y', ax=ax, c=i, label=df_color.key.iloc[0])

ax.legend(loc='best')

You'll get enter image description here

Upvotes: 2

Related Questions