-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

Anurag Dabas
  • 23,866
  • 9
  • 21
  • 41
sarah444
  • 69
  • 1
  • 9
  • Why not use subplots? – Ahmad Anis Apr 02 '21 at 13:17
  • With subplots too i don't know how to plot the graphs side by side. – sarah444 Apr 02 '21 at 13:23
  • Check the example in official docs. https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.subplots.html – Ahmad Anis Apr 02 '21 at 13:25
  • Your [mre] 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](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – wwii Apr 02 '21 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](https://stackoverflow.com/a/45574378/2823755)?? – wwii Apr 02 '21 at 13:47
  • Does [Plot two pandas data frames side by side, each in subplot style](https://stackoverflow.com/questions/49006699/plot-two-pandas-data-frames-side-by-side-each-in-subplot-style) answer your question? – wwii Apr 02 '21 at 13:54
  • Thank you guys for all your help really appreciate it. – sarah444 Apr 02 '21 at 14:00
  • Did one of those links solve your problem? – wwii Apr 02 '21 at 14:52
  • They didn't explain what i was looking for but seriously thanks for your help and time. – sarah444 Apr 02 '21 at 15:06

1 Answers1

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()
r-beginners
  • 31,170
  • 3
  • 14
  • 32
  • 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. – sarah444 Apr 02 '21 at 15:02