Search code examples
pythonseabornboxplotsubplotgraph-visualization

Draw subplots boxplot using python


I want to draw two parallel box plot in together. For that I used sub plots function in python, below code I used for the that process, but I couldn't get good out put from the code, because its already draw addition two empty graphs, how I remove these empty graphs from the output? Please give ideas for that?

f, axes = plt.subplots(2,2,figsize = (14,10))
sns.boxplot(x='Heating QC',y='SalePrice',hue='Central Air',  data=df ,ax=axes[0,0])
sns.boxplot(x='Heating',y='SalePrice',hue='Central Air',  data=df ,ax=axes[0,1])

out put

p

After changes got below outputs

IndexError                                Traceback (most recent call last)
<ipython-input-543-7dfa6ebf0390> in <module>
      1 f, axes = plt.subplots(1,2,figsize = (14,10))
----> 2 sns.boxplot(x='Heating QC',y='SalePrice',hue='Central Air',  data=df ,ax=axes[0,0])
      3 sns.boxplot(x='Heating',y='SalePrice',hue='Central Air',  data=df ,ax=axes[0,1])

IndexError: too many indices for array

enter image description here


Solution

  • Just create two plots, in which case axes will be a list of 2 elements and use those plot.

    Refer the documentation.

    f, axes = plt.subplots(2, figsize = (14,10))
    sns.boxplot(x='Heating QC',y='SalePrice',hue='Central Air',  data=df, ax=axes[0])
    sns.boxplot(x='Heating',y='SalePrice',hue='Central Air',  data=df, ax=axes[1])