Search code examples
pythonpandasmatplotlibscatter

How do I add color as a 3rd dimension to matplotlib scatterplot?


I have a pandas dataframe like following:

Category X_Axis Y_Axis
Categor1 10000 10000
Categor2 15000 20000
Categor3 20000 30000
Categor1 25000 40000
Categor2 10000 50000

I want my code to create a scatter plot with X_Axis values on horizontal, and Y_Axis on vertical, and colors showing the categories, with every category having a different color and a legend showing them.

I cannot do this manually because of the size of the data and having a lot of different categories.

Thanks in advance.


Solution

  • If you can use seaborn:

    import seaborn as sns
    
    sns.scatterplot('X_Axis','Y_Axis',hue='Category',data=df)
    

    Result:

    enter image description here