如何更改海运地块中的轴刻度数 [英] How to change the number of axis ticks in seaborn plots

查看:16
本文介绍了如何更改海运地块中的轴刻度数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在Python3.5中控制海运绘图上的轴刻度数。我非常习惯使用R的gglot,所以我在使用Python中的类似功能时遇到了一些麻烦。

作为示例,下面是我当前使用的数据类型:

test = pd.DataFrame()
test["X"] = [1,2,3,1,2,3]
test["Y"] = [1,5,3,7,2,4]
test["Category"] = ["A", "A", "A", "B", "B", "B"]

并且我想通过执行以下操作来做一些类似gglot的facet_work()的操作:

sns.set(style = "ticks", color_codes = True)
test_plot = sns.FacetGrid(test, col = "Category")
test_plot = (test_plot.map(sns.plt.plot, "X", "Y").add_legend())
test_plot.set_xticks(np.arange(1,4,1))
sns.plt.show(test_plot)
但是,我收到以下错误。问题似乎是关于在FaceGrid中设置轴标签,但我不知道如何解决它。这是Python 3的问题还是在多面图上指定轴的问题?

UserWarning: tight_layout : falling back to Agg renderer

warnings.warn("Tre_Layout:回退到Agg渲染器")

test_plot.set_xticks(np.arange(1,4,1))
AttributeError: 'FacetGrid' object has no attribute 'set_xticks'

推荐答案

set_xticks是matplotlib Axes对象上的方法,但FacetGrid有多个轴。您可以循环遍历它们并在每个元素上设置xtick,但更简单的方法是调用FacetGrid.set(xticks=np.arange(1,4,1)),它将在内部执行循环。

test_plot = sns.FacetGrid(test, col = "Category")
test_plot = test_plot.map(sns.lineplot, "X", "Y")
test_plot.set(xticks=np.arange(1,4,1), yticks=np.arange(1,10,1))

  • 现在应该用sns.relplotkind='line'
  • 来实现
p = sns.relplot(data=test, kind='line', x='X', y='Y', col='Category')
p.set(xticks=np.arange(1,4,1), yticks=np.arange(1,10,1))

  • 这也适用于轴级绘图
p = sns.lineplot(data=test, x='X', y='Y', hue='Category')
_ = p.set(xticks=np.arange(1,4,1), yticks=np.arange(1,10,1))

这篇关于如何更改海运地块中的轴刻度数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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