Home > other >  Add horizontal lines from min point to max point per category in seaborn catplot
Add horizontal lines from min point to max point per category in seaborn catplot

Time:03-12

I am trying to visualize different type of "purchases" over a quarterly period for selected customers. To generate this visual, I am using a catplot functionality in seaborn but am unable to add a horizontal line that connects each of the purchased fruits. Each line should start at the first dot for each fruit and end at the last dot for the same fruit. Any ideas on how to do this programmatically?

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

dta = pd.DataFrame(columns=["Date", "Fruit", "type"], data=[['2017-01-01','Orange', 
'FP'], ['2017-04-01','Orange', 'CP'], ['2017-07-01','Orange', 'CP'], 
['2017-10-08','Orange', 'CP'],['2017-01-01','Apple', 'NP'], ['2017-04-01','Apple', 'CP'], 
['2017-07-01','Banana', 'NP'], ['2017-10-08','Orange', 'CP']
                                                        ])
dta['quarter'] = pd.PeriodIndex(dta.Date, freq='Q')

sns.catplot(x="quarter", y="Fruit", hue="type", kind="swarm", data=dta)

plt.show()

This is the result:

enter image description here.

How can I add individual horizontal lines that each connect the dots for purchases of orange and apple?

CodePudding user response:

You just need to enable the horizontal grid for the chart as follows:

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

dta = pd.DataFrame(
    columns=["Date", "Fruit", "type"],
    data=[
        ["2017-01-01", "Orange", "FP"],
        ["2017-04-01", "Orange", "CP"],
        ["2017-07-01", "Orange", "CP"],
        ["2017-10-08", "Orange", "CP"],
        ["2017-01-01", "Apple", "NP"],
        ["2017-04-01", "Apple", "CP"],
        ["2017-07-01", "Banana", "NP"],
        ["2017-10-08", "Orange", "CP"],
    ],
)


dta["quarter"] = pd.PeriodIndex(dta.Date, freq="Q")
sns.catplot(x="quarter", y="Fruit", hue="type", kind="swarm", data=dta)
plt.grid(axis='y')
plt.show()

Preview

enter image description here

CodePudding user response:

Each line should start at the first dot for each fruit and end at the last dot for the same fruit.

  • Related