-1

I have this data: https://www.kaggle.com/uciml/mushroom-classification. I have split the dataframe by target column values, and trying to plot dataframes plots side by side to analyze the difference. I have used two for loops for each dataframe. this is my code:

edible = df[df['class']=='e']
poisonous = df[df['class']=='p']

for i in edible:
    fig = px.bar(x=df[i].value_counts().index, y=df[i].value_counts(), text= 
(df[i].value_counts()/len(df[i])*100),title=str(i)+' Edible Mushroom Distribution')
    fig.show()
    
for j in poisonous:
    fig = px.bar(x=df[j].value_counts().index, y=df[j].value_counts(), text=(df[j].value_counts()/len(df[i])*100),title=str(i)+' Poisonous Mushroom Distribution')
fig.show()  

Code is successful in creating all the plots but there are two problems:

  1. It takes two much time to show all plots
  2. I need the columns to be side by side for analysis, I don't know how can i achieve that.

Can someone please help me. Thanks

9
  • Why not use subplots? Commented Apr 2, 2021 at 13:17
  • With subplots too i don't know how to plot the graphs side by side. Commented Apr 2, 2021 at 13:23
  • Check the example in official docs. matplotlib.org/stable/api/_as_gen/… Commented Apr 2, 2021 at 13:25
  • Your minimal reproducible example should include example data - it can be fake data that you made up as long as it illustrates the problem. How to make good reproducible pandas examples Commented Apr 2, 2021 at 13:39
  • You want edible[0] and poisonous[0] to be plotted side-by-side on the first row? Then edible[1] and poisonous[1] side-by-side in the next row? ... Or like this https://stackoverflow.com/a/45574378/2823755?? Commented Apr 2, 2021 at 13:47

1 Answer 1

0

Subplots in plotly are created by using 'make_subplots'. The first step is to determine the skeleton of the plural graph (the size of the matrix), and then specify the type of graph. After that, you can use your own code. You can also add titles and decorations by referring to the official reference.

import plotly.graph_objects as go
from plotly.subplots import make_subplots

edible = df[df['class']=='e']
poisonous = df[df['class']=='p']

fig = make_subplots(rows=len(edible.columns), # len(edible.columns)
                    cols=2,
                    column_widths=[0.5, 0.5],
                    specs=[[{'type':'bar'},{'type':'bar'}] for i in range(len(edible.columns))])

for i,col in enumerate(edible):
    fig.add_trace(
        go.Bar(x=edible[col].value_counts().index, y=edible[col].value_counts(), ), row=i+1, col=1)
    fig.add_trace(
        go.Bar(x=poisonous[col].value_counts().index, y=poisonous[col].value_counts(), ), row=i+1, col=2)

fig.update_layout(height=2000)
fig.show()
Sign up to request clarification or add additional context in comments.

1 Comment

Actually I did use the make.subplot but i was using two loops and didn't know how to assign rows and columns to each loop. Thank you soo much really appreciate your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.