Setting the interval of x-axis for seaborn plot

一曲冷凌霜 提交于 2021-02-10 04:21:16

问题


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.

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')

回答1:


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()



来源:https://stackoverflow.com/questions/43639096/setting-the-interval-of-x-axis-for-seaborn-plot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!