Setting the interval of x-axis for seaborn plot

19,743

Seaborn pointplot is a categorical plot. This means that the different categories are simply placed one by one along the x axis.

The idea would therefore be to change the locator as well as the formatter for the xticks.

import seaborn.apionly as sns
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import numpy as np; np.random.seed(1)

x = np.random.randint(0,20,size=(100))
y = np.random.rand(100)

ax = sns.pointplot(x,y )
ax.xaxis.set_major_locator(ticker.MultipleLocator(5))
ax.xaxis.set_major_formatter(ticker.ScalarFormatter())

plt.show()

enter image description here

Share:
19,743
Amrith Krishna
Author by

Amrith Krishna

COMPUTER SCIENCE ENGINEER. Period.

Updated on June 14, 2022

Comments

  • Amrith Krishna
    Amrith Krishna almost 2 years

    I have a set of subplots to plot. Here, how do we set the interval for x-axis in the subplot in second row, i.e ax4 to ax6. Currently all the values from 1 to 100 are printed as shown in the figure. I tried ax4.set_xticks(range(1,100,5)). But there, the range shown was 1 to 20. I was expecting a range from 1 to 100, with an interval of 5, i.e. 1,5,10...95,100

    Currently the plot has x-axis as shown below. I have not added the code for first row.

    enter image description here

    yInit = initRes
    yInit = yInit[(yInit['nodeSKT'] < 92) & (yInit['nodeSKT'] > 1)]
    
    sns.set_context("paper", font_scale=2, rc={"lines.linewidth": 1.2})
    fig, (ax4, ax5, ax6) = plt.subplots(nrows=1,ncols=3,figsize=(18,10))
    plt.figure()
    
    xval = 'nodeSKT'
    sns.pointplot(x=xval, y='lemmaPrec', data=yInit,join=False,ax=ax4)
    sns.pointplot(x=xval, y='wordPrec',color="#2ecc71",data=yInit, join=False,ax=ax4)
    sns.pointplot(x=xval, y='lemmaReca', data=yInit,join=False,ax=ax5)
    sns.pointplot(x=xval, y='wordReca',color="#2ecc71",data=yInit, join=False,ax=ax5)
    sns.pointplot(x=xval, y='lemmaFsco', data=yInit,join=True,ax=ax6)
    sns.pointplot(x=xval, y='wordFsco',color="#2ecc71",data=yInit, join=False,ax=ax6)
    plt.savefig('lem_fscore.png')