How to plot 2 seaborn lmplots side-by-side?

前端 未结 2 459
广开言路
广开言路 2020-12-24 02:32

Plotting 2 distplots or scatterplots in a subplot works great:

import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import pandas as pd
%         


        
2条回答
  •  悲&欢浪女
    2020-12-24 02:47

    You get that error because matplotlib and its objects are completely unaware of seaborn functions.

    Pass your axes objects (i.e., ax1 and ax2) to seaborn.regplot or you can skip defining those and use the col kwarg of seaborn.lmplot

    With your same imports, pre-defining your axes and using regplot looks like this:

    # create df
    x = np.linspace(0, 2 * np.pi, 400)
    df = pd.DataFrame({'x': x, 'y': np.sin(x ** 2)})
    df.index.names = ['obs']
    df.columns.names = ['vars']
    
    idx = np.array(df.index.tolist(), dtype='float')  # make an array of x-values
    
    # call regplot on each axes
    fig, (ax1, ax2) = plt.subplots(ncols=2, sharey=True)
    sns.regplot(x=idx, y=df['x'], ax=ax1)
    sns.regplot(x=idx, y=df['y'], ax=ax2)
    

    Using lmplot requires your dataframe to be tidy. Continuing from the code above:

    tidy = (
        df.stack() # pull the columns into row variables   
          .to_frame() # convert the resulting Series to a DataFrame
          .reset_index() # pull the resulting MultiIndex into the columns
          .rename(columns={0: 'val'}) # rename the unnamed column
    )
    sns.lmplot(x='obs', y='val', col='vars', hue='vars', data=tidy)
    

提交回复
热议问题