0

I'm searching to color the points of a plot using a variable containing age of people. This is the code that I use:

import matplotlib.pyplot as plt
plt.scatter(df.x.values, df.y.values, s=60, c=df['BIRTHDT'])
plt.figtext(.5,.9,'My plot', fontsize=16, ha='center',fontweight="bold")
plt.xlabel('X',fontweight="bold",fontsize=16)
plt.ylabel('Y',fontweight="bold",fontsize=16)
plt.legend()
plt.rcParams['figure.figsize'] = [16, 9]
plt.show()

I have this Warning:

No handles with labels found to put in legend

the dataset is:

ID BIRTHDT X Y
0   49.0 5 6
1   66.0 7 7
2   56.0 4 7 
3   79.0 4 7 
4   66.0 12 4

I want the colors based on the continuous age like: <40 = blue ; 40-60=red; >60=orange and a legend in which there is the explanation of the colors. In my own way I have no legend.

Thanks a lot

Arkistarvh Kltzuonstev
  • 6,824
  • 7
  • 26
  • 56
Will
  • 1,619
  • 5
  • 23
  • I moved my answer to [this question](https://stackoverflow.com/a/60030582/12046409), as the question is similar, but it is needs some adaptions to map the current question to it. – JohanC Feb 02 '20 at 21:09
  • 1
    I extended the [answer](https://stackoverflow.com/a/60030582/12046409) with an example of how to have a colorbar in proportion to the age ranges. – JohanC Feb 02 '20 at 22:35
  • @JohanC instead if I want the legend for just a scatterplot colored using a discrete variable (like 1, 2 or 3) ? c = df['Z']; plt.scatter(df.x, df.y, s=60, c=c) – Will Feb 02 '20 at 23:58
  • 1
    For example [this post](https://stackoverflow.com/questions/26558816/matplotlib-scatter-plot-with-legend) and also [this](https://stackoverflow.com/questions/59344443/python-pyplot-legend-scatter) – JohanC Feb 03 '20 at 06:38

0 Answers0