更改seaborn图中的x轴刻度数

时间:2015-10-01 18:56:22

标签: python axes seaborn

我希望能够在Python 3.5中控制seaborn图上的轴刻度数。我已经习惯使用R的ggplot了,所以我在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"]

我想通过以下方式做一些像ggplot的facet_wrap():

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)

但是,我收到以下错误。问题似乎是关于在FacetGrid中设置轴标签,但我不知道如何解决它。这是Python 3的问题还是在分面图上指定轴?

UserWarning: tight_layout : falling back to Agg renderer

warnings.warn(“tight_layout:回到Agg渲染器”)

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

1 个答案:

答案 0 :(得分:14)

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