设置seaborn图的x轴间隔 [英] Setting the interval of x-axis for seaborn plot

查看:262
本文介绍了设置seaborn图的x轴间隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组要绘制的子图.在这里,我们如何在第二行的子图中设置 x 轴的间隔,即 ax4ax6.目前,从 1 到 100 的所有值都被打印出来,如图所示.我试过 ax4.set_xticks(range(1,100,5)).但是在那里,显示的范围是 1 到 20.我期望范围是 1 到 100,间隔为 5,即 1,5,10...95,100

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

目前该图具有 x 轴,如下所示.我没有添加第一行的代码.

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

推荐答案

Seaborn pointplot 是一个分类图.这意味着不同的类别只是简单地沿 x 轴一一放置.

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

因此,我们的想法是更改 xticks 的定位器和格式化程序.

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

这篇关于设置seaborn图的x轴间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆