Search code examples
pythonpandasmatplotlibseabornpolar-coordinates

How to change degrees in polar plot projection (seaborn.FacetGrid) with datas from a column in my df?


so, basically I am trying to plot this point-cloud in which different sets of points belongs to different given "families"(e.g. k1, k2, k3, k4...). The results is kinda nice, but untill now I am not being able to set the angles of the polar plot as I wish, that is giving the degree values from my dataframe, so giving the directions to the various sets. Is it possible to do that?

here's what I've tried and the result:

g = sns.FacetGrid(data_kn, hue='discontinuity set', subplot_kws=dict(projection='polar'), height=5, sharex=False, sharey=False, despine=False)

g.map_dataframe(sns.scatterplot, x='Dip direction (degrees)', y='Dip (degrees)')

g.add_legend()

plt.show()

as you can see the points are sparse throughout the polar countour, even if their 'radiant position' is right. Beyond this, it is impossible to see the points belonging to two sets.


Solution

  • You have to convert the 'angle' column into degrees.

    Here is an example with the cardioid curve. If you keep the angles in degrees:

    import pandas as pd
    import seaborn as sns 
    import numpy as np
    import matplotlib.pyplot as plt
    
    ##### Creating the dataframe
    angle = np.linspace(0, 360, 100)
    d = {'Angles': angle, 'Radius': np.cos(np.pi*angle/180)*2 + 1}
    df = pd.DataFrame(data=d)
    
    ### making the plot
    g = sns.FacetGrid(df, subplot_kws=dict(projection='polar'), height=5, sharex=False, sharey=False, despine=False)
    g.map_dataframe(sns.scatterplot, x='Angles', y='Radius')
    plt.show()
    

    enter image description here

    But if you convert them before:

    import pandas as pd
    import seaborn as sns 
    import numpy as np
    import matplotlib.pyplot as plt
    
    ### Creating the dataframe
    angle = np.linspace(0, 360, 100)
    d = {'Angles': angle, 'Radius': np.cos(np.pi*angle/180)*2 + 1}
    df = pd.DataFrame(data=d)
    
    
    df['Angles'] = df['Angles']*np.pi/180 ### convert the angles into radiant
    
    ### Make the plot
    g = sns.FacetGrid(df, subplot_kws=dict(projection='polar'), height=5, sharex=False, sharey=False, despine=False)
    g.map_dataframe(sns.scatterplot, x='Angles', y='Radius')
    plt.show()
    

    If you want to change the position of the origin, just add before plt.show()

    plt.gca().set_theta_zero_location("N")
    

    Here 'N' goes for 'North'.

    enter image description here