FacetGrid的Matplotlib轴

时间:2020-05-30 10:16:31

标签: matplotlib seaborn

为什么 https://seaborn.pydata.org/generated/seaborn.FacetGrid.html

没有ax参数像其他图形一样接受matplotlib轴

例如https://seaborn.pydata.org/generated/seaborn.distplot.html

因为我需要为FacetGrid设置matplotlib轴以引入自定义样式。

1 个答案:

答案 0 :(得分:1)

FaceGrid生成网格,因此无法将网格传递给它。各个图都存储在内部,并且可以像下面的代码一样进行操作。

import seaborn as sns
import pandas as pd

#dummy data
data= pd.DataFrame(data={'a':np.random.randint(0,2, 100),
                         'b':np.random.rand(100),
                         'c':np.random.rand(100)})

# make facetgrid and store in variable
g = sns.FacetGrid(data, col='a') # make facetgrid with 2 columns, because there are two distinct values in column a of data
g.map(plt.scatter, 'b', 'c') # map function to grid

# the individual axes of the grid are stored in g.
# you can access and edit them like so:

for ax in g.axes[0]:
    ax.set_ylabel('test')
    ax.set_ylim(0,1.5)
    ax.set_title('title')

enter image description here