更改 seaborn 图中 x 轴刻度的数量 [英] Change number of x-axis ticks in seaborn plots
问题描述
我希望能够在 Python 3.5 中控制 seaborn 图上的轴刻度数.我非常习惯使用 R 的 ggplot,所以我在 Python 中的类似功能上遇到了一些麻烦.
I would like to be able to control the number of axis ticks on a seaborn plot in Python 3.5. I'm very accustomed to using R's ggplot, so I'm having some trouble with analogous functionality in Python.
例如,这是我目前正在处理的数据类型:
As a example, here is the kind of data I'm currently working with:
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() 的事情:
And I would like to do something like ggplot's facet_wrap() by doing:
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 的问题还是在分面图上指定轴的问题?
However, I get the following errors. The problem seems to be something about setting axis labels in FacetGrid, but I don't know how to resolve it. Is this an issue with Python 3 or with specifying axes on a facetted plot?
UserWarning: tight_layout : falling back to Agg renderer
warnings.warn("tight_layout : 回退到 Agg 渲染器")
warnings.warn("tight_layout : falling back to Agg renderer")
test_plot.set_xticks(np.arange(1,4,1))
AttributeError: 'FacetGrid' object has no attribute 'set_xticks'
推荐答案
set_xticks
是 matplotlib Axes 对象上的一个方法,但是 FacetGrid
有很多轴.您可以遍历它们并为每个设置 xticks,但更简单的方法是调用 FacetGrid.set(xticks=np.arange(1,4,1))
,它将执行循环内部.
set_xticks
is a method on a matplotlib Axes object, but FacetGrid
has many axes. You could loop over them and set the xticks on each one, but an easier way is to call FacetGrid.set(xticks=np.arange(1,4,1))
, which will do the loop internally.
这篇关于更改 seaborn 图中 x 轴刻度的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!