I have a dataset size similar for the following code:
import numpy as np
from sklearn import datasets
import matplotlib.pyplot as plt
from plotly.subplots import make_subplots
import plotly.graph_objs as go
from plotly.offline import plot
X, y = datasets.make_classification(n_samples=300, n_features=70, n_classes=3, n_redundant=0,
n_clusters_per_class=1, weights=[0.5, 0.3, 0.2], random_state=42)
data=[]
for i in range(1,X.shape[1]+1,5):
data.append(go.Box(y = X[i],
name = i,
showlegend = False))
plot(data)
i am not getting the desired result, I want to plot every 5 columns alone in each figure
hopefully my question is understandable
You can use make_subplots
to create each 5 figures along with a for loop.
Full code:
import numpy as np
from sklearn import datasets
import matplotlib.pyplot as plt
from plotly.subplots import make_subplots
import plotly.graph_objs as go
from plotly.offline import plot
X, y = datasets.make_classification(n_samples=300, n_features=70, n_classes=3, n_redundant=0,
n_clusters_per_class=1, weights=[0.5, 0.3, 0.2], random_state=42)
data=[]
count = 1
fig = make_subplots(rows=1, cols=int(X.shape[1]/5))
for i in range(1,X.shape[1]+1):
fig.add_trace(go.Box(y = X[i],
name = i,
showlegend = False), row=1, col=count)
if i % 5 == 0:
count += 1
plot(fig)
**Output:**
[![enter image description here][1]][1]
To save every 5 plots in the one figure and export it use the following code:
import numpy as np
from sklearn import datasets
import matplotlib.pyplot as plt
from plotly.subplots import make_subplots
import plotly.graph_objs as go
from plotly.offline import plot
import plotly.io as pio
X, y = datasets.make_classification(n_samples=300, n_features=70, n_classes=3, n_redundant=0,
n_clusters_per_class=1, weights=[0.5, 0.3, 0.2], random_state=42)
colors = ['rgba(93, 164, 214, 0.5)', 'rgba(255, 144, 14, 0.5)', 'rgba(44, 160, 101, 0.5)',
'rgba(255, 65, 54, 0.5)', 'rgba(207, 114, 255, 0.5)']
image_count = 1
fig = go.Figure()
for i in range(1,X.shape[1]+1):
fig.add_trace(go.Box(y = X[i],
name = i,
showlegend = False,
boxpoints='all',
jitter=0.5,
whiskerwidth=0.2,
fillcolor=colors[i%5],
marker_size=2,
line_width=1))
fig.update_layout(
title=f'Figure {image_count}',
yaxis=dict(
autorange=True,
showgrid=True,
zeroline=True,
gridcolor='rgb(255, 255, 255)',
gridwidth=1,
zerolinecolor='rgb(255, 255, 255)',
zerolinewidth=2,
),
margin=dict(
l=40,
r=30,
b=80,
t=100,
),
paper_bgcolor='rgb(243, 243, 243)',
plot_bgcolor='rgb(243, 243, 243)',
)
if i % 5 == 0:
fig.write_html(f"image{image_count}.html")
fig = go.Figure()
image_count += 1