Issue
I have a dataframe with 150 columns and 800 rows. Each row represents a sample, which belongs to one of 5 classes. Therefore all samples are pre-classified. I need to create 150 boxplot charts, one for each column (variable), showing the distribution of the data between the classes, for that variable.
I managed to build a code to generate the graphs, but I have to adjust by hand each of the 150 lines to indicate the location of the graph, which is a sequence [0,0], [0,1], [0,2], [1,0], [1,1], [1,2] etc., as well as the y, which could come from a list, but I don't know how to do this.
Below is an example of how it looks like. The first 9 I did by hand, but to do the other 150 would be a lot of work. It should be possible to automate this, I think, but I don't know how. Does anyone have an idea?
fig, axes = plt.subplots(3, 3, figsize=(18, 12))
fig.suptitle('SAPIENS BOXPLOTS')
sns.boxplot(ax=axes[0, 0], data=sapiens, x='classe', y='meanB0')
sns.boxplot(ax=axes[0, 1], data=sapiens, x='classe', y='meanB1')
sns.boxplot(ax=axes[0, 2], data=sapiens, x='classe', y='meanB2')
sns.boxplot(ax=axes[1, 0], data=sapiens, x='classe', y='meanB3')
sns.boxplot(ax=axes[1, 1], data=sapiens, x='classe', y='meanB4')
sns.boxplot(ax=axes[1, 2], data=sapiens, x='classe', y='varB0')
sns.boxplot(ax=axes[2, 0], data=sapiens, x='classe', y='varB1')
sns.boxplot(ax=axes[2, 1], data=sapiens, x='classe', y='varB2')
sns.boxplot(ax=axes[2, 2], data=sapiens, x='classe', y='varB3')
Solution
First, you need to assign columns of sapiens
which will be your y
for each boxplot. Assuming that your first column is classe
and you want to plot every column after that column, this is how you do it:
# get y values
y_labels = sapiens.columns[1:]
Next, decide on figsize
, nrows
, and ncols
for plt.figsize
. And finally start drawing using a loop.
import math
# calculate figure size
ncols = 3
nrows = math.ceil(len(y_labels) / 3)
figsize = (ncols * 6, nrows * 4)
# assign fig and axes
fig, axes = plt.subplots(nrows=nrows, ncols=ncols, figsize=figsize)
fig.suptitle('SAPIENS BOXPLOTS')
# set y_labels index
y_idx = 0
# drawing plots
for axs in axes:
for ax in axs:
sns.boxplot(ax=ax, data=sapiens, x='classe', y=y_labels[y_idx])
## update y_idx
y_idx += 1
Answered By - Gusti Adli
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.