更改海运图中的x轴滴答数

时间:2021-03-05 23:41:47

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.

我希望能够控制Python 3.5中的海运图上的轴节拍数。我非常习惯于使用R的ggplot,所以我在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"]

And I would like to do something like ggplot's facet_wrap() by doing:

我想做一些类似于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)

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?

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

UserWarning: tight_layout : falling back to Agg renderer

warnings.warn("tight_layout : falling back to Agg renderer")

警告。警告(“tight_layout:回落到Agg渲染器”)

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

1 个解决方案

#1


11  

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.

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

#1


11  

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.

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