Is it possible to remove the empty side of the scatter plot matrix?
Question:
I would like to remove the 6 empty boxes on the top right side of the plot(pls see the figure marked in red). I tried few different arguments and it didn’t work.
Here is the code I used.
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
# Example dataset
iris = sns.load_dataset("iris")
# Create PairGrid object with subplots
g = sns.PairGrid(iris, height=1.5, aspect=1.2)
# Create scatter plots on the lower side
g.map_lower(sns.scatterplot)
# Add regression line
g.map_lower(sns.regplot)
# Add histograms
g.map_diag(sns.histplot, kde=True)
# Then include correlation values for each scatter plot.
for i, j in zip(*plt.np.triu_indices_from(g.axes, k=1)):
corr_coef = plt.np.corrcoef(iris.iloc[:, i], iris.iloc[:, j])[0][1]
g.axes[j, i].annotate(f"R = {corr_coef:.2f}", xy=(.1, .9), xycoords=g.axes[j, i].transAxes)
plt.show()
Answers:
I would like to remove the 6 empty boxes on the top right side of the plot(pls see the figure marked in red). I tried few different arguments and it didn’t work.
Here is the code I used.
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
# Example dataset
iris = sns.load_dataset("iris")
# Create PairGrid object with subplots
g = sns.PairGrid(iris, height=1.5, aspect=1.2)
# Create scatter plots on the lower side
g.map_lower(sns.scatterplot)
# Add regression line
g.map_lower(sns.regplot)
# Add histograms
g.map_diag(sns.histplot, kde=True)
# Then include correlation values for each scatter plot.
for i, j in zip(*plt.np.triu_indices_from(g.axes, k=1)):
corr_coef = plt.np.corrcoef(iris.iloc[:, i], iris.iloc[:, j])[0][1]
g.axes[j, i].annotate(f"R = {corr_coef:.2f}", xy=(.1, .9), xycoords=g.axes[j, i].transAxes)
plt.show()