I would like to be able to plot a 4 x 5 (rows x columns) seaborn
histogram plots using a for loop
. For the code I have below, the plots come out individually and not together in 1 plot. This is what I have so far:
from sklearn.datasets import make_classification
import seaborn as sns
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
X_train,y_train = make_classification(n_samples=500,
n_features=20,
n_informative=9,
n_redundant=0,
n_repeated=0,
n_classes=10,
n_clusters_per_class=1,
class_sep=9,
flip_y=0.2,
#weights=[0.5,0.5],
random_state=17)
sns.set_style('darkgrid')
coeff_to_analyze = np.arange(0,20,1)
rows = 4
cols = 5
N_BINS = 60
fig, axes = plt.subplots(rows, cols, figsize=(45,12))
for i in coeff_to_analyze:
ax = plt.subplot(rows, cols, i+1)
sns.displot(X_train[i, :], bins=60, kde=True)
ax.set_title(f'Coefficient {i}')
fig.tight_layout()
plt.savefig(f'Histogram_test.pdf', bbox_inches='tight')
plt.show()
Note that the links provided here seems to state that I have to convert my data to a pandas
dataframe. Can I get the plots to show up without converting my data to a pandas
dataframe?
I can get the plots to show up correctly if it is NOT a seaborn
histogram. However, I am having trouble for the plots to show up if they are seaborn
histogram plots