【问题标题】:Matplotlib axis for FacetGridFacetGrid 的 Matplotlib 轴
【发布时间】:2020-05-30 10:16:31
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: matplotlib seaborn


    【解决方案1】:

    FaceGrid 生成网格,因此您不能将网格传递给它。各个图都存储在里面,可以像下面的代码一样进行操作。另请注意Axes and Axis are different in Matplotlib。下面的代码展示了一种同时获取 Axes 和 Axis 的方法:

    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')
        # now you can obtain the axis from one of the axes
        x_axis = ax.get_xaxis()
    

    【讨论】:

      猜你喜欢
      • 2019-08-02
      • 1970-01-01
      • 1970-01-01
      • 2019-04-28
      • 1970-01-01
      • 2018-02-25
      • 1970-01-01
      • 2017-04-10
      • 1970-01-01
      相关资源
      最近更新 更多