Example #1
0
# Initialize a figure with ff.create_table(table_data)
fig = ff.create_table(table_data, height_constant=60)

# Add graph data
teams = [
    'Montréal Canadiens', 'Dallas Stars', 'NY Rangers', 'Boston Bruins',
    'Chicago Blackhawks', 'Ottawa Senators'
]
GFPG = [3.54, 3.48, 3.0, 3.27, 2.83, 3.18]
GAPG = [2.17, 2.57, 2.0, 2.91, 2.57, 2.77]

# Make traces for graph
trace1 = go.Bar(x=teams,
                y=GFPG,
                xaxis='x2',
                yaxis='y2',
                marker=dict(color='#0099ff'),
                name='Goals For<br>Per Game')
trace2 = go.Bar(x=teams,
                y=GAPG,
                xaxis='x2',
                yaxis='y2',
                marker=dict(color='#404040'),
                name='Goals Against<br>Per Game')

# Add trace data to figure
fig.add_traces([trace1, trace2])

# initialize xaxis2 and yaxis2
fig['layout']['xaxis2'] = {}
fig['layout']['yaxis2'] = {}
Example #2
0
GROUP BY country_name
'''


X = []
Y = []



cursor.execute(query)
for c, number in cursor.fetchall():
    X.append(c)
    Y.append(number)


fig = go.Figure(data=[go.Bar(x=X, y=Y)],
                layout=go.Layout(xaxis={'title': 'Країни'},
                yaxis={'title': 'Кількість фільмів'}))
fig.update_layout(title='Кількість фільмів та шоу по країнам')
url1 = py.plot(fig, filename='query1.html')


#SECOND QUERY


query = '''
SELECT genres_name AS genres, round(COUNT(name) * 100 / (SELECT SUM(COUNT(genres_name)) FROM movies_vies GROUP BY genres_name), 1) AS percentage
FROM movies_vies
GROUP BY genres_name
'''
Example #3
0
authandavgratingg = authandavgratingg.reset_index('authors')
authandavgratingg.rename(columns={
    'average_rating': 'Average Rating',
    'authors': 'Authors'
},
                         inplace=True)

#Used to find avg ratings to be included in the bar chart
pg = authandavgratingg[authandavgratingg['Authors'] == 'James Patterson']

data = go.Bar(x=authorswithmostbooks.authors,
              y=authorswithmostbooks['Number of Books Written'],
              marker=dict(color='#ffcdd2'),
              hovertext=[
                  '4.147 Avg Rating', '3.974 Avg Rating', '4.187 Avg Rating',
                  '3.779 Avg Rating', '3.989 Avg Rating', '3.717 Avg Rating',
                  '4.039 Avg Rating', '3.742 Avg Rating', '3.962 Avg Rating',
                  '3.906 Avg Rating'
              ])
layout = go.Layout(xaxis=dict(title='Author'),
                   yaxis=dict(title='Number of Books'))

with st.beta_expander(
        'Click here to view the top ten most published authors according to Goodreads data'
):
    bookswrittenfig = go.Figure(data=data, layout=layout)
    st.markdown('#')
    st.write(
        'Hover over the bar graph to view the authors\' average rating. Is this a case of quantity over quality? You decide'
    )
Example #4
0
    fig=fig.add_trace(
        go.Scatter(
            y=df[countries[i]],
            x=df.index,
            mode="markers",
            name=countries[i],
            marker=dict(
                color=colours[i]
            )
        )
    )
fig.add_trace(
    go.Bar(
        y=df["Average"],
        x=df.index,
        name="General Average",
        error_y=dict(
            type="data",
            array=df["ConfInt"])
    )
)
fig.update_layout(
    hovermode="compare"
)
fig.show()


import chart_studio.plotly as py
import chart_studio
chart_studio.tools.set_credentials_file(username='******', api_key='S2uZaCyuN8maE8Mq5EuJ')

py.plot(fig, filename = 'African Job Data', auto_open=True)
Example #5
0
namez = []  ## create empty list
for x in dfs.describe().columns:  ## get column names
    namez.append(x)  ### append to a list namez

ys = dfs.describe().values[
    1:]  ### get all arrays of values except for the first which is count
x1 = dfs.describe().columns[3:]
texts = [
    'count', 'mean', 'std', 'min', '25%', '50%', '75%', 'max'
]  ### we will use these labels to show information when hovering on the chart
fig = go.Figure(
)  ### create an empty figure so that we can then append some data into it
for ll in range(0, len(texts)):
    fig.add_trace((go.Bar(x=x1,
                          y=ys[ll - 1],
                          name=texts[ll],
                          text=texts[ll],
                          hoverinfo='text+y')
                   ))  ## add a new trace on hte graph for each of the columns
fig.update_layout(
    barmode='group',
    title="Air Pollution <br> Using Decribe Data")  ## call barmode as group
st.write("the data description plot")
st.write(df.describe())
st.write(fig)

process_cols = [[
    'x', 'y', 'population', 'dist-mroads', 'dist-setl', 'dist-coast',
    'dist-forest', 'slope', 'elevation', 'dayofweek', 'sin_day', 'cos_day',
    'sin_year', 'cos_year', 'TEMP', 'DEW', 'SKY', 'VIS', 'ATM', 'Wind-Rate',
    'sin_wind', 'cos_wind'
def plot_totalreview_google_version(dataframe):
  df_google = dataframe

  #df_google['version'] = df_google['version'].apply(LooseVersion)
  #df_google.sort_values(by='version',inplace=True)
  #df_google.reset_index(drop=True, inplace=True)

  #split review
  neg_google = df_google[df_google['rating'] <= 3].groupby(['version']).count(
  ).reset_index().drop(['rating', 'at'], axis=1).rename(columns={"review": "negative_review"})
  pos_google = df_google[df_google['rating'] > 3].groupby(['version']).count(
  ).reset_index().drop(['rating', 'at'], axis=1).rename(columns={"review": "positive_review"})

  #merge it
  df = pd.merge(neg_google, pos_google, on=[
                'version'], how='outer')
  df.fillna({'positive_review': 0, 'negative_review': 0}, inplace=True)
  list_version = df['version'].tolist()
  list_version.sort(key=LooseVersion)
  dict_version = []
  for i in range(len(list_version)):
    dict_version.append(i)
  sorted_version = pd.DataFrame()
  sorted_version['dict'] = dict_version
  sorted_version['version'] = list_version
  df = pd.merge(df, sorted_version, on=['version'], how='right')
  df.sort_values(by=['dict'], inplace=True, ignore_index=True)

  # plot
  plot = go.Figure(data=[go.Bar(
      name='Positive Review',
      x=df['version'],
      y=df['positive_review'],
      marker_color='rgb(52,186,83)'
  ),
      go.Bar(
      name='Negative Review',
      x=df['version'],
      y=df['negative_review'],
      marker_color='rgb(234,67,53)'
  )
  ])
  plot.update_layout(barmode='stack')
  plot.update_layout(legend=dict(
      orientation="h",
      yanchor="bottom",
      y=1.02,
      xanchor="right",
      x=0.93),
      title={
      'text': '<span style="font-size: 25px;">Google User Review across Versions</span>',
      'y': 0.97,
      'x': 0.45,
      'xanchor': 'center',
      'yanchor': 'top'},
      paper_bgcolor="#ffffff",
      plot_bgcolor="#ffffff",
      width=1500, height=700
  )
  # Add image
  plot.add_layout_image(
      dict(
          source="https://res-2.cloudinary.com/crunchbase-production/image/upload/c_lpad,h_170,w_170,f_auto,b_white,q_auto:eco/k5gw7klohl445zwak3mc",
          xref="paper", yref="paper",
          x=0.92, y=1.04,
          sizex=0.1, sizey=0.1,
          xanchor="right", yanchor="bottom"
      )
  )
  # Set x-axis title
  plot.update_xaxes(title_text="Version")
  # Set y-axes titles
  plot.update_yaxes(title_text="Number of Reviews", showgrid=False)
  graphJSON = json.dumps(plot, cls=plotly.utils.PlotlyJSONEncoder)
  return graphJSON
import pandas as pd
import plotly.graph_objects as go
import plotly.offline as pyo

df = pd.read_csv('../dataset/2018WinterOlympics.csv')

trace1 = go.Bar(
    x=df['NOC'],  # NOC stands for National Olympic Committee
    y=df['Gold'],
    name='Gold',
    marker=dict(color='#FFD700')  # set the marker color to gold
)
trace2 = go.Bar(
    x=df['NOC'],
    y=df['Silver'],
    name='Silver',
    marker=dict(color='#9EA0A1')  # set the marker color to silver
)
trace3 = go.Bar(
    x=df['NOC'],
    y=df['Bronze'],
    name='Bronze',
    marker=dict(color='#CD7F32')  # set the marker color to bronze
)
data = [trace1, trace2, trace3]

layout = go.Layout(title='2018 Winter Olympic Medals by Country')
fig = go.Figure(data=data, layout=layout)
pyo.plot(fig)
Example #8
0
def totGoalsLine(statTable, val):
    y = statTable['Total_Goals'].iloc[:-2]
    x = statTable['Season_Season'].iloc[:-2].drop_duplicates()

    # Goals to games ratio
    totGames = int(statTable['Total_Apps'].tail(1))
    totGoals = int(statTable['Total_Goals'].tail(1))
    goalGames = round(totGoals / totGames, 2)

    # League-only stats
    # totGamesL = int(statTable['League_Apps'].iloc[-1])
    # totGoalsL = int(statTable['League_Goals'].iloc[-1])
    # goalGamesL = round(totGoalsL/totGamesL,2)

    # Goals per game ratio
    goalGames = round(totGoals / totGames, 2)
    goalPerGame = pd.DataFrame(
        statTable['Season_Season'].iloc[:-2].drop_duplicates())
    goalPerGame['Goals per Game'] = goalGames

    # Create figure
    fig = make_subplots(specs=[[{"secondary_y": True}]])

    # If goals is chosen in dropdown;
    if val == "Goals":
        fig.add_trace(
            go.Bar(
                y=y,
                x=x,
                marker=dict(color='rgb(22, 26, 72)',
                            # line=dict(
                            # color='MediumPurple',
                            # width=2
                            # )
                            ),
                name="Goals per Season"))
        fig.add_trace(
            go.Scatter(y=goalPerGame['Goals per Game'],
                       x=x,
                       name="Goals per Game"),
            secondary_y=True,
        )

        fig.update_layout(title='Total Goals per Season',
                          xaxis_title="Season",
                          yaxis_title="Goals per Season",
                          font_color="white",
                          plot_bgcolor='rgb(0, 12, 39)',
                          paper_bgcolor='rgb(0, 12, 39)',
                          xaxis={'showgrid': False},
                          yaxis={'showgrid': False})

        return fig

    # If games played is chosen in dropdown;
    elif val == "Games Played":
        fig.add_trace(
            go.Bar(
                y=statTable['Total_Apps'].iloc[:-2],
                x=x,
                marker=dict(color='rgb(22, 26, 72)',
                            # line=dict(
                            # color='MediumPurple',
                            # width=2
                            # )
                            ),
                name="Games Played per Season",
            ))
        fig.add_trace(
            go.Scatter(y=statTable['Total_Goals'].iloc[:-2],
                       x=x,
                       name="Goals per Season"),
            secondary_y=True,
        )

        fig.update_layout(title='Games Played per Season',
                          xaxis_title="Season",
                          yaxis_title="Games Played per Season",
                          font_color="white",
                          plot_bgcolor='rgb(0, 12, 39)',
                          paper_bgcolor='rgb(0, 12, 39)',
                          xaxis={'showgrid': False},
                          yaxis={'showgrid': False})

        return fig
# Other Libraries
from urllib.request import urlopen
import json

import os
import pathlib
# us-states dataset
APP_PATH = str(pathlib.Path(__file__).parent.resolve().parent.resolve())

df = pd.read_csv(os.path.join(APP_PATH, os.path.join("data", "us-states.csv")))

fig = go.Figure()
fig.add_trace(
    go.Bar(x=df['state'],
           y=df['cases'],
           name='Cases by State',
           marker_color='rgb(55, 83, 109)'))
fig.add_trace(
    go.Bar(x=df['state'],
           y=df['deaths'],
           name='Deaths by State',
           marker_color='rgb(26, 118, 255)'))

fig.update_layout(
    title='Cases and Death Count Per State',
    xaxis_tickfont_size=14,
    yaxis=dict(
        title='State',
        titlefont_size=16,
        tickfont_size=14,
    ),
Example #10
0
def content2(app):

    ################## Data Analysis ##################

    #2019
    a = pd.read_csv(
        'https://raw.githubusercontent.com/R-Akira/US-Flight-Data/master/Analysis/Jan_2019_ontime.csv'
    )
    b = pd.read_csv(
        'https://raw.githubusercontent.com/R-Akira/US-Flight-Data/master/Analysis/Feb_2019_ontime.csv'
    )

    #2020
    c = pd.read_csv(
        'https://raw.githubusercontent.com/R-Akira/US-Flight-Data/master/Analysis/Jan_2020_ontime.csv'
    )
    d = pd.read_csv(
        'https://raw.githubusercontent.com/R-Akira/US-Flight-Data/master/Analysis/Feb_2020_ontime.csv'
    )

    # Number of flights by airline

    f_num = j19_df.groupby('OP_CARRIER')[['DEST']].count()
    f_num = f_num.sort_values('DEST', ascending=False)
    f_num.reset_index(level=0, inplace=True)

    f_num_fev = f19_df.groupby('OP_CARRIER')[['DEST']].count()
    f_num_fev = f_num_fev.sort_values('DEST', ascending=False)
    f_num_fev.reset_index(level=0, inplace=True)

    f_num20 = j20_df.groupby('OP_CARRIER')[['DEST']].count()
    f_num20 = f_num20.sort_values('DEST', ascending=False)
    f_num20.reset_index(level=0, inplace=True)

    f_num_fev_20 = f20_df.groupby('OP_CARRIER')[['DEST']].count()
    f_num_fev_20 = f_num_fev_20.sort_values('DEST', ascending=False)
    f_num_fev_20.reset_index(level=0, inplace=True)

    # Number of delayed flights by airline

    f_del = j19_df.groupby('OP_CARRIER')[['DEP_DEL15']].sum()
    f_del = f_del.sort_values('DEP_DEL15', ascending=False)
    f_del.reset_index(level=0, inplace=True)

    f_del_fev = f19_df.groupby('OP_CARRIER')[['DEP_DEL15']].sum()
    f_del_fev = f_del_fev.sort_values('DEP_DEL15', ascending=False)
    f_del_fev.reset_index(level=0, inplace=True)

    f_del20 = j20_df.groupby('OP_CARRIER')[['DEP_DEL15']].sum()
    f_del20 = f_del20.sort_values('DEP_DEL15', ascending=False)
    f_del20.reset_index(level=0, inplace=True)

    f_del_fev_20 = f20_df.groupby('OP_CARRIER')[['DEP_DEL15']].sum()
    f_del_fev_20 = f_del_fev_20.sort_values('DEP_DEL15', ascending=False)
    f_del_fev_20.reset_index(level=0, inplace=True)

    # Number of cancelled flights by airline

    f_cancel = j19_df.groupby('OP_CARRIER')[['CANCELLED']].sum()
    f_cancel.reset_index(level=0, inplace=True)

    f_cancel_fev = f19_df.groupby('OP_CARRIER')[['CANCELLED']].sum()
    f_cancel_fev.reset_index(level=0, inplace=True)

    f_cancel20 = j20_df.groupby('OP_CARRIER')[['CANCELLED']].sum()
    f_cancel20.reset_index(level=0, inplace=True)

    f_cancel_fev_20 = f20_df.groupby('OP_CARRIER')[['CANCELLED']].sum()
    f_cancel_fev_20.reset_index(level=0, inplace=True)

    # Number of diverted flights by airline

    f_div = j19_df.groupby('OP_CARRIER')[['DIVERTED']].sum()
    f_div.reset_index(level=0, inplace=True)

    f_div_fev = f19_df.groupby('OP_CARRIER')[['DIVERTED']].sum()
    f_div_fev.reset_index(level=0, inplace=True)

    f_div20 = j20_df.groupby('OP_CARRIER')[['DIVERTED']].sum()
    f_div20.reset_index(level=0, inplace=True)

    f_div_fev_20 = f20_df.groupby('OP_CARRIER')[['DIVERTED']].sum()
    f_div_fev_20.reset_index(level=0, inplace=True)

    #delayed, cancelled, diverted percentage

    f_del_perc = f_del['DEP_DEL15'] / f_num['DEST']
    f_del_perc_fev = f_del_fev['DEP_DEL15'] / f_num_fev['DEST']
    f_del_perc20 = f_del20['DEP_DEL15'] / f_num20['DEST']
    f_del_perc_fev_20 = f_del_fev_20['DEP_DEL15'] / f_num_fev_20['DEST']

    ##################  Figures ##################
    #Figure 1
    overall_figure = make_subplots(
        rows=2,
        cols=2,
        subplot_titles=("January 2019", "February 2019", "January 2020",
                        "February 2020"))

    overall_figure.add_trace(go.Bar(x=f_del.OP_CARRIER,
                                    y=f_del_perc,
                                    marker_color='gray'),
                             row=1,
                             col=1)
    overall_figure.add_trace(go.Bar(x=f_del.OP_CARRIER,
                                    y=f_del_perc_fev,
                                    marker_color='gray'),
                             row=1,
                             col=2)
    overall_figure.add_trace(go.Bar(x=f_del.OP_CARRIER,
                                    y=f_del_perc20,
                                    marker_color='gray'),
                             row=2,
                             col=1)
    overall_figure.add_trace(go.Bar(
        x=f_del.OP_CARRIER,
        y=f_del_perc_fev_20,
        marker_color='gray',
    ),
                             row=2,
                             col=2)

    overall_figure.update_layout(
        title_text="Delayed Airplanes",
        title_x=0.5,
        plot_bgcolor='white',
        paper_bgcolor='white',
        showlegend=False,
    )

    overall_figure.update_xaxes(showline=True, linewidth=2, linecolor='black')
    overall_figure.update_yaxes(range=[0, 0.2],
                                showline=True,
                                linewidth=2,
                                linecolor='black')

    ################## Texts ##################
    text_3 = '''
      
    Analyzing late arrivals we can identify an important drop in delays from 2019 to 2020, with the following changes:  \n
    - January 2019: 18.59%  \n
   \n
    - January 2020: 13.73%  \n
   \n
    - February 2019: 23.14%  \n
   \n
    - February 2020: 14.90%  \n

    The reasons for this drop in delays are not clear, since the number of flights remained at similar levels in both months in 2019 and 2020.  \

    Analyzing the distribution of delays(%) per company we get the following:  \n


    '''

    text_4 = '''

    It’s extremely surprising to see that delays(%) gives us an almost uniform distribution on all periods being analyzed. This might indicate that flight delays are being managed and companies are being helped or penalized in order to spread the risk across all airlines.  \

    If that is the case it will be useless to develop a predictive model, since the target variable will be mostly impacted by deliberate decisions.

    '''

    ################## Layout ##################

    a = html.Div(children=[
        dcc.Markdown(children=text_3,
                     style={
                         'textAlign': 'left',
                         'marginLeft': 20
                     }),
        dcc.Graph(id='graph1', figure=overall_figure),
        dcc.Markdown(children=text_4,
                     style={
                         'textAlign': 'left',
                         'marginLeft': 20
                     })
    ])

    return a
def app():
    st.title('Analyses du site NH Hôtels')
    st.write(
        "Dans cette page vous allez trouver des analyses sur le site NH Hôtels, via lequel on peut chercher 357 hôtels parmis 29 pays dans le monde entier."
    )

    st.header("Objectif des analyses")
    st.write(
        "Récemment, le changement climatique, tel que le réchauffement de la planète, est devenu plus visible pour le public. "
        "Nous pensons qu'il est nécessaire que les entreprises de tous les domaines, y compris le tourisme, prennent plus sérieusement en compte l'environnement. "
        "Dans le site NH Hôtels, il y a des hôtels qui mettent en avant leur respect de l'environements. "
        "Dans cette page vous trouverez des analyses sur les hôtels en fonction de leur respect de l’environnement (Eco_Friendly)."
    )

    # Lire la data scrapée
    df = pd.read_csv("./data/output_nhHotels.csv")

    # présentation de donnée
    st.subheader("Donées sur les hôtels du site NH Hôtels")
    st.write(df)
    st.markdown(
        "Ce sont des données que nous avons extraites depuis le site NH hôtels. "
        "Elles contiennent les informations suivantes : ")
    st.markdown("- **Pays**")
    st.markdown("- **Nom de l'hôtel**")
    st.markdown("- **Nombre d'étoile de l'hôtel**")
    st.markdown(
        "- **Eco Friendly** : *Présence ou non d'un signe indiquant que l'hôtel adopte une demarche spécifique de protection de l'environement*."
        " Plus d'information [ici](https://www.nh-hotels.fr/environnement/hotels-ecologiques-developpement-durable)"
    )
    st.markdown("- **Nombre d'étoiles sur Trip Adviser**")

    # ====== Parie Analyes ===== #
    st.write("")
    st.header("Analyses")

    # doughnut chart
    st.subheader("Répartition des hôtels")
    labels = ['Non Eco Friendly', 'Eco friendly']
    vals = df['eco_friendly'].value_counts()
    values = [vals[0], vals[1]]
    fig = go.Figure(data=[go.Pie(labels=labels, values=values, hole=.3)])
    st.write(fig)
    st.markdown(
        "**56%** des hôtels qui se trouvent dans ce site ont le signe de *Eco Friendly* et **44%** de les hôtels ne l'ont pas. "
        "Vous pouvez regarder les chiffres détaillés lorsque vous passer votre souris sur le graphe."
    )

    # Bar chart
    st.subheader("Note moyenne sur Trip Adviser")
    means = df.groupby('eco_friendly')['avis_client'].mean()
    colors = [
        'lightslategray',
    ] * 2
    colors[1] = 'crimson'
    fig_bar = go.Figure(
        data=[go.Bar(x=labels, y=[means[0], means[1]], marker_color=colors)])
    st.write(fig_bar)
    st.markdown(
        "Nous pouvons observer que la note des avis clients sur Trip Adviser pour les hôtels Eco Friendly est plus élevée à **4.12 étoiles** "
        "que ceux Non Eco Friendly à **3.9 étoiles**. Nous povons donc remarquer que les clients ont eu des expériences plus positives avec des hôtels Eco Friendly."
    )

    # ===== Conclusion ===== #
    st.write("")
    st.subheader("Conclusion")
    st.write(
        "D'après ces résultats, nous povons observer que, parmi les hôtels répertoriés sur ce site, "
        "ceux qui sont plus respectueux de l'environnement offrent un meilleur service à leurs clients."
        " Afin de promouvoir des activités plus respectueuses de l'environnement de la part du secteur du tourisme, "
        "il est conseillé de mettre ces informations sur le site afin qu'elles soient plus clairement visibles pour les clients potentiels."
        " Par conséquent, nous pensons qu'un site web qui permet aux utilisateurs de rechercher des hôtels en fonction de leur respect de l'environnement est "
        "précieux pour offrir un service plus satisfaisant aux futurs clients."
    )
Example #12
0
import numpy as np
from matplotlib.pyplot import plot

y = NelsonSiegelSvenssonCurve(0.028, -0.03, -0.04, -0.015, 1.1, 4.0)
t = np.linspace(0, 20, 100)
plot(t, y(t))

import numpy as np
from nelson_siegel_svensson.calibrate import calibrate_ns_ols
from nelson_siegel_svensson.calibrate import calibrate_nss_ols

t = np.array([0.0, 0.5, 1.0, 2.0, 3.0, 4.0, 5.0, 10.0, 15.0, 20.0, 25.0, 30.0])
y = np.array([
    0.01, 0.011, 0.013, 0.014, 0.018, 0.020, 0.025, 0.03, 0.035, 0.037, 0.038,
    0.04
])

curve, status = calibrate_ns_ols(
    t, y, tau0=1.0)  # starting value of 1.0 for the optimization of tau
assert status.success
print(curve)

curve.beta

# NSS Model fit
curve, status = calibrate_nss_ols(t, y)

import plotly.graph_objects as go
fig = go.Figure(data=go.Bar(y=[2, 3, 1]))
fig.write_html('first_figure.html', auto_open=True)
Example #13
0
def g_relative_bars(p_y0, p_y1, p_theme, p_dims):
    """

    Generates a plot with two bars (two series of values) and two horizontal lines (medians of each
    series)

    Requirements
    ------------
    numpy
    pandas
    plotly

    Parameters
    ----------

    p_y0: dict
        values for upper bar plot

        {data: y0 component to plot (left axis), color: for this data, type: line/dash/dash-dot,
        size: for this data, n_ticks: number of ticks for this axis}

    p_y1: dict
        values for lower bar plot

        {data: y0 component to plot (right axis), color: for this data, type: line/dash/dash-dot,
        size: for this data, n_ticks: number of ticks for this axis}

    p_theme: dict
        colors and font sizes

        {'color_1': '#ABABAB', 'color_2': '#ABABAB', 'color_3': '#ABABAB', 'font_color_1': '#ABABAB',
        'font_size_1': 12, 'font_size_2': 16}

    p_dims: dict
        final dimensions of the plot as a file

        {'width': width in pixels, 'heigth': height in pixels}

    Returns
    -------
    fig_relative_bars: plotly
        Object with plotly generating code for the plot

    Debugging
    ---------
    p_y0 = df_f_data['c2'].copy()
    p_y1 = df_f_data['c16'].copy()
    p_theme = base_theme

    """

    # list of periods for the x axis
    l_periods = pd.date_range('2017-01-01', '2020-02-28',
                              freq='MS').strftime("%m/%y")

    # calculations for the y axis
    l_profit = p_y0['data'] / 100
    y_profit = np.round(np.median(l_profit), 4)
    l_dde = p_y1['data'] / 100
    y_dde = np.round(np.median(l_dde), 4)

    # ticks values for y axis
    y0_ticks_vals = np.arange(min(l_dde), max(l_profit),
                              (max(l_profit) - min(l_dde)) / 10)
    y0_ticks_vals = np.append(y0_ticks_vals, max(l_profit))
    y0_ticks_vals = np.round(y0_ticks_vals, 4)

    # instantiate a figure object
    fig_relative_bars = go.Figure()

    # add upper bars
    fig_relative_bars.add_trace(
        go.Bar(name='Return (%)',
               x=l_periods,
               y=l_profit,
               marker_color=p_y0['color'],
               marker_line_color=p_y0['color'],
               marker_line_width=1.5,
               opacity=0.99))
    # add lower bars
    fig_relative_bars.add_trace(
        go.Bar(name='DrawDown (%)',
               x=l_periods,
               y=l_dde,
               marker_color=p_y1['color'],
               marker_line_color=p_y1['color'],
               marker_line_width=1.5,
               opacity=0.99))

    # Horizontal lines
    lines = [
        dict(x0=0,
             x1=1,
             xref='paper',
             y0=y_profit,
             y1=y_profit,
             yref='y',
             type='line',
             line=dict(color=p_theme['color_3'], width=1.5, dash='dashdot')),
        dict(x0=0,
             x1=1,
             xref='paper',
             y0=y_dde,
             y1=y_dde,
             yref='y',
             type='line',
             line=dict(color=p_theme['color_3'], width=1.5, dash='dashdot'))
    ]

    # Layout
    fig_relative_bars.update_layout(
        barmode='relative',
        yaxis_tickformat='.2%',
        margin=go.layout.Margin(l=50, r=50, b=20, t=50, pad=20),
        xaxis=dict(title_text='month/year', rangeslider=dict(visible=False)),
        yaxis=dict(title_text='DrawDown (%)    |    Return(%)'),
        shapes=lines)

    # Text anotations inside graph
    fig_relative_bars.update_layout(annotations=[
        go.layout.Annotation(x=36,
                             y=y_profit * 1.2,
                             text="Median Return <b> (" +
                             str(np.round(y_profit * 100, 2)) + "%) </b>",
                             textangle=0,
                             xref="x",
                             yref='y',
                             showarrow=False,
                             font=dict(size=p_theme['font_size_1'],
                                       color=p_theme['color_2'])),
        go.layout.Annotation(x=36,
                             y=y_dde * 1.2,
                             text="Median DrawDown <b>(" +
                             str(np.round(y_dde * 100, 2)) + "%)</b>",
                             textangle=0,
                             xref="x",
                             yref="y",
                             showarrow=False,
                             font=dict(size=p_theme['font_size_1'],
                                       color=p_theme['color_2']))
    ])

    # Update layout for the background
    fig_relative_bars.update_layout(
        paper_bgcolor='white',
        yaxis=dict(tickvals=y0_ticks_vals,
                   zeroline=False,
                   automargin=True,
                   tickfont=dict(color='grey', size=p_theme['font_size_1'])),
        xaxis=dict(tickfont=dict(color='grey', size=p_theme['font_size_1'])))

    # Update layout for the y axis
    fig_relative_bars.update_yaxes(showgrid=True,
                                   gridwidth=.25,
                                   gridcolor='lightgrey')

    # Legend format
    fig_relative_bars.update_layout(paper_bgcolor='white',
                                    plot_bgcolor='white',
                                    legend=go.layout.Legend(x=.41,
                                                            y=-.15,
                                                            orientation='h',
                                                            font=dict(
                                                                size=14,
                                                                color='grey')),
                                    margin=go.layout.Margin(l=50,
                                                            r=50,
                                                            b=50,
                                                            t=50,
                                                            pad=10))

    # Saquare in the upper text anotation
    fig_relative_bars.add_shape(type="rect",
                                x0=32,
                                y0=y_profit * 1.1,
                                x1=40,
                                y1=y_profit * 1.3,
                                line=dict(color=p_theme['color_3'], width=2),
                                fillcolor=p_theme['color_3'])

    # Saquare in the lower text anotation
    fig_relative_bars.add_shape(type="rect",
                                x0=32,
                                y0=y_dde * 1.1,
                                x1=40,
                                y1=y_dde * 1.3,
                                line=dict(color=p_theme['color_3'], width=2),
                                fillcolor=p_theme['color_3'])

    # Final plot dimensions
    fig_relative_bars.layout.autosize = True
    fig_relative_bars.layout.width = p_dims['width']
    fig_relative_bars.layout.height = p_dims['height']

    return fig_relative_bars
Example #14
0
join DESTINATION on DESTINATION.DESTINATION=PRODUCT_DETAILS.DESTINATION_CODE
group by DESTINATION.DESTINATION
having max(COMMISION) > 150;
"""

cursor.execute(request_3)

x_request_3 = list()
y_request_3 = list()

for DESTIN_1, max_value in cursor:
    x_request_3.append(ESTIN_1)
    y_request_3.append(max_value)

# ----end query3-----
bar = go.Bar(y=x_request_1, x=y_request_1)
graph_request_1 = py.plot([bar], auto_open=False, filename='request_1')

pie = go.Pie(labels=x_request_2, values=y_request_2)
graph_request_2 = py.plot([pie], auto_open=False, filename='request_2')

scatter = go.Scatter(x=x_request_3, y=y_request_3)
graph_request_3 = py.plot([scatter], auto_open=False, filename='request_3')

cursor.close()
con.close()

# dashboard creation ---------------------------------------------------

MyBoard1 = dashboard.Dashboard()
dia_1 = {
Example #15
0
def poll_vis(responses, poll_id, question_id=None, crosstab_variable="-"):
    """returns bar graph"""
    # ------------------ subset the data ----------------- #
    if question_id is None:
        target_questions = responses[responses.poll_id ==
                                     poll_id].question_id.unique()
        # check if there's only one question for the poll, if there's more than 1 --
        # tell the user that's not supported
        assert target_questions.size == 1, "Please select a question:"
        # + target_questions -- have the assert statement include a list of the question options
        question_id = target_questions[0]
    target_responses = responses[
        (responses.poll_id == poll_id)
        & (responses.question_id == question_id)
        & (responses.xtab1_var == crosstab_variable)
        # make sure that we're not including any responses for the first cross-tab variable that have a second cross-tab variable
        & (responses.xtab2_var == "-")]

    # create a list comprehsion to get the response labels in the same order as the response order

    top_labels = target_responses.sort_values(
        by=["response_order"]).response_shortened.unique()

    resp_df = target_responses.sort_values(by=["response_order"])[[
        "response_shortened",
        "percent_norm",
        "favorability",
        "response_order",
        "pct_fav",
    ]]

    def wrap_string(text, n):
        """add plotly-compliant linebreaks to a string without breaking words across lines

        Parameters
        ----------
        text : str
            a string that you want to format
        n : int
            number of characters after which you want to add a linebreak

        Returns
        -------
        string
            formatted string
        """
        return "<br>".join(textwrap.wrap(str(text), n, break_long_words=False))

    # wrap top_labels to a manageable width
    def wrap_labels(labels, n):
        """wraps each string in a list to a given length without breaking words across lines using plotly-compliant line breaks

        Parameters
        ----------
        labels : list
            list of strings to format
        n : int
            number of characters after which you want to add a linebreak

        Returns
        -------
        list
            list of formatted strings
        """
        return [wrap_string(label, n) for label in labels]

    # ---------------------- colors ---------------------- #
    STRONG_OPP = "#777777"
    WEAK_OPP = "#A9A9A9"
    NUETRAL = "#F5F5F5"
    WEAK_SUP = "#4998E2"
    STRONG_SUP = "#1565C0"

    if len(top_labels) == 6:
        colors = [STRONG_OPP, WEAK_OPP, NUETRAL, NUETRAL, WEAK_SUP, STRONG_SUP]
        top_labels = wrap_labels(top_labels, 7)
    elif len(top_labels) == 5:
        colors = [STRONG_OPP, WEAK_OPP, NUETRAL, WEAK_SUP, STRONG_SUP]
        top_labels = wrap_labels(top_labels, 7)
    elif len(top_labels) == 4:
        colors = [STRONG_OPP, WEAK_OPP, WEAK_SUP, STRONG_SUP]
        top_labels = wrap_labels(top_labels, 10)
    elif len(top_labels) == 3:
        colors = [STRONG_OPP, NUETRAL, STRONG_SUP]
        top_labels = wrap_labels(top_labels, 15)

    elif len(top_labels) == 2:
        colors = [STRONG_OPP, STRONG_SUP]
        top_labels = wrap_labels(top_labels, 15)

    # ------------------ prepare inputs ------------------ #
    # create list of unique xtab1_vals ordered by val_order
    xtab1_vals = target_responses.sort_values(
        by=["val_order"], ascending=False).xtab1_val.unique()
    # create list of lists of the percent_norm of each response in order of response_order
    # eg. [0.27 0.12 0.06 0.23 0.32]
    x_data = [
        target_responses[target_responses.xtab1_val == val].sort_values(
            by=["response_order"]).percent_norm.values for val in xtab1_vals
    ]

    # create list of y_data that is used to label the bars eg. ["Black", "Hispanic", "White"]
    y_data = xtab1_vals

    net_fav_df = target_responses.groupby(["xtab1_val"])["pct_fav"].sum()

    # NOTE this updates the figure with an empty fig so the function plays nice with the callbacks
    if len(x_data) < 1:
        return {}

    # ---------------------------------------------------- #
    #                 actual graph creation                #
    # ---------------------------------------------------- #
    fig = go.Figure()

    # example: if there is a 5-point response scale, than we would iterate through [0, 1, 2, 3, 4]
    for i in range(0, len(x_data[0])):
        for xd, yd in zip(x_data, y_data):
            # NOTE: this is tricky, the traces are drawn from the bottom left corner up
            fig.add_trace(
                go.Bar(
                    x=[xd[i]],
                    y=[yd],
                    orientation="h",
                    text=["{:.0%}".format(xd[i])],
                    textposition="inside",
                    name=top_labels[i],
                    insidetextanchor="middle",
                    insidetextfont=dict(
                        family="Arial",
                        size=20,
                    ),
                    marker=dict(
                        color=colors[i],
                        line=dict(color="rgb(248, 248, 249)", width=1),
                    ),
                    width=0.4 if len(y_data) == 1 else (),
                    hoverinfo="skip",
                    showlegend=True,
                ))

    fig.update_layout(
        xaxis=dict(
            showgrid=False,
            showline=False,
            showticklabels=False,
            zeroline=False,
            domain=[0.15, 1],
        ),
        yaxis=dict(
            showgrid=False,
            showline=False,
            showticklabels=False,
            zeroline=False,
        ),
        paper_bgcolor="rgb(248, 248, 255)",
        plot_bgcolor="rgb(248, 248, 255)",
        margin=dict(l=120, r=10, t=140, b=80),
        legend=dict(
            orientation="h",
            yanchor="bottom",
            traceorder="normal",
            xanchor="left",
            x=0.15,
            y=1.02,
        ),
        barmode="stack",
    )
    # ex. if there are 3 horizontal bars with 5-point response scale, then len(fig.data) will be 15
    for i in range(1, len(fig.data)):
        # eg. check if trace is
        if i % len(y_data) != 0:
            fig.data[i].showlegend = False
    # ---------------------------------------------------- #
    #                 Label the bar graphs                 #
    # ---------------------------------------------------- #
    annotations = []
    for yd, xd in zip(y_data, x_data):
        # labeling the y-axis
        annotations.append(
            dict(
                xref="paper",
                yref="y",
                x=0.14,
                y=yd,
                xanchor="right",
                text=wrap_string(str(yd), 15) if yd != "-" else "All",
                font=dict(family="Arial", size=14, color="rgb(67, 67, 67)"),
                showarrow=False,
                align="right",
            ))
        # add the net favorability for each bar
        # add a trace to the end to display the net favorability
        fig.add_trace(
            go.Bar(
                x=[0.10],
                y=[yd],
                orientation="h",
                text="<b>{fav:+.0f}</b>".format(
                    fav=net_fav_df[yd]
                ),  # NOTE test if we can do auto text to label the graphs
                textposition=
                "inside",  # NOTE test if we can do auto text to label the graphs
                insidetextanchor="middle",
                insidetextfont=dict(
                    family="Arial",
                    size=20,
                ),
                marker=dict(color="white", line=dict(color="white", width=1)),
                hoverinfo="skip",
                showlegend=False,
            ), )

    fig.update_layout(
        annotations=annotations,
        margin=dict(l=10, r=20, t=60, b=80),
    )

    fig.update_yaxes(automargin=True)
    fig.update_xaxes(automargin=True)

    # -------- format source information at bottom ------- #
    pollster = target_responses.loc[:, ["pollster"]].values[0][0]
    xtab1_var = target_responses.loc[:, ["xtab1_var"]].values[0][0]
    date = target_responses.loc[:, ["date"]].values[0][0]
    date = pd.to_datetime(date).strftime("%B %Y")
    url = target_responses.loc[:, ["Link"]].values[0][0]
    country = target_responses.loc[:, ["country"]].values[0][0]
    demographic = target_responses.loc[:, ["demographic"]].values[0][0]

    try:
        sample_size = int(target_responses.loc[:,
                                               ["sample_size"]].values[0][0])
        sample_size = "{:,}".format(sample_size)
    except:
        sample_size = ""

    # Change source text depending on whether this is actually the Swiss Referendum
    if poll_id == 3:
        source_text = "Results of 2016 Swiss referendum in which 2,494,848 votes were cast."
    else:
        source_text = "Survey of {sample_size} {demographic}, {country} by {pollster}, {date}. Retrieved from ".format(
            sample_size=sample_size,
            demographic=demographic,
            pollster=pollster,
            country=country,
            date=date,
        )

    # ---------------------------------------------------- #
    source_url = "<br><a target='blank' href='{}'>{}</a>".format(url, url)

    source = wrap_string(source_text, 100) + source_url

    # add annotation for data source
    fig.add_annotation(
        xref="paper",
        yref="paper",
        xanchor="left",
        x=0.15,
        y=-0.25,
        text=source,
        font=dict(family="Arial", size=12, color="rgb(67, 67, 67)"),
        align="left",
        showarrow=False,
    )

    ubicenter.add_ubi_center_logo(fig, y=-0.2)

    # NOTE: use format_fig as defined above instead of ubicenter.format_fig
    return format_fig(fig, show=False).update_layout(autosize=True)
def app():
    @st.cache(allow_output_mutation=True)
    def load_data():
        data = pd.read_csv('crimes.csv')
        c11 = pd.read_csv('india2011.csv')
        lit = pd.read_csv('Literacy.csv')

        data = data.iloc[:, 1:]
        tc = c11.groupby("State name").sum().reset_index().sort_values(
            "State name")
        c11p = c11[["District name", "Population", "State name"]]
        c11p.columns = ["DISTRICT", "Population", "State"]

        lit.columns = ['Unnamed: 0', 'DISTRICT', 'State', 'Literacy']
        lit.drop("Unnamed: 0", axis=1, inplace=True)

        lit["DISTRICT"] = lit["DISTRICT"].str.lstrip()
        lit.drop("State", axis=1, inplace=True)

        for i in range(0, len(c11["State name"])):
            c11["State name"][i] = c11["State name"][i].title()
            c11["District name"][i] = c11["District name"][i].title()

        for i in range(0, len(data['Year'])):
            data['DISTRICT'][i] = data['DISTRICT'][i].title()
            data['STATE/UT'][i] = data['STATE/UT'][i].title()

        for i in range(0, len(data["Year"])):
            if data['DISTRICT'][i] in [
                    "Total District(S)", "Zz Total", "Delhi Ut Total"
            ]:
                data['DISTRICT'][i] = "Total"

        for i in range(0, len(data["Year"])):
            if data['STATE/UT'][i] == "D&N Haveli":
                data['STATE/UT'][i] = "D & N Haveli"

        for i in range(0, len(data["Year"])):
            if data['STATE/UT'][i] == "Delhi Ut":
                data['STATE/UT'][i] = "Delhi"

        for i in range(0, len(data["Year"])):
            if data['STATE/UT'][i] == "A&N Islands":
                data['STATE/UT'][i] = "A & N Islands"

        #this is done because there are a lot of UTs divided by direction, ie., Delhi ans Sikkim both has North, South district.

        for i in range(0, len(data["Year"])):
            if data['DISTRICT'][i] in ["South", "North", "East", "West"]:
                data['DISTRICT'][i] = str(data["STATE/UT"][i]) + " " + str(
                    data['DISTRICT'][i])

        sd = data[["STATE/UT", "DISTRICT"]].drop_duplicates()

        for i in range(0, len(data["DISTRICT"])):
            if data["DISTRICT"][i] == "Coochbehar":
                data["DISTRICT"][i] = "Koch Bihar"

            elif data["DISTRICT"][i] == "Malda":
                data["DISTRICT"][i] = "Maldah"

            elif data["DISTRICT"][i] in [
                    "24 Parganas North", "North 24 Parganas"
            ]:
                data["DISTRICT"][i] = "North Twenty Four Parganas"

            elif data["DISTRICT"][i] in [
                    "24 Parganas South", "South 24 Parganas"
            ]:
                data["DISTRICT"][i] = "South Twenty Four Parganas"
            elif data["DISTRICT"][i] == "Hooghly":
                data["DISTRICT"][i] = "Hugli"

            elif data["DISTRICT"][i] == "Hyderabad City":
                data["DISTRICT"][i] = "Hyderabad"

            elif data["DISTRICT"][i] == "Cyberabad":
                data["STATE/UT"][i] = "Andhra Pradesh"

            District_Total = data.groupby("DISTRICT").sum()

            #Lets add Population and Literacy to every individual District

            District_Total = pd.merge(District_Total,
                                      c11p,
                                      on="DISTRICT",
                                      how="left")
            District_Total = pd.merge(District_Total,
                                      lit,
                                      on="DISTRICT",
                                      how="left")
            # District_Total = pd.merge(District_Total,sd, on = "DISTRICT", how="left")
            District_Total20 = District_Total.sort_values(
                by="Rape", ascending=False).reset_index().head(22)
            District_Total20.drop(0, axis=0, inplace=True)

            # We will use join(inner join) to concate two tables

            state_total = data[data["DISTRICT"] == "Total"]
            label10 = np.arange(1, 11)
            label20 = np.arange(0, 20)

        return data, District_Total, state_total, c11p, lit

    data_re, District_Total_re, state_total_re, c11p, lit = load_data()
    data = data_re
    District_Total = District_Total_re
    state_total = state_total_re
    st.title(
        "Crime against Women(2001-2014, India) Data Analysis and Visulization")

    st.text("""
    Disclaimer(please read it):
    
    This analysis and visualization is only for educational purposes, use of below
    graphs and analysis is strictly prohibited in any official or political work.
     
    Reason: Data is not well organised, dataset has various issues.In case of any
    query/issue, please connect through provided username in Footer! 
     
    Brief:Data is gathered from three different sources.

    Crimes against women data from Data.gov.in(CSV File)
    India 2011 Census Data from kaggle.com(CSV File)
    Literacy Data(based on Census 2011) from census2011.co.in/district.php(Data 
    was scraped and then stored into CSV file).
    
    As data is gathered from different sources there are some issues, I tried my best 
    to minimize them. For example, there were spelling differences with multiple 
    West Bengal districts and Cyberabad is notmentioned in Census data.Another important
    thing is to be noted that the Literacy and Population data isfrom the 2011 India
    Census and is used against all years between 2001 to 2014.
    
    Bonus Point: This notebook is made using Plotly library, you can zoom-in the graph,
    hover the point using the mouse to see detailed information in the cartesian
    planeand last 3D plot can be rotated 360 using mouse and hovering over the Scatter
    point will show you multiple information with that particular point

    """)

    label10 = np.arange(1, 11)
    label20 = np.arange(0, 20)
    ydata = data.groupby("Year").sum().reset_index()
    #Lets see the line graph for years between 2001 to 2014
    st.subheader(
        'Line graph for years between 2001 to 2014 for all the Crimes against Women'
    )

    # Create traces
    fig = go.Figure()
    fig.add_trace(
        go.Scatter(
            x=ydata.Year,
            y=ydata.Rape,
            mode='lines+markers',
            name='Rape',
            hovertext="Rape",
        ))
    fig.add_trace(
        go.Scatter(x=ydata.Year,
                   y=ydata["Dowry Deaths"],
                   mode='lines+markers',
                   name='Dowry Deaths'))
    fig.add_trace(
        go.Scatter(x=ydata.Year,
                   y=ydata["Kidnapping and Abduction"],
                   mode='lines+markers',
                   name='Kidnapping and Abduction',
                   hovertext="Kidnapping and Abduction"))
    fig.add_trace(
        go.Scatter(x=ydata.Year,
                   y=ydata["Importation of Girls"],
                   mode='lines+markers',
                   name='Importation of Girls',
                   hovertext='Importation of Girls'))
    fig.add_trace(
        go.Scatter(x=ydata.Year,
                   y=ydata["Cruelty by Husband or his Relatives"],
                   mode='lines+markers',
                   name='Cruelty by Husband or his Relatives',
                   hovertext='Cruelty by Husband or his Relatives'))
    fig.add_trace(
        go.Scatter(x=ydata.Year,
                   y=ydata["Insult to modesty of Women"],
                   mode='lines+markers',
                   name='Insult to modesty of Women',
                   hovertext='Insult to modesty of Women'))
    fig.add_trace(
        go.Scatter(
            x=ydata.Year,
            y=ydata["Assault on women with intent to outrage her modesty"],
            mode='lines+markers',
            name='Assault on women ',
            hovertext="Assault on women with intent to outrage her modesty"))

    fig.update_layout(title="", plot_bgcolor='rgba(0,0,0,0)', width=900)
    st.plotly_chart(fig)

    st.write(
        "We can see a sudden surge in Rape, Assault on women with intent to outrage her modesty and Kidnapping and Abduction after 2012. One of the main reasons for this could be Nirbhaya Delhi Case, after this, India got a moment of awareness and people started coming out against the crimes."
    )

    st.subheader(
        "Total Crimes in each States(Dropdown for Bar Graph and Pie Chart) ")
    st.write(
        "Hint: Here we have added all the crimes into one column for each state."
    )
    vis_type = st.selectbox("Select Bar Graph or Pie Chart?",
                            ["Bar Graph", "Pie Chart"])
    if vis_type == "Bar Graph":
        state_total_for_pie = state_total.groupby("STATE/UT").sum()
        state_total_for_pie[
            "Total"] = state_total_for_pie["Rape"] + state_total_for_pie[
                "Kidnapping and Abduction"] + state_total_for_pie[
                    "Dowry Deaths"] + state_total_for_pie[
                        "Insult to modesty of Women"] + state_total_for_pie[
                            "Cruelty by Husband or his Relatives"] + state_total_for_pie[
                                "Importation of Girls"]
        state_total_for_pie = state_total_for_pie.sort_values(by="Total")
        fig = go.Figure(data=[
            go.Bar(x=state_total_for_pie.index,
                   y=state_total_for_pie['Total'],
                   marker={
                       "color": np.arange(0, len(state_total_for_pie['Total']))
                   })
        ])
        fig.update_layout(autosize=True, plot_bgcolor='rgba(0,0,0,0)')
        st.plotly_chart(fig)

    elif vis_type == "Pie Chart":
        state_total_for_pie = state_total.groupby("STATE/UT").sum()
        state_total_for_pie[
            "Total"] = state_total_for_pie["Rape"] + state_total_for_pie[
                "Kidnapping and Abduction"] + state_total_for_pie[
                    "Dowry Deaths"] + state_total_for_pie[
                        "Insult to modesty of Women"] + state_total_for_pie[
                            "Cruelty by Husband or his Relatives"] + state_total_for_pie[
                                "Importation of Girls"]
        state_total_for_pie = state_total_for_pie.sort_values(by="Total")
        fig = go.Figure(data=[
            go.Pie(labels=state_total_for_pie.index,
                   values=state_total_for_pie['Total'])
        ])
        fig.update_traces(
            hoverinfo='label+percent',
            textinfo='label+percent',
            textfont_size=10,
        )
        fig.update_layout(autosize=True, )
        st.plotly_chart(fig)

    st.subheader(
        "Top 20 districts with highest crimes(sum of all crimes) happened against Women"
    )
    st.write("Hint: Hover-over the bars to see all informations.")
    District_Total["Total"] = District_Total["Rape"] + District_Total[
        "Kidnapping and Abduction"] + District_Total[
            "Dowry Deaths"] + District_Total[
                "Insult to modesty of Women"] + District_Total[
                    "Cruelty by Husband or his Relatives"] + District_Total[
                        "Importation of Girls"]
    srt = District_Total.sort_values(by="Total", ascending=False)
    srt = srt.head(23).reset_index()
    srt.drop([0, 6, 7], axis=0, inplace=True)
    # srt.drop(7,axis=0,inplace=True)
    fig = px.bar(
        srt,
        x="DISTRICT",
        y="Total",
        color='State',
        text='Total',
        title="",
    )
    fig.update_traces(textposition='outside')
    fig.update_layout(plot_bgcolor='white', width=900)
    st.plotly_chart(fig)

    st.subheader("Top 10 States for different Crimes(Dropdown)")
    variables = [
        "Top 10 States/UT with Highest number of Rapes",
        "Top 10 States/UT with Highest number of Cruelty by Husband or his Relatives",
        "Top 10 states with Highest number of Dowry Deaths",
        "Top 10 states with Highest number of Kidnapping and Abduction"
    ]
    x = st.selectbox("Chose from Dropdown", variables)

    if x == "Top 10 States/UT with Highest number of Rapes":
        state_total_r = state_total.groupby("STATE/UT").sum().sort_values(
            by="Rape", ascending=False).head(10).reset_index()
        fig = go.Figure(data=[
            go.Bar(x=state_total_r['STATE/UT'],
                   y=state_total_r["Rape"],
                   marker={'color': state_total_r['Rape']})
        ])
        fig.update_layout(title="Total number of Rape vs State/UT",
                          xaxis_title="Name of State/UT",
                          yaxis_title="Number of Rapes",
                          plot_bgcolor='white')
        fig.data[0].marker.line.width = 3
        fig.data[0].marker.line.color = "black"
        st.plotly_chart(fig)
    elif x == "Top 10 States/UT with Highest number of Cruelty by Husband or his Relatives":
        total_data_c = state_total.groupby("STATE/UT").sum().sort_values(
            by="Cruelty by Husband or his Relatives", ascending=False).head(10)
        fig = go.Figure(data=[
            go.Bar(x=total_data_c.index,
                   y=total_data_c["Cruelty by Husband or his Relatives"],
                   marker={'color': label10})
        ])
        fig.update_layout(
            title="Cruelty by Husband or his Relatives vs State/UT",
            xaxis_title="Name of State/UT",
            yaxis_title="Cruelty by Husband or his Relatives",
            plot_bgcolor='white')
        fig.data[0].marker.line.width = 3
        fig.data[0].marker.line.color = "black"
        st.plotly_chart(fig)
    elif x == "Top 10 states with Highest number of Dowry Deaths":
        state_total_d = state_total.groupby("STATE/UT").sum().sort_values(
            by="Dowry Deaths", ascending=False).head(10)
        fig = go.Figure(data=[
            go.Bar(x=state_total_d.index,
                   y=state_total_d["Dowry Deaths"],
                   marker={'color': label10})
        ])
        fig.update_layout(title="Dowry Deaths vs State/UT",
                          xaxis_title="Name of State/UT",
                          yaxis_title="Dowry Deaths",
                          plot_bgcolor='white')
        fig.data[0].marker.line.width = 3
        fig.data[0].marker.line.color = "black"
        st.plotly_chart(fig)
    elif x == "Top 10 states with Highest number of Kidnapping and Abduction":
        total_data_k = state_total.groupby("STATE/UT").sum().sort_values(
            by="Kidnapping and Abduction", ascending=False).head(10)
        fig = go.Figure(data=[
            go.Bar(x=total_data_k.index,
                   y=total_data_k["Kidnapping and Abduction"],
                   marker={'color': label10})
        ])
        fig.update_layout(title="Kidnapping and Abduction vs State/UT",
                          xaxis_title="Name of State/UT",
                          yaxis_title="Kidnapping and Abduction",
                          plot_bgcolor='white')
        fig.data[0].marker.line.width = 3
        fig.data[0].marker.line.color = "black"
        st.plotly_chart(fig)

    st.write(
        "Madhya Pradesh had the highest number of rapes between 2001 to 2014, lets see top 20 districts from Madhya Pradesh with highest Rapes."
    )
    MP_data = data[data['STATE/UT'] == "Madhya Pradesh"]
    MP_data_District = MP_data.groupby("DISTRICT").sum().sort_values(
        by="Rape", ascending=False).head(20)
    MP_data_District = pd.merge(
        MP_data_District, lit, on="DISTRICT",
        how="inner")  # Adding Literacy of Madhya Pradesh's Districts
    MP_data_District = pd.merge(
        MP_data_District, c11p, on="DISTRICT",
        how="inner")  # Adding Population of Madhya Pradesh's  Districts

    fig = go.Figure(data=[
        go.Scatter(x=MP_data_District.DISTRICT,
                   y=MP_data_District.Rape,
                   mode='markers',
                   marker=dict(size=(MP_data_District.Rape / 25),
                               color=label20))
    ])
    fig.update_layout(
        title="Top 20 Districts with Highest number of Rapes in Madhya Pradesh",
        xaxis_title="Name of Districts",
        yaxis_title="Number of Rapes",
        plot_bgcolor='white')
    fig.data[0].marker.line.width = 3
    fig.data[0].marker.line.color = "black"
    st.plotly_chart(fig)

    st.subheader(
        "Now We gonna find Districts (with State, Population and Literacy) those have the highest number of rapes in Entire India"
    )
    st.write(
        "Hint: Hover-over the bars to see State, Population, Literacy and other infos."
    )
    val = st.slider("Slide to see top N Districts", 5, 20, 15)
    District_Total__ = data.groupby("DISTRICT").sum().sort_values(
        by="Rape", ascending=False).reset_index().head(val + 2)
    District_Total__.drop(0, axis=0, inplace=True)

    #Lets add Population and Literacy to every individual District

    District_Total__ = pd.merge(District_Total__,
                                c11p,
                                on="DISTRICT",
                                how="left")
    District_Total__ = pd.merge(District_Total__,
                                lit,
                                on="DISTRICT",
                                how="left")
    District_Total__.drop(2, inplace=True)
    District_Total__.drop("Year", axis=1, inplace=True)

    #---------#

    District_Total = data.groupby("DISTRICT").sum().sort_values(
        by="Rape", ascending=False).reset_index().head(22)
    District_Total.drop(0, axis=0, inplace=True)
    District_Total = pd.merge(District_Total, c11p, on="DISTRICT", how="left")
    District_Total = pd.merge(District_Total, lit, on="DISTRICT", how="left")
    District_Total.drop(2, inplace=True)
    District_Total.drop("Year", axis=1, inplace=True)

    fig = px.bar(
        District_Total__,
        x="DISTRICT",
        y=District_Total__["Rape"],
        color=District_Total__["Rape"],
        hover_data=["State", "Population", "Literacy"],
    )
    fig.update_layout(xaxis_title="Name of Districts",
                      yaxis_title="Number of Rapes",
                      plot_bgcolor='white',
                      height=600,
                      width=700)
    fig.data[0].marker.line.width = 3
    fig.data[0].marker.line.color = "black"
    st.plotly_chart(fig)

    st.subheader(
        "Line graph for India's top Rape Reported district to see if there any relation between Rape and Literacy."
    )
    st.write(
        "Values are normalized using MinMax method, you can read about MinMax normalization on the Internet"
    )
    temp_District_Total = District_Total
    District_TotalMM = pd.DataFrame(scaler.fit_transform(
        District_Total[["Literacy", "Population", "Rape"]]),
                                    columns=["Literacy", "Population", "Rape"])
    District_TotalMM.set_index(District_Total.index, inplace=True)

    # Create traces
    fig = go.Figure()
    fig.add_trace(
        go.Scatter(x=temp_District_Total.DISTRICT,
                   y=District_TotalMM.Rape,
                   mode='lines+markers',
                   name='Rape',
                   hovertext="Rape"))

    fig.add_trace(
        go.Scatter(x=temp_District_Total.DISTRICT,
                   y=District_TotalMM.Literacy,
                   mode='lines+markers',
                   name='Literacy'))

    # Edit the layout
    fig.update_layout(title='',
                      xaxis_title='Districts Name',
                      yaxis_title='Normalized values in between 0 and 1',
                      plot_bgcolor='white')
    st.plotly_chart(fig)

    st.subheader(
        "Line graph for India's top Rape Reported district to see if there any relation between Rape and Population."
    )
    st.write(
        "Values are normalized using MinMax method, you can read about MinMax normalization on the Internet"
    )

    fig = go.Figure()
    fig.add_trace(
        go.Scatter(x=temp_District_Total.DISTRICT,
                   y=District_TotalMM.Rape,
                   mode='lines',
                   name='Rape',
                   hovertext="Rape"))

    fig.add_trace(
        go.Scatter(x=temp_District_Total.DISTRICT,
                   y=District_TotalMM.Population,
                   mode='lines',
                   name='Population'))

    # Edit the layout
    fig.update_layout(
        title='',
        xaxis_title='Districts Name',
        yaxis_title='Normalized values in between 0 and 1',
        plot_bgcolor='white',
    )
    st.plotly_chart(fig)

    st.subheader("3D Scatter Plot for top 20 Rape Reported District!")
    fig = px.scatter_3d(
        District_Total,
        x='Rape',
        y='Literacy',
        z='Population',
        color='DISTRICT',
        symbol="State",
    )
    fig.update_layout(plot_bgcolor='white',
                      paper_bgcolor='rgba(0,0,0,0)',
                      height=600,
                      width=900)
    st.plotly_chart(fig)

    st.write(
        "Hi there, if you have come so far, it shows your love for exploring things,\
     this whole project is made using four open-source libraries pandas, numpy, \
         plotly and streamlit.")
    st.subheader("Via - Satyampd(Username for Github, Kaggle and LinkedIn)")
Example #17
0
def update_intraday_graph(n_clicks, interval_value, period_value,
                          input_symbol_value):
    num_xticks = 20
    OHLCV_data = pd.DataFrame()
    present_time = datetime.datetime.now()
    day_of_week = datetime.datetime.now().weekday()
    ticker = yf.Ticker(input_symbol_value)
    if interval_value == '1m':
        OHLCV_data = ticker.history(interval=interval_value,
                                    start=present_time +
                                    relativedelta(days=-6),
                                    end=present_time)
    else:
        OHLCV_data = ticker.history(interval=interval_value,
                                    start=present_time +
                                    relativedelta(days=-59),
                                    end=present_time)

    OHLCV_data = OHLCV_data.reset_index(drop=False)

    columns = OHLCV_data.columns
    columns = [x.lower() for x in columns]
    OHLCV_data.columns = columns
    OHLCV_data = OHLCV_data.rename(columns={"datetime": "date"})
    OHLCV_data["date"] = OHLCV_data["date"].dt.tz_localize(None)

    OHLCV_data = OHLCV_data.drop(OHLCV_data.index[0])  # first row is blank
    OHLCV_data = OHLCV_data.drop(OHLCV_data.index[-1])  # last row is blank

    stock_metrics(OHLCV_data)

    #########################################################################################
    ##########                   ASSIGN DATE WINDOW                                ##########
    #########################################################################################

    if period_value == "5d":
        if day_of_week == 5:
            mask = (OHLCV_data["date"] >=
                    present_time + relativedelta(days=-8))
        elif day_of_week == 6:
            mask = (OHLCV_data["date"] >=
                    present_time + relativedelta(days=-9))
        else:
            mask = (OHLCV_data["date"] >=
                    present_time + relativedelta(days=-7))

    elif period_value == "4d":
        if day_of_week == 5:
            mask = (OHLCV_data["date"] >=
                    present_time + relativedelta(days=-7))
        elif day_of_week == 6:
            mask = (OHLCV_data["date"] >=
                    present_time + relativedelta(days=-8))
        else:
            mask = (OHLCV_data["date"] >=
                    present_time + relativedelta(days=-6))

    elif period_value == "3d":
        if day_of_week == 5:
            mask = (OHLCV_data["date"] >=
                    present_time + relativedelta(days=-6))
        elif day_of_week == 6:
            mask = (OHLCV_data["date"] >=
                    present_time + relativedelta(days=-7))
        else:
            mask = (OHLCV_data["date"] >=
                    present_time + relativedelta(days=-5))

    elif period_value == "2d":
        if day_of_week == 5:
            mask = (OHLCV_data["date"] >=
                    present_time + relativedelta(days=-4))
        elif day_of_week == 6:
            mask = (OHLCV_data["date"] >=
                    present_time + relativedelta(days=-5))
        else:
            mask = (OHLCV_data["date"] >=
                    present_time + relativedelta(days=-3))

    elif period_value == "1d":
        if day_of_week == 5:
            mask = (OHLCV_data["date"] >=
                    present_time + relativedelta(days=-3))
        elif day_of_week == 6:
            mask = (OHLCV_data["date"] >=
                    present_time + relativedelta(days=-4))
        else:
            mask = (OHLCV_data["date"] >=
                    present_time + relativedelta(days=-2))

    else:
        if day_of_week == 5:
            mask = (OHLCV_data["date"] >= present_time +
                    relativedelta(days=-3) + relativedelta(hours=-4))
        elif day_of_week == 6:
            mask = (OHLCV_data["date"] >=
                    present_time + relativedelta(days=-4))
        else:
            mask = (OHLCV_data["date"] >=
                    present_time + relativedelta(hours=-4))

    # OHLCV_data["date"] = OHLCV_data["date"].dt.strftime("%H:%M:%S %m-%d-%Y")
    OHLCV_data["date"] = pd.to_datetime(OHLCV_data["date"])
    OHLCV_data = OHLCV_data.sort_values(by="date", ascending=True)
    OHLCV_data["date"] = OHLCV_data["date"].dt.strftime("%H:%M %m-%d-%Y")

    #########################################################################################
    ##########              SET CANDLESTICK YAXIS BOUNDS                           ##########
    #########################################################################################

    #########################################################################################
    ##########              SET CANDLESTICK YAXIS BOUNDS                           ##########
    #########################################################################################

    subset = OHLCV_data[["open", "high", "low", "close", "bolupp",
                         "bollow"]].columns
    candle_low = OHLCV_data.loc[mask, subset].min().min()
    candle_high = OHLCV_data.loc[mask, subset].max().max()

    #########################################################################################
    ##########                   SET RSI 70 / 30 LINES                             ##########
    #########################################################################################

    rsiupL = [
        OHLCV_data.loc[mask, "date"].iloc[0], OHLCV_data.loc[mask,
                                                             "date"].iloc[-1]
    ]
    rsiupR = [70, 70]

    rsidownL = [
        OHLCV_data.loc[mask, "date"].iloc[0], OHLCV_data.loc[mask,
                                                             "date"].iloc[-1]
    ]
    rsidownR = [30, 30]
    rsimidL = [
        OHLCV_data.loc[mask, "date"].iloc[0], OHLCV_data.loc[mask,
                                                             "date"].iloc[-1]
    ]
    rsimidR = [50, 50]

    fig = make_subplots(rows=3,
                        cols=1,
                        shared_xaxes=False,
                        vertical_spacing=0.03,
                        row_width=[0.2, 0.6, 0.2])

    # RSI
    fig.add_trace(go.Scatter(
        name="RSI " + "{:.2f}".format(OHLCV_data["RSI"].iloc[-1]),
        hoverlabel=dict(font=dict(size=20), align="right"),
        x=OHLCV_data["date"].loc[mask],
        y=OHLCV_data["RSI"].loc[mask],
        mode="lines",
        line=dict(color="black", width=2),
        yaxis="y1",
        hovertemplate="RSI: %{y:.2f}" + "<extra></extra>"),
                  row=1,
                  col=1)
    # RSI 70
    fig.add_trace(go.Scatter(name="RSI 70",
                             mode="lines",
                             showlegend=False,
                             hoverinfo="none",
                             x=rsiupL,
                             y=rsiupR,
                             line=dict(color="black", width=2),
                             yaxis="y1"),
                  row=1,
                  col=1)
    # RSI 50
    fig.add_trace(go.Scatter(name="RSI 50",
                             mode="lines",
                             showlegend=False,
                             hoverinfo="none",
                             x=rsimidL,
                             y=rsimidR,
                             line=dict(color="black", width=2, dash="dot"),
                             yaxis="y1"),
                  row=1,
                  col=1)
    # RSI 30
    fig.add_trace(go.Scatter(name="RSI 30",
                             mode="lines",
                             showlegend=False,
                             hoverinfo="none",
                             x=rsidownL,
                             y=rsidownR,
                             line=dict(color="black", width=2),
                             yaxis="y1"),
                  row=1,
                  col=1)

    bollinecolor = "rgba(0,0,0,1)"
    # Bollinger Bands - upper
    fig.add_trace(go.Scatter(fill=None,
                             mode="lines",
                             hoverlabel=dict(font=dict(size=20),
                                             align="right",
                                             namelength=0),
                             hovertemplate="BB(UP): %{y:$.2f}" +
                             "<extra></extra>",
                             line_color=bollinecolor,
                             yaxis="y21",
                             name="bollup",
                             x=OHLCV_data["date"].loc[mask],
                             y=OHLCV_data["bolupp"].loc[mask],
                             line=dict(width=2, dash="dot"),
                             showlegend=False),
                  row=2,
                  col=1)

    # Bollinger Bands - lower
    fig.add_trace(
        go.Scatter(
            # fill="tonexty",
            mode="lines",
            hoverlabel=dict(font=dict(size=20), align="right", namelength=0),
            hovertemplate="BB(LOW): %{y:$.2f}" + "<extra></extra>",
            line_color=bollinecolor,
            # fillcolor=bollinecolor,
            name="bollow",
            yaxis="y21",
            x=OHLCV_data["date"].loc[mask],
            y=OHLCV_data["bollow"].loc[mask],
            line=dict(width=2, dash="dot"),
            showlegend=False),
        row=2,
        col=1)
    # Candlesticks
    fig.add_trace(go.Candlestick(name=str(input_symbol_value),
                                 showlegend=False,
                                 hoverlabel=dict(font=dict(size=20),
                                                 align="left",
                                                 namelength=0),
                                 x=OHLCV_data["date"].loc[mask],
                                 open=round(OHLCV_data["open"].loc[mask], 2),
                                 high=round(OHLCV_data["high"].loc[mask], 2),
                                 low=round(OHLCV_data["low"].loc[mask], 2),
                                 close=round(OHLCV_data["close"].loc[mask], 2),
                                 decreasing=dict(line=dict(color="red"),
                                                 fillcolor="red"),
                                 increasing=dict(
                                     line=dict(color="forestgreen"),
                                     fillcolor="forestgreen"),
                                 yaxis="y2"),
                  row=2,
                  col=1)

    # 20 day EMA
    fig.add_trace(go.Scatter(
        name="ExpMA(20) " + "{:.2f}".format(OHLCV_data["expMA"].iloc[-1]),
        hoverlabel=dict(font=dict(size=20), align="right"),
        x=OHLCV_data["date"].loc[mask],
        y=OHLCV_data["expMA"].loc[mask],
        mode="lines",
        line=dict(color="orange", width=2),
        yaxis="y2",
        hovertemplate="20EMA: %{y:$.2f}" + "<extra></extra>"),
                  row=2,
                  col=1)

    # 20 day SMA
    fig.add_trace(go.Scatter(
        name="MA(20) " + "{:.2f}".format(OHLCV_data["twenty_day"].iloc[-1]),
        hoverlabel=dict(font=dict(size=20), align="right"),
        x=OHLCV_data["date"].loc[mask],
        y=OHLCV_data["twenty_day"].loc[mask],
        mode="lines",
        line=dict(color="yellow", width=2),
        yaxis="y2",
        hovertemplate="20MA: %{y:$.2f}" + "<extra></extra>"),
                  row=2,
                  col=1)

    # 50 day SMA
    fig.add_trace(go.Scatter(
        name="MA(50) " + "{:.2f}".format(OHLCV_data["fifty_day"].iloc[-1]),
        hoverlabel=dict(font=dict(size=20), align="right"),
        x=OHLCV_data["date"].loc[mask],
        mode="lines",
        y=OHLCV_data["fifty_day"].loc[mask],
        line=dict(color="blue", width=2),
        yaxis="y2",
        cliponaxis=True,
        hovertemplate="50MA: %{y:$.2f}" + "<extra></extra>"),
                  row=2,
                  col=1)

    # 200 day SMA
    fig.add_trace(
        go.Scatter(cliponaxis=True,
                   name="MA(200) " +
                   "{:.2f}".format(OHLCV_data["twoHundred_day"].iloc[-1]),
                   hoverlabel=dict(font=dict(size=20), align="right"),
                   x=OHLCV_data["date"].loc[mask],
                   y=OHLCV_data["twoHundred_day"].loc[mask],
                   line=dict(color="purple", width=2),
                   yaxis="y2",
                   hovertemplate="200MA: %{y:$.2f}" + "<extra></extra>",
                   mode="lines"),
        row=2,
        col=1)

    # volume
    fig.add_trace(go.Bar(name="volume",
                         showlegend=False,
                         hoverlabel=dict(font=dict(size=20),
                                         align="left",
                                         namelength=0),
                         x=OHLCV_data["date"].loc[mask],
                         y=OHLCV_data["volume"].loc[mask],
                         marker_color=OHLCV_data["color"].loc[mask],
                         yaxis="y3"),
                  row=3,
                  col=1)

    # fig.update_layout(yaxis2=dict(overlaying="y2",layer="below traces"))

    fig.update_layout(

        # plot_bgcolor="rgba(255,255,255,0)",
        # paper_bgcolor="rgba(0,0,0,0)",
        title=dict(
            text=input_symbol_value.upper() + ". " +
            str(present_time.strftime("%Y-%m-%d %H:%M:%S")) + ". Open: " +
            "{:.2f}".format(OHLCV_data["open"].iloc[-1]) + ". High: " +
            "{:.2f}".format(OHLCV_data["high"].iloc[-1]) + ". Low: " +
            "{:.2f}".format(OHLCV_data["low"].iloc[-1]) + ". Close: " +
            "{:.2f}".format(OHLCV_data["close"].iloc[-1]) + ". Volume: " +
            "{:.1f}".format(OHLCV_data["volume"].iloc[-1]),
            xanchor="left",
            yanchor="top",
            x=0,
            y=.98,
            font=dict(color="black", family="Arial", size=18)),
        legend=dict(x=0,
                    y=1.12,
                    xanchor="left",
                    traceorder="normal",
                    font=dict(family="Arial", size=16, color="black"),
                    bgcolor="white",
                    bordercolor="Black",
                    borderwidth=2,
                    orientation="h"),
        autosize=True,
        height=900,
        margin={
            "l": 0,
            "b": 30,
            "t": 120
        },
        hovermode="x")

    # fig.update_xaxes(showspikes=True,spikecolor="black",spikethickness=2)
    # fig.update_yaxes(showspikes=True,spikecolor="black",spikethickness=2)
    # fig.update_layout(spikedistance=1000, hoverdistance=1000)

    # Update yaxis properties
    fig.update_layout(
        yaxis1=dict(tickangle=0,
                    showline=True,
                    linewidth=1.5,
                    linecolor="black",
                    mirror='allticks',
                    showgrid=False,
                    gridwidth=1,
                    gridcolor="black",
                    tickfont=dict(family="Arial", color="black", size=18),
                    nticks=6,
                    range=[10, 90]))

    fig.update_layout(
        yaxis2=dict(tickangle=0,
                    showline=True,
                    linewidth=1.5,
                    linecolor="black",
                    mirror="allticks",
                    showgrid=True,
                    gridwidth=1,
                    gridcolor="black",
                    tickfont=dict(family="Arial", color="black", size=18),
                    fixedrange=False,
                    range=[
                        candle_low - candle_low * 0.01, candle_high +
                        candle_high * 0.01
                    ],
                    autorange=True,
                    nticks=20,
                    tickformat="$.2f"))

    fig.update_layout(
        yaxis3=dict(tickangle=0,
                    showline=True,
                    linewidth=1.5,
                    linecolor="black",
                    mirror="allticks",
                    showgrid=True,
                    gridwidth=1,
                    gridcolor="black",
                    tickfont=dict(family="Arial", color="black", size=18)))

    fig.update_layout(
        xaxis1=dict(type="category",
                    nticks=num_xticks,
                    tickangle=45,
                    fixedrange=False,
                    showticklabels=False,
                    showline=True,
                    linewidth=1.5,
                    linecolor="black",
                    mirror=True,
                    showgrid=True,
                    gridwidth=1,
                    gridcolor="black",
                    tickfont=dict(family="Arial", color="black", size=14)))

    fig.update_layout(
        xaxis2=dict(type="category",
                    nticks=num_xticks,
                    tickangle=45,
                    rangemode="tozero",
                    fixedrange=False,
                    showticklabels=False,
                    showline=True,
                    linewidth=1.5,
                    linecolor="black",
                    mirror=True,
                    showgrid=True,
                    gridwidth=1,
                    gridcolor="black",
                    tickfont=dict(family="Arial", color="black", size=14),
                    rangeslider=dict(visible=True, thickness=0)))

    fig.update_layout(
        xaxis3=dict(type="category",
                    nticks=num_xticks,
                    tickangle=35,
                    rangemode="tozero",
                    fixedrange=False,
                    showline=True,
                    linewidth=1.5,
                    linecolor="black",
                    mirror=True,
                    showgrid=True,
                    gridwidth=1,
                    gridcolor="black",
                    tickfont=dict(family="Arial", color="black", size=18),
                    rangeslider=dict(visible=True, thickness=0)))

    fig.update_layout(paper_bgcolor="rgb(250, 250, 200)")

    return fig
def plotly_error_comparison(selected_run_data, selected_comparison_data,
                            metric, own_name, opponent_name, meta_data,
                            bin_no):
    x_axis_text = 'log E (E / GeV)'
    y_axis_text = y_axis_print(metric)
    bin_mids = meta_data['1d_histogram']['bin_mids']
    bin_widths = meta_data['1d_histogram']['bin_widths']
    bin_lengths = [ibin * 2 for ibin in bin_widths]
    bin_counts = meta_data['1d_histogram']['counts']
    own_error_bin_edges = selected_run_data[metric]['error_histograms'][
        bin_no]['x_edges']
    own_error_x_bin_mids = (own_error_bin_edges[:-1] +
                            own_error_bin_edges[1:]) / 2
    own_error_x_bin_widths = 1.0 * (own_error_bin_edges[1] -
                                    own_error_bin_edges[0])
    own_percentiles = selected_run_data[metric]['error_histograms'][bin_no][
        'percentiles']
    own_performance = selected_run_data[metric]['1d_histogram']['performance']
    opponent_error_bin_edges = selected_comparison_data[metric][
        'error_histograms'][bin_no]['x_edges']
    opponent_error_x_bin_mids = (opponent_error_bin_edges[:-1] +
                                 opponent_error_bin_edges[1:]) / 2
    opponent_error_x_bin_widths = 1.0 * (opponent_error_bin_edges[1] -
                                         opponent_error_bin_edges[0])
    opponent_percentiles = selected_comparison_data[metric][
        'error_histograms'][bin_no]['percentiles']
    opponent_performance = selected_comparison_data[metric]['1d_histogram'][
        'performance']
    histogram_colors = ['grey'] * len(bin_mids)
    histogram_colors[bin_no] = 'red'
    trace1 = go.Bar(x=bin_mids,
                    y=bin_counts,
                    width=bin_lengths,
                    opacity=0.2,
                    marker_color=histogram_colors,
                    xaxis='x1',
                    yaxis='y2',
                    name='test data',
                    showlegend=True)

    trace2 = go.Scatter(x=bin_mids,
                        y=own_performance,
                        mode='markers',
                        name=own_name,
                        showlegend=True,
                        marker=dict(color='Red'),
                        error_x=dict(type='data',
                                     array=bin_widths,
                                     visible=True),
                        xaxis='x1')

    trace3 = go.Scatter(x=bin_mids,
                        y=opponent_performance,
                        mode='markers',
                        name=opponent_name,
                        showlegend=True,
                        marker=dict(color='Green'),
                        error_x=dict(type='data',
                                     array=bin_widths,
                                     visible=True),
                        xaxis='x1')

    trace4 = go.Bar(
        x=own_error_x_bin_mids,
        y=selected_run_data[metric]['error_histograms'][bin_no]['counts'],
        width=own_error_x_bin_widths,
        xaxis='x2',
        yaxis='y3',
        name=own_name + ' error',
        opacity=0.5,
        marker_color='Red',
        showlegend=False)
    trace5 = go.Bar(x=opponent_error_x_bin_mids,
                    y=selected_comparison_data[metric]['error_histograms']
                    [bin_no]['counts'],
                    width=opponent_error_x_bin_widths,
                    xaxis='x2',
                    yaxis='y3',
                    name=opponent_name + ' error',
                    opacity=0.5,
                    marker_color='Green',
                    showlegend=False)

    data = [trace1, trace2, trace3, trace4, trace5]

    layout = go.Layout(
        xaxis=dict(rangemode='tozero', title=x_axis_text, domain=[0, 0.6]),
        xaxis2=dict(
            title=y_axis_text,
            domain=[0.7, 1],
            # range=[
            #     selected_run_data[metric][bin_type]['percentiles']['low_cut_percentile'][bin_no],
            #     selected_run_data[metric][bin_type]['percentiles']['high_cut_percentile'][bin_no]
            # ]
        ),
        yaxis=dict(
            rangemode='tozero',
            title='(84th percentile - 16th percentile) / 2',
            # range=[0,]
        ),
        yaxis2=dict(title='Events', type='log', overlaying='y', side='right'),
        yaxis3=dict(title='Frequency', side='right', anchor='x2'))

    fig = go.Figure(data=data, layout=layout)
    fig.update_layout(legend=dict(x=-.1, y=1.2, orientation='h'), height=500)

    fig.update_layout(annotations=[
        dict(x=0.01,
             y=0.99,
             showarrow=False,
             text='Events: {}'.format(int(sum(bin_counts))),
             xref='paper',
             yref='paper',
             bgcolor='white'),
        dict(x=0.99,
             y=0.99,
             showarrow=False,
             text='Events: {}'.format(
                 int(
                     sum(selected_run_data[metric]['error_histograms'][bin_no]
                         ['counts']))),
             xref='paper',
             yref='paper',
             bgcolor='white'),
    ])

    fig.add_shape(
        type='line',
        xref='x2',
        yref='paper',
        x0=own_percentiles[0],
        y0=0,
        x1=own_percentiles[0],
        y1=1,
        name=own_name + ' 16\%',
        line=dict(color='Red', width=1, dash='dot'),
    )
    fig.add_shape(
        type='line',
        xref='x2',
        yref='paper',
        x0=own_percentiles[2],
        y0=0,
        x1=own_percentiles[2],
        y1=1,
        name=own_name + ' 84\%',
        line=dict(color='Red', width=1, dash='dot'),
    )

    fig.add_shape(
        type='line',
        xref='x2',
        yref='paper',
        x0=opponent_percentiles[0],
        y0=0,
        x1=opponent_percentiles[0],
        y1=1,
        name=opponent_name + ' 16\%',
        line=dict(color='Green', width=1, dash='dot'),
    )
    fig.add_shape(
        type='line',
        xref='x2',
        yref='paper',
        x0=opponent_percentiles[2],
        y0=0,
        x1=opponent_percentiles[2],
        y1=1,
        name=opponent_name + ' 84\%',
        line=dict(color='Green', width=1, dash='dot'),
    )
    return fig
def plot_totalreview_google_date(dataframe):
  df = dataframe
  #print(df.head())
  df.index = df['at']
  df = pd.DataFrame(df['review'].resample('MS').count()).join(
      pd.DataFrame(df['rating'].resample('MS').mean()))
  fig = go.Figure()
  fig = make_subplots(specs=[[{"secondary_y": True}]])

  fig.add_trace(
      go.Bar(
          x=df.index,
          y=df.review,
          name="Total Review",
          marker_color='#14c2a5',
          opacity=1
      ),
      secondary_y=False
  )

  fig.add_trace(
      go.Scatter(
          x=df.index,
          y=df.rating,
          mode="lines",
          name="Average Rating",
          marker_color='#3d4a57',
          opacity=0.4
      ),
      secondary_y=True
  )

  # Add figure title
  fig.update_layout(legend=dict(
      orientation="h",
      yanchor="bottom",
      y=1.02,
      xanchor="right",
      x=0.93),
      title={
      'text': '<span style="font-size: 25px;">Google User Total Review and Average Rating</span>',
      'y': 0.97,
      'x': 0.45,
      'xanchor': 'center',
      'yanchor': 'top'},
      paper_bgcolor="#ffffff",
      plot_bgcolor="#ffffff",
      width=1500, height=700
  )

  # Add image
  fig.add_layout_image(
      dict(
          source="https://res-2.cloudinary.com/crunchbase-production/image/upload/c_lpad,h_170,w_170,f_auto,b_white,q_auto:eco/k5gw7klohl445zwak3mc",
          xref="paper", yref="paper",
          x=0.92, y=1.04,
          sizex=0.1, sizey=0.1,
          xanchor="right", yanchor="bottom"
      )
  )

  # Set x-axis title
  fig.update_xaxes(title_text="Time")

  # Set y-axes titles
  fig.update_yaxes(title_text="Number of Reviews",
                   secondary_y=False, showgrid=False)
  fig.update_yaxes(title_text="Rating", range=[
                   0, 5], secondary_y=True, showgrid=False)
  graphJSON = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
  return graphJSON
Example #20
0
def make_graph(value):
    return {"data": [go.Bar(x=df["都道府県"], y=df[value]/60)]}
    numbers = data[data['MajorText'] == crime].groupby('LookUp_BoroughName').sum()['202010']
    area = widths*numbers
    area = area.round(0) # numbers rounded up for clarity + rounding errors
    numbers = (numbers * 100).round(1) # convert to percentage for graph clarity vs decimal points
    print(numbers)
    crimelist = np.array([crime, crime, crime])
    fig.add_trace(go.Bar(
        name=crime,
        y=numbers,
        x=np.cumsum(widths) - widths,
        width=widths,  # customize width here
        offset=0,
        customdata=np.transpose([boroughs, area, crimelist]),
        texttemplate="%{y}% x %{width} =<br>%{customdata[1]}",
        textposition="inside",
        textangle=0,
        textfont_color="white",
        hovertemplate="<br>".join([
            "Crime: %{customdata[2]}",  # added crimes (customdata[2]) so that it is easier to identify
            "Borough: %{customdata[0]}",
            "Total crimes: %{width}",
            "Percentage of total crimes: %{y}",
            "Amount committed: %{customdata[1]} <extra></extra>", # <extra> removes grey secondary box https://plotly.com/python/reference/#scatter-hovertemplate
        ])
    ))

fig.update_xaxes(
    tickvals=np.cumsum(widths)-widths/2,
    ticktext= ["%s<br>%d" % (l, w) for l, w in zip([s + " Crimes" for s in boroughs], widths)] # add crimes to bar labels for graph clarity
)
Example #22
0
def retrun_graph_table(dfs, pipelines, title, task, t, opts, scores):
    scatters = []
    histos = []
    table = [html.H3(title)]
    if (dfs[0] is None or dfs[1] is None):
        return {
            'scatter_' + scores[0]: None,
            'histo_' + scores[0]: None,
            'scatter_' + scores[1]: None,
            'histo_' + scores[1]: None,
            'options': None
        }, None, dbc.Tabs([],
                          id="tabs-class",
                          active_tab="",
                          style={'hidden': 'true'})
    else:
        for df in dfs:
            df['pipelines'] = get_pipelines_button(df[['dataset']],
                                                   df.columns[1].split('-')[1])
            table.append(
                dbc.Table.from_dataframe(df,
                                         striped=True,
                                         bordered=True,
                                         hover=True))
            for col in df.columns[1:-1]:
                scatters.append(
                    go.Scatter(x=df['dataset'],
                               y=df[col],
                               name=col.split('-')[0],
                               mode='lines+markers'))
                histos.append(
                    go.Bar(x=df['dataset'], y=df[col], name=col.split('-')[0]))

        table.append(
            dbc.Tabs(
                [
                    dbc.Tab(label="Histograms", tab_id="histogram"),
                    dbc.Tab(label="Scatter", tab_id="scatter"),
                    dbc.Tab(label="Algorithm Options", tab_id="algo-options"),
                ],
                id="tabs-" + task,
                active_tab="histogram",
            ))

        opts = opts.to_dict()
        options = [
            html.Div([
                html.P([
                    "Running time for Autosklearn: " +
                    str(opts['autosklearn'][0]) + " minute/s"
                ]),
                html.P([
                    "Running time for TPOT: " + str(opts['tpot'][0]) +
                    " generation/s"
                ]),
                html.P([
                    "Running time for H2O: " + str(opts['h2o'][0]) +
                    " minute/s"
                ]),
                html.P([
                    "Running time for AutoKeras: " +
                    str(opts['autokeras'][0]) + " epoch/s"
                ]),
                html.P([
                    "Running time for AutoGluon: " +
                    str(opts['autogluon'][0]) + " minute/s"
                ]),
            ])
        ]

        limit = 5 if t == 'OpenML' else 6  # 6 perchè c'è il leader per i kaggle
        return {
            'scatter_' + scores[0]: scatters[:limit],
            'histo_' + scores[0]: histos[:limit],
            'scatter_' + scores[1]: scatters[limit:],
            'histo_' + scores[1]: histos[limit:],
            'options': options
        }, pipelines, table
Example #23
0
    def stack_chart_top_x_contributors_activity(self, x: int) -> Figure:
        """Generate stack chart for top x active contributors."""
        issue_creators = self.processing.process_issues_creators()
        pr_creators = self.processing.process_pr_creators()
        issue_closers = self.processing.process_issues_closers()
        pr_reviews = self.processing.process_pr_reviewers()

        overall_stats = {}
        for issue_creator, pr_creator, issue_closer, reviewer in zip(
                issue_creators.keys(), pr_creators.keys(),
                issue_closers.keys(), pr_reviews.keys()):
            if issue_creator not in overall_stats.keys():
                overall_stats[issue_creator] = 0
            if issue_closer not in overall_stats.keys():
                overall_stats[issue_closer] = 0
            if pr_creator not in overall_stats.keys():
                overall_stats[pr_creator] = 0
            if reviewer not in overall_stats.keys():
                overall_stats[reviewer] = 0

            overall_stats[issue_creator] += issue_creators[issue_creator]
            overall_stats[issue_closer] += issue_closers[issue_closer]
            overall_stats[pr_creator] += pr_creators[pr_creator]
            overall_stats[reviewer] += pr_reviews[reviewer]

        overall_stats = sorted(list(overall_stats.items()),
                               key=lambda contributor: contributor[1],
                               reverse=True)
        top_x_contributors = [el[0] for el in overall_stats[:x]]
        data = []

        data.append(
            go.Bar(
                name="issues created",
                x=top_x_contributors,
                y=[
                    issue_creators[creator] for creator in top_x_contributors
                    if creator in issue_creators.keys()
                ],
            ))
        data.append(
            go.Bar(
                name="issues closed",
                x=top_x_contributors,
                y=[
                    issue_closers[closer] for closer in top_x_contributors
                    if closer in issue_closers.keys()
                ],
            ))
        data.append(
            go.Bar(
                name="pull requests created",
                x=top_x_contributors,
                y=[
                    pr_creators[creator] for creator in top_x_contributors
                    if creator in pr_creators.keys()
                ],
            ))
        data.append(
            go.Bar(
                name="reviews given",
                x=top_x_contributors,
                y=[
                    pr_reviews[reviewer] for reviewer in top_x_contributors
                    if reviewer in pr_reviews.keys()
                ],
            ))

        fig = go.Figure(data)
        fig.update_layout(barmode="stack",
                          title_text=f"Top {x} active contributors")
        return fig
Example #24
0
plt.title('Receiver Operating Characterstic for adaboosting')
plt.plot(FPR_ADA, TPR_ADA)
plt.plot([0, 1], ls='--')
plt.plot([0, 0], [1, 0], c='.3')
plt.plot([1, 1], c='.3')
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.show()

# In[221]:

z = [
    scores_svm.mean(),
    scores_logReg.mean(),
    scores_random.mean(),
    scores_ada.mean()
]
classifiers = ["SVM", "Logistic Regression", "Random Forest", "AdaBoosting"]
fig = go.Figure(
    data=[go.Bar(
        x=classifiers,
        y=z,
        text=z,
        textposition='auto',
    )])
fig.show()

# In[ ]:

# In[ ]:
Example #25
0
# Create a D6
die_1 = Die()
die_2 = Die()

# Make some rolls, and store results in a list
results = []
for roll_number in range(1000):
    result = die_1.roll() + die_2.roll()
    results.append(result)

# Analyze the results
frequencies = []
max_result = die_1.num_sides + die_2.num_sides
for value in range(2, max_result + 1):
    frequency = results.count(value)
    frequencies.append(frequency)

# Visualize the results
x_values = list(range(2, max_result + 1))
x_axis_config = {'title': 'Result', 'dtick': 1}
y_axis_config = {'title': 'Frequency of Result'}
my_layout = go.Layout(title='Results of rolling two D6 1000 times',
                      xaxis=x_axis_config,
                      yaxis=y_axis_config)

data = [go.Bar(x=x_values, y=frequencies)]
fig = go.Figure(data, my_layout)
fig.show()

# offline.plot({'data': data, 'layout': my_layout},
#              filename='d6.html')
Example #26
0
def updateDescribeMap(mapName, mode, layer, versionList):
    cols = plotly.colors.DEFAULT_PLOTLY_COLORS
    fig = make_subplots(rows=1,
                        cols=4,
                        subplot_titles=('Match Length (min)',
                                        'Remaining Tickets of the Winner',
                                        'Player Count', ''))
    testdf = df.loc[(df['map'] == mapName) & (df['mode'] == mode) &
                    (df['layer'] == layer) & (df['version'].isin(versionList)),
                    [
                        'playerCount', 'winningTeam', 'duration',
                        'winningTeamName', 'winningTickets'
                    ]]
    team1Wins = testdf.loc[testdf['winningTeam'] == 1]
    team2Wins = testdf.loc[testdf['winningTeam'] == 2]

    trace0, trace1, trace2, trace3, trace4, trace5 = go.Box(), go.Box(
    ), go.Box(), go.Box(), go.Box(), go.Box()

    team2HasWins = True
    if len(team2Wins) > 0:
        trace0 = go.Box(y=team2Wins['duration'],
                        name=f'{team2Wins["winningTeamName"].iloc[0]}',
                        line=dict(width=2, color=cols[0]))
        trace2 = go.Box(y=team2Wins['winningTickets'],
                        name=f'{team2Wins["winningTeamName"].iloc[0]}',
                        line=dict(width=2, color=cols[0]),
                        showlegend=False)
        trace4 = go.Box(y=team2Wins['playerCount'],
                        name=f'{team2Wins["winningTeamName"].iloc[0]}',
                        line=dict(width=2, color=cols[0]),
                        showlegend=False)
    else:
        team2Wins = False

    team1HasWins = True
    team1WinTickets = team1Wins['winningTickets']
    if mode == 'Insurgency':
        team1WinTickets = 100 * team1WinTickets
    if len(team1Wins) > 0:
        trace1 = go.Box(y=team1Wins['duration'],
                        name=f'{team1Wins["winningTeamName"].iloc[0]}',
                        line=dict(width=2, color=cols[3]))
        trace3 = go.Box(y=team1WinTickets,
                        name=f'{team1Wins["winningTeamName"].iloc[0]}',
                        line=dict(width=2, color=cols[3]),
                        showlegend=False)
        trace5 = go.Box(y=team1Wins['playerCount'],
                        name=f'{team1Wins["winningTeamName"].iloc[0]}',
                        line=dict(width=2, color=cols[3]),
                        showlegend=False)
    else:
        team1HasWins = False

    winningTeamCounts = pd.value_counts(testdf['winningTeamName'].values,
                                        sort=True)
    if sum(testdf['winningTeam'] == 1) > sum(testdf['winningTeam'] == 2):
        winningTeamCounts = winningTeamCounts[::-1]
    markerColors = [cols[0], cols[3]]
    if sum(testdf['winningTeam'] == 2) == 0:
        markerColors = [cols[3]]
    trace6 = go.Bar(x=winningTeamCounts.index,
                    y=winningTeamCounts.values,
                    marker_color=markerColors,
                    showlegend=False)
    if team2HasWins:
        fig.add_trace(trace0, row=1, col=1)
        fig.add_trace(trace2, row=1, col=2)
        fig.add_trace(trace4, row=1, col=3)

    if team1HasWins:
        fig.add_trace(trace1, row=1, col=1)
        fig.add_trace(trace3, row=1, col=2)
        fig.add_trace(trace5, row=1, col=3)
    fig.add_trace(trace6, row=1, col=4)
    titleText = f'Winner Statistics: {formatMapName(mapName)} - {mode} - {layer}'
    fig.update_layout(title_text=titleText)
    return fig
import pandas as pd
import csv
import plotly.graph_objects as pg
 
df = pd.read_csv("data.csv")
fig = pg.Figure(pg.Bar(x = df.groupby("level")["attempt"].mean(),
y = ['Level 1','Level 2', 'Level 3','Level 4'],orientation = 'h'
))
fig.show()
for i in range(4):
    site_name = site_names[i]
    temp_vals = list()
    for yr in year_list:
        filt_val = flat_avg_df[(flat_avg_df.site_name == site_name)
                               & (flat_avg_df.datatype == 'tmax')
                               & (flat_avg_df.year == yr)]
        if len(filt_val) > 0:
            temp_val = filt_val.rel_avg_temp_C.values[0]
        else:
            logger.info(f'Filling value for {site_name} in {yr} to be 0')
            temp_val = 0
        temp_vals.append(temp_val)
    fig.add_trace(
        go.Bar(
            x=year_list,
            y=temp_vals,
        ),
        row=(i // 2) + 1,
        col=((i % 2) + 1),
    )

fig.update_layout(barmode='relative', height=500, width=700)
fig.show(config={'displayModeBar': False})

# ==================================================
# ========== SUBPLOT BARS - V2 ==========
# ==================================================
site_names = flat_avg_df.site_name.unique()[:4]
year_list = [
    yr for yr in flat_avg_df.year.unique() if yr in flat_avg_df.year.unique()
]
Example #29
0
 def get_graph_data_bar(x, y) -> List[plotly.graph_objs._BaseTraceType]:
     return [go.Bar(
         x=x,
         y=y,
     )]
Example #30
0
    def plot(self,
             plot_type='OHLC',
             display_volume=True,
             ohlc_kwargs=None,
             volume_kwargs=None,
             ohlc_add_trace_kwargs=None,
             volume_add_trace_kwargs=None,
             fig=None,
             **layout_kwargs):
        """Plot OHLCV data.

        Args:
            plot_type: Either 'OHLC' or 'Candlestick'.
            display_volume (bool): If True, displays volume as bar chart.
            ohlc_kwargs (dict): Keyword arguments passed to `plot_type`.
            volume_kwargs (dict): Keyword arguments passed to `plotly.graph_objects.Bar`.
            ohlc_add_trace_kwargs (dict): Keyword arguments passed to `add_trace` for OHLC.
            volume_add_trace_kwargs (dict): Keyword arguments passed to `add_trace` for volume.
            fig (plotly.graph_objects.Figure): Figure to add traces to.
            **layout_kwargs: Keyword arguments for layout.

        ## Example

        ```python-repl
        >>> import vectorbt as vbt

        >>> vbt.utils.data.download("BTC-USD").vbt.ohlcv.plot()
        ```

        ![](/vectorbt/docs/img/ohlcv.png)
        """
        from vectorbt.settings import ohlcv, color_schema

        if ohlc_kwargs is None:
            ohlc_kwargs = {}
        if volume_kwargs is None:
            volume_kwargs = {}
        if ohlc_add_trace_kwargs is None:
            ohlc_add_trace_kwargs = {}
        if volume_add_trace_kwargs is None:
            volume_add_trace_kwargs = {}
        if display_volume:
            ohlc_add_trace_kwargs = merge_dicts(dict(row=1, col=1),
                                                ohlc_add_trace_kwargs)
            volume_add_trace_kwargs = merge_dicts(dict(row=2, col=1),
                                                  volume_add_trace_kwargs)

        column_names = ohlcv[
            'column_names'] if self._column_names is None else self._column_names
        open = self._obj[column_names['open']]
        high = self._obj[column_names['high']]
        low = self._obj[column_names['low']]
        close = self._obj[column_names['close']]

        # Set up figure
        if fig is None:
            if display_volume:
                fig = make_subplots(rows=2,
                                    cols=1,
                                    shared_xaxes=True,
                                    vertical_spacing=0,
                                    row_heights=[0.7, 0.3])
            else:
                fig = FigureWidget()
            fig.update_layout(showlegend=True,
                              xaxis=dict(rangeslider_visible=False,
                                         showgrid=True),
                              yaxis=dict(showgrid=True),
                              bargap=0)
        fig.update_layout(**layout_kwargs)
        if plot_type.lower() == 'ohlc':
            plot_type = 'OHLC'
            plot_obj = go.Ohlc
        elif plot_type.lower() == 'candlestick':
            plot_type = 'Candlestick'
            plot_obj = go.Candlestick
        else:
            raise ValueError("Plot type can be either 'OHLC' or 'Candlestick'")
        ohlc = plot_obj(x=self.wrapper.index,
                        open=open,
                        high=high,
                        low=low,
                        close=close,
                        name=plot_type,
                        increasing_line_color=color_schema['increasing'],
                        decreasing_line_color=color_schema['decreasing'])
        ohlc.update(**ohlc_kwargs)
        fig.add_trace(ohlc, **ohlc_add_trace_kwargs)

        if display_volume:
            volume = self._obj[column_names['volume']]

            marker_colors = np.empty(volume.shape, dtype=np.object)
            marker_colors[(close.values -
                           open.values) > 0] = color_schema['increasing']
            marker_colors[(close.values -
                           open.values) == 0] = color_schema['gray']
            marker_colors[(close.values -
                           open.values) < 0] = color_schema['decreasing']
            volume_bar = go.Bar(x=self.wrapper.index,
                                y=volume,
                                marker=dict(color=marker_colors, line_width=0),
                                opacity=0.5,
                                name='Volume')
            volume_bar.update(**volume_kwargs)
            fig.add_trace(volume_bar, **volume_add_trace_kwargs)

        return fig