Skip to content
Competition - crowdfunding marketing
  • AI Chat
  • Code
  • Report
  • Spinner
    import pandas as pd
    import plotly.express as px
    from plotly.subplots import make_subplots
    import plotly.graph_objects as go
    
    df= pd.read_csv('./data/crowdfunding.csv')
    
    # total donation amount
    total_amount = int(df.amount.sum())
    
    # total donors
    total_donors = int(len(df.amount))
    
    # highest donation amount
    highest_amount = int(df.amount.max())
    
    # median donation amount
    median_amount = int(df.amount.median())
    
    
    stats = go.Figure()
    
    stats.add_trace(go.Indicator(
        mode = "number",
        title = {'text': "Total Funds Raised"},
        number = {'prefix': "$"},
        value = total_amount,
        domain = {'row': 0, 'column': 0}))
    
    stats.add_trace(go.Indicator(
        mode = "number",
        title = {'text': "Number of Donations"},
        value = total_donors,
        domain = {'row': 0, 'column': 1}))
    
    stats.add_trace(go.Indicator(
        mode = "number",
        number = {'prefix': "$"},
        title = {'text': "Median Donation"},
        value = median_amount,
        domain = {'row': 1, 'column': 0})) 
    
    stats.add_trace(go.Indicator(
        mode = "number",
        number = {'prefix': "$"},
        title = {'text': "Highest Donation"},
        value = highest_amount,
        domain = {'row': 1, 'column': 1}))         
    
    stats.update_layout(
        grid = {'rows': 2, 'columns': 2, 'pattern': "independent"},
        plot_bgcolor='#333',
        paper_bgcolor='#333',
        autosize=True,margin={'t': 50,'l':100,'b':50,'r':100},
        height=300,
        #width=500,
        template = {'data' : {'indicator': [{
            'title': {"font_color":"#ffffff","font_size":18, "font_family":"Lato, sans-serif"},
            'mode' : "number+delta+gauge",
            'number' : {"font_color":"#ffffff","font_size":52}}]  
         }})
    
    stats.show()
    # donut chart
    fig_donut = px.pie(df, names='device', hole=0.7, title='<b>Most used device by donators',
                       color_discrete_sequence=['#e2e2e2', '#b1eb1e'])
    
    fig_donut.update_traces(hovertemplate=None, textposition='outside', textinfo='percent+label', rotation=90)
    
    fig_donut.update_layout(margin=dict(t=100, b=30, l=0, r=0),
                            showlegend=False,
                            plot_bgcolor='#333',
                            height=350,
                            #width=600,
                            paper_bgcolor='#333',
                            title_font=dict(size=30,
                            color='#ffffff',
                            family="Lato, sans-serif"),
                            font=dict(size=17, color='#ffffff'),
                            hoverlabel=dict(bgcolor="#444", font_size=13, font_family="Lato, sans-serif"))                      
    fig_donut.show()
    
    fig_top3 = px.histogram(df, 
        y='category',
        x="amount",
        title='<b>The biggest categorie donations',
        color="category",
        color_discrete_map={
                        "Sports": "#b1eb1e",
                        "Games": "#b1eb1e",
                        "Technology": "#b1eb1e",
                        "Environment": "#e2e2e2",
                        "Fashion": "#e2e2e2",
                        }
    )
    fig_top3.update_xaxes(visible=False,range = [150000, 170000])
    fig_top3.update_yaxes(showgrid=False, ticksuffix='  ', showline=False,categoryorder='total ascending')
    fig_top3.update_traces(hovertemplate=None, marker=dict(line=dict(width=0)))
    fig_top3.update_layout(margin=dict(t=85, b=20, l=70, r=40),
                           hovermode="y unified", 
                           showlegend=False,
                           xaxis_title=' ',
                           yaxis_title=" ",
                           plot_bgcolor='#333',
                           paper_bgcolor='#333',
                           title_font=dict(size=30,
                           color='#ffffff',
                           family="Lato, sans-serif"),
                           #width=700,
                           #height=350,
                           font=dict(color='#ffffff', size=17),
                           hoverlabel=dict(bgcolor='#333', font_size=13, font_family="Lato, sans-serif"))
    fig_top3.show()
    
    # Age proportion per device
    fig_age_p = px.histogram(df,
        y='age',
        histnorm="probability density",
        title='<b>Proportion of donations by age and device',
        color="device",
        barmode="group",
        labels={"device":""},
        color_discrete_sequence=['#e2e2e2', '#b1eb1e'],
        category_orders=dict(age=['18-24', '25-34', '35-44', '45-54', '55+']),
    )
    fig_age_p.update_xaxes(visible=False)
    fig_age_p.update_yaxes(showgrid=False, ticksuffix='  ', showline=False)
    fig_age_p.update_traces(hovertemplate=None, marker=dict(line=dict(width=0)))
    fig_age_p.update_layout(margin=dict(t=85, b=20, l=70, r=40),
                           hovermode="y unified",
                           showlegend=True,
                           xaxis_title="",
                           yaxis_title="",
                           plot_bgcolor='#333',
                           paper_bgcolor='#333',
                           title_font=dict(size=30, color='#ffffff', family="Lato, sans-serif"),
                           #width=700,
                           #height=450,
                           legend=dict(orientation="h", yanchor="bottom", y=1, xanchor="center", x=0.5),
                           font=dict(color='#ffffff', size=15),
                           hoverlabel=dict(bgcolor='#333', font_size=13, font_family="Lato, sans-serif"))
    fig_age_p.show()
    # Age distribution
    # making a copy of df
    dff = df.copy()
    
    # making 2 df one for iOS and another for android with category 
    df_ios = dff[dff['device']=='iOS'][['age', 'device']].rename(columns={'device':'ios'})
    df_android = dff[dff['device']=='android'][['age', 'device']].rename(columns={'device':'android'})
    
    
    df_ios = pd.DataFrame(df_ios.age.value_counts()).reset_index().rename(columns={'index':'ios'})
    df_ios['age_final'] = df_ios['age'] 
    # making age column value negative
    df_ios['age'] *= -1
    
    df_android = pd.DataFrame(df_android.age.value_counts()).reset_index().rename(columns={'index':'android'})
    
    fig_age_n = make_subplots(rows=1, cols=2, specs=[[{}, {}]], shared_yaxes=True, horizontal_spacing=0)
    # bar plot for iOS
    fig_age_n.append_trace(go.Bar(x=df_ios.age, y=df_ios.ios, orientation='h', showlegend=True, 
                            text=df_ios.age_final, name='iOS', marker_color='#e2e2e2'), 1, 1)
    # bar plot for android
    fig_age_n.append_trace(go.Bar(x=df_android.age, y=df_android.android, orientation='h', showlegend=True, text=df_android.age,
                            name='Android', marker_color='#b1eb1e'), 1, 2)
    fig_age_n.update_xaxes(showgrid=False,visible=False)
    fig_age_n.update_yaxes(showgrid=False, ticksuffix=' ', showline=False,categoryorder='array', categoryarray= ['55+','45-54','35-44','25-34','18-24'])
    fig_age_n.update_traces(hovertemplate=None, marker=dict(line=dict(width=0)),texttemplate='%{text:.2s}')
    fig_age_n.update_layout(title='<b>Number of donations across age and device',
                      margin=dict(t=100, b=0, l=70, r=40),
                      hovermode="y unified",
                      xaxis_title="",
                      yaxis_title="",
                      plot_bgcolor='#333',
                      paper_bgcolor='#333',
                      title_font=dict(size=30, color='#ffffff', family="Lato, sans-serif"),
                      font=dict(color='#ffffff', size=15),
                      legend=dict(orientation="h", yanchor="bottom", y=1, xanchor="center", x=0.5),
                      hoverlabel=dict(bgcolor='#333', font_size=13, font_family="Lato, sans-serif"))     
    fig_age_n.show()   
    
    # Amount distribution
    fig_age = px.violin(df, x="amount", color ="device",violinmode="overlay",facet_col="age",
        color_discrete_sequence=['#e2e2e2', '#b1eb1e'],
        labels=dict(device=""),
        category_orders=dict(age=['18-24', '25-34', '35-44', '45-54', '55+']),
        facet_col_spacing=0
    )
    fig_age.update_traces(orientation='h', side='positive', meanline_visible=True, line_width=5, opacity=0.9,
    points=False)
    fig_age.update_xaxes(showgrid=False,zeroline=False, tickvals=["10","40","80"],title="",range=[0,100])
    fig_age.update_yaxes(showgrid=False,zeroline=False,range=[0.01,0.25])
    fig_age.update_layout(title='<B>Amount of donations distribution by age and device',
                      margin=dict(t=140, b=10, l=0, r=30), #l=left,r=ight,t=top,b=botton
                      width=450,
                      #height=400,
                      hovermode="y unified",
                      xaxis_title="",
                      yaxis_title="",
                      plot_bgcolor='#333',
                      paper_bgcolor='#333',
                      title_font=dict(size=30, color='#ffffff', family="Lato, sans-serif"),
                      font=dict(color='#ffffff', size=15),
                      legend=dict(orientation="h", yanchor="bottom", y=1.1, xanchor="center", x=0.5),
                      hoverlabel=dict(bgcolor='#333', font_size=13, font_family="Lato, sans-serif"))
    fig_age.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))        
    fig_age.show()