Bring Drag & Drop to Dash with Dashboard Engine. 💫 Learn how at our next webinar!

How to subplots with Plotly?

Hello guys, I’m new to plotly and I tried to create a dashboard with different plots.

Now the code doesn’t print a unique dashboard. This is my code, the code like this works perfectly but the output is 4 different HTMLs.

The pie has more implementation because checks some variables and NA values.

Database

  • out.csv ( out.csv is an export from a DB, but in my regular code I’m using a connection with MongoDB)

OS System and libraries

  • Ubuntu 16
  • Plotly express
  • Pandas and NumPy
import json
import pandas as pd
import os
import sys
import numpy as np
import plotly
import plotly.express as px    

df = pd.read_csv('out.csv')
    
fig = px.scatter(df, x='timestamp', y ='class', title='Detection Time Series', color='class', symbol='class') #size = df.index

fig.update_xaxes(rangeslider_visible=True)

fig_1 = px.histogram(df, x="class", color ='class')

#Count Time Series with class

fig_3 = px.line(df, x='timestamp', title='Detection Time Series with class', color='class', labels=df['class'])

fig_3.update_xaxes(rangeslider_visible=True)


def pie_count(data, field="class"):

    data[field] = data[field].replace('', 'NA')      # Replace blanks with NA (Not Available)

    data= data[field].value_counts().to_frame()     # Create a new Dataframe with count of countries

    total = data[field].sum()
    data['percentage'] = 100 * data[field]/total     # Create a new column of percentage

    percent_limit=0.5
    otherdata = data[data['percentage'] < percent_limit]  # Combine all data which is less than limit
    maindata = data[data['percentage'] >= percent_limit]
    others = otherdata['percentage'].sum()

    data = maindata
    other_label = "Others(<" + str(percent_limit) + "% each)"
    data.loc[other_label] = pd.Series({field:otherdata[field].sum()})
    
    labels = data.index.tolist()                     # List of Country Names
    datavals = data[field].tolist()                  # List of medal count
    title = "Pie Chart Percentage"

    trace=plotly.graph_objs.Pie(labels=labels,values=datavals)

    layout = plotly.graph_objs.Layout(
        title=title,
        height=600,
        margin=plotly.graph_objs.layout.Margin(l=0, r=200, b=100, t=100, pad=4)   # Margins - Left, Right, Top Bottom, Padding
        )
    
    fig_2 = plotly.graph_objs.Figure(data=[trace], layout=layout)
    fig_2.update_traces(textposition='inside', textinfo='percent+label')
    fig_2.show()

pie_count(df)
fig_1.show()
fig_3.show()
fig.show()

print(data)

This is my first post, please be nice. :heart_eyes: :smiling_face_with_three_hearts:

Feel free to propose different implementation and correction,

Thanks for your attention,

Best regards,

:eagle: :heart:

You don’t need to use different fig.show() for each chart if you trying to plot them as subplot.
Look here for examples.