Home > Blockchain >  Changing the tick frequency on the x axis for each subplot on a FacetGrid plot in Seaborn
Changing the tick frequency on the x axis for each subplot on a FacetGrid plot in Seaborn

Time:01-21

Problem: I am trying to create a FacetGrid plot in Seaborn, where I have a yearWeek column as the x-axis and a conversionRate column as the y-axis. However, I want to only display every second yearWeek on the x-axis. How can I achieve this?   My current attempt:  

!python --version
print(f'Seaborn version: {sns.__version__}')
 
data = {'yearWeek': ['2022-W1','2022-W2','2022-W3','2022-W4','2022-W5','2022-W6','2022-W7','2022-W8','2022-W9','2022-W10','2022-W11','2022-W12']*3,
        'country': ['US','US','US','US','US','US','US','US','US','US','US','US']   ['India','India','India','India','India','India','India','India','India','India','India','India']   ['Australia','Australia','Australia','Australia','Australia','Australia','Australia','Australia','Australia','Australia','Australia','Australia'],
        'conversionRate': [np.random.rand() for i in range(12*3)]
       }
 
df = pd.DataFrame(data)
 
g = sns.FacetGrid(df, col="country", aspect=1.5)
g.map_dataframe(sns.lineplot, x='yearWeek', y='conversionRate')
for ax in g.axes.flat:
    ax.set_xticks(ax.get_xticks()[::2])
    plt.setp(ax.get_xticklabels(), rotation=45)

enter image description here

CodePudding user response:

If I understand correctly, the question is most related to enter image description here

Full code:

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

# generate reproducible `conversionRate`
rng = np.random.default_rng(12)

data = {'yearWeek': ['2022-W1','2022-W2','2022-W3','2022-W4','2022-W5','2022-W6','2022-W7','2022-W8','2022-W9','2022-W10','2022-W11','2022-W12']*3,
        'country': ['US','US','US','US','US','US','US','US','US','US','US','US']   ['India','India','India','India','India','India','India','India','India','India','India','India']   ['Australia','Australia','Australia','Australia','Australia','Australia','Australia','Australia','Australia','Australia','Australia','Australia'],
        'conversionRate': [rng.random() for i in range(12*3)]
       }
 
df = pd.DataFrame(data)
df = df[1::2].reset_index(drop=True)   

print(df)

g = sns.FacetGrid(df, col="country", aspect=1.5)
g.map_dataframe(sns.lineplot, x='yearWeek', y='conversionRate')

for ax in g.axes.flat:
    plt.setp(ax.get_xticklabels(), rotation=45)

plt.show()

with df as:

    yearWeek    country  conversionRate
0    2022-W2         US        0.946753
1    2022-W4         US        0.179291
2    2022-W6         US        0.230541
3    2022-W8         US        0.115079
4   2022-W10         US        0.858130
5   2022-W12         US        0.541466
6    2022-W2      India        0.257955
7    2022-W4      India        0.453616
8    2022-W6      India        0.927517
9    2022-W8      India        0.187890
10  2022-W10      India        0.946619
11  2022-W12      India        0.880250
12   2022-W2  Australia        0.936696
13   2022-W4  Australia        0.871556
14   2022-W6  Australia        0.219390
15   2022-W8  Australia        0.661634
16  2022-W10  Australia        0.201345
17  2022-W12  Australia        0.763625

Finally, if you don't want to change the original df, please check result of code

  • Related