【问题标题】:Subplots in seabornseaborn 中的子图
【发布时间】:2019-09-11 18:45:02
【问题描述】:

如何创建一个带有 2 个 seaborn 地块(子地块)的地块

如何将 2 个 seaborn 地块合并为 1 个?

plt.figure(figsize = (12, 6))
ax = sns.scatterplot(x = model1.fittedvalues, y = model1.resid)
plt.grid()
ax.axhline(y=0, color='r', linewidth=4) 
ax.set_xlabel("vysvětlovaná proměnná");
ax.set_ylabel("residua");


plt.figure(figsize = (12, 6))
ax = sns.distplot(a = model1.resid, bins = 40, norm_hist=True,)
plt.grid()
ax.set_title("Histogram reziduí", fontsize = 25);

【问题讨论】:

    标签: python seaborn


    【解决方案1】:

    您可以随心所欲地创建子图(使用 plt.subplots()fig.add_subplot()GridSpec,您可以命名),然后使用 ax=<your axes reference> 将轴的引用传递给 seaborn 函数

    fig, (ax1, ax2) = plt.subplots(1,2, figsize = (24, 6))
    sns.scatterplot(x = model1.fittedvalues, y = model1.resid, ax=ax1)
    ax1.grid()
    ax1.axhline(y=0, color='r', linewidth=4) 
    ax1.set_xlabel("vysvětlovaná proměnná");
    ax1.set_ylabel("residua");
    
    sns.distplot(a = model1.resid, bins = 40, norm_hist=True, ax=ax2)
    ax2.grid()
    ax2.set_title("Histogram reziduí", fontsize = 25);
    

    【讨论】:

      【解决方案2】:

      你可以更好地定义子图:

      import matplotlib.pyplot as plt
      import seaborn as sns
      import numpy as np
      
      x = np.array([4,3,2,3])
      y = np.array([5,1,6,4])
      
      **fig, sub = plt.subplots(2,1)**
      
      plt.figure(figsize = (12, 6))
      sns.scatterplot(x, y , **ax=sub[0]**)
      
      ax.axhline(y=0, color='r', linewidth=4) 
      ax.set_xlabel("vysvětlovaná proměnná")
      ax.set_ylabel("residua")
      
      
      
      plt.figure(figsize = (12, 6))
      sns.distplot(x, bins = 40, norm_hist=True, **ax=sub[1]**)
      
      ax.set_title("Histogram reziduí", fontsize = 25)
      

      【讨论】:

        猜你喜欢
        • 2020-08-22
        • 2020-11-09
        • 1970-01-01
        • 2017-05-14
        • 1970-01-01
        • 2017-02-25
        • 2018-09-05
        • 2020-05-11
        • 2021-03-10
        相关资源
        最近更新 更多