Search code examples
pythonpandasseabornscatter-plot

Scatterplot grouped by a column


I am trying to make a scatterplot over two different types of categorical variables, each with three different levels. Right now I am using the seaborn library in python:

sns.pairplot(x_vars = ['UTM_x'], y_vars = ['UTM_y'], data = df, hue = "Mobility_Provider", height = 5)
sns.pairplot(x_vars = ['UTM_x'], y_vars = ['UTM_y'], data = df, hue = "zone_number", height = 5)

which gives me two separate scatter plot, one grouped by Mobility_Provider, one grouped by zone_number. However, I was wondering if it's possible to combine these two graphs together, e.g. different levels of Mobility_Provider are represented in different colours, while different levels of zone_number are represented in different shapes/markers of the plot. Thanks a lot!

A sample plot would be: Plot1 Plot2

Each row of the df has x and y values, and two categorical variables ("Mobility_Provider" and "zone_number")


Solution

  • This can be easily done using seaborn's scatterplot, just use
    hue = "Mobility_Provider",style="zone_number"
    Something like this

    import matplotlib.pyplot as plt
    import seaborn as sns; sns.set()
    import pandas as pd
    
    df = pd.DataFrame({'x':[1,2,3,4],'y':[1,2,3,4],'Mobility_Provider':[0,0,1,1],\
                       'zone_number':[0,1,0,1]})
    
    sns.scatterplot(x="x", y="y",s=100,hue='Mobility_Provider',style='zone_number', data=df)
    plt.show()
    

    enter image description here