1

I want to add another plot next to pairplot result in the same figure.

For example, this code:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.datasets import load_iris


iris = load_iris()
data = pd.DataFrame(data = np.c_[iris['data'], iris['target']], columns = iris['feature_names'] + ['target'])

sns.pairplot(data)
plt.show()
plt.plot(data['sepal length (cm)'].to_numpy())
plt.show()

I want to somehow have both the pairplot and the lineplot (plt.plot(data['sepal length (cm)'].to_numpy())) to be on the same figure. Is there a way to do so?

Like getting the figure of pairplot, extend it or something?

Update

I am not after adding a plot onto one of the pairplot axis. I want to add additional axis in the figure.

Mentally, think like:

f, ax = plt.subplots(1, 2)

ax[0] = pairplot...
ax[1] = something else...

But since pairplot returns a figure element can we add to it an axis? Change its size to be able to add more axis?

Eric Johnson
  • 682
  • 1
  • 12
  • 1
    The linked answers are not what I'm asking for. I am asking about adding a different axis, not part of pairplot created axis and plot on it. I need to extend the figure with more axis. – Eric Johnson Oct 16 '22 at 15:46
  • 1
    No, `sns.pairplot` is a [`PairGrid`](https://seaborn.pydata.org/generated/seaborn.PairGrid.html#seaborn.PairGrid), which is already a figure; it does not work with `plt.subplots`. seaborn is an API for matplotlib, which automates many of the manual configuration steps. In this case, you would want to construct the plot completely with the matplotlib object oriented API (axes), or create two separate figures. – Trenton McKinney Oct 16 '22 at 18:51
  • 1
    @TrentonMcKinney, I know, my mental trick won't work, hence I asked, can we do something with the figure returned from `pairplot`? Maybe add to it a subplot? – Eric Johnson Oct 16 '22 at 19:17
  • [Here](https://stackoverflow.com/questions/35042255/how-to-plot-multiple-seaborn-jointplot-in-subplot) you could find a discussion about how tricky it is to subplot a `pairplot`. Even though the title is about `jointplot`, you may find a `pairgrid` placement example there. – Nikita Shabankin Oct 22 '22 at 05:37
  • This looks interesting: https://stackoverflow.com/a/70816718/19156305. – Eric Johnson Oct 23 '22 at 07:41

0 Answers0