new_cases.add_trace( go.Scatter(x=current_country['date'].to_list(), y=current_country['new_cases_MA'].to_list(), name="7-Days Moving Average")) new_cases.update_layout(legend=dict(yanchor="top", y=0.99, xanchor="left", x=0.01), legend_title="Cases", title="New Cases in {}".format(country_select)) st.plotly_chart(new_cases) new_deaths = px.line(data_frame=current_country, x='date', y='new_deaths', title='New Deaths in {}'.format(country_select), width=700, height=450, labels={"new_deaths": "New Deaths"}) st.plotly_chart(new_deaths) total_cases = px.line(data_frame=current_country, x='date', y='total_cases', title='Total Cases in {}'.format(country_select), width=700, height=450, labels={"total_cases": "Total Cases"}) st.plotly_chart(total_cases) # ----------------------------------------------------------------------------
def app(): st.title('Market Signals') st.write('Useful market signals from popular strategies.') st.markdown("## Rotational strategy") st.write( "Signal for the asset rotation strategy that buys either gold, bonds or equities" " (see https://seekingalpha.com/article/4283733-simple-rules-based-asset-rotation-strategy)." ) rotstrat_curves = utils.load_economic_curves( date.today() - timedelta(365 * 2), date.today()) rotstrat_curves = rotstrat_curves.drop(['Max'], axis=1) columns = rotstrat_curves.columns rotstrat_curves['date'] = rotstrat_curves.index rotstrat_curves_long = pd.melt(rotstrat_curves, id_vars=['date'], value_vars=columns, var_name='asset', value_name='signal') fig = px.line(rotstrat_curves_long, x="date", y="signal", color="asset") st.plotly_chart(fig, use_container_width=True) col1, col2 = st.beta_columns(2) col1.markdown("- If the yield curve (T10Y2Y) is highest, buy stocks. ") col1.markdown( "- If the inflation expectation rate minus the yield curve (T10YIE_T10Y2Y) is highest, buy gold." ) col1.markdown( "- If the 20-year TIP rate (DFII20) is highest, buy long-term bonds.") RotStrat_stocks = [ [ 'DFII20', '20-Year Treasury Inflation-Indexed Security, Constant Maturity', 'https://fred.stlouisfed.org/series/DFII20' ], [ 'T10Y2Y', '10-Year Treasury Constant Maturity Minus 2-Year Treasury Constant Maturity', 'https://fred.stlouisfed.org/series/T10Y2Y' ], [ 'T10YIE_T10Y2Y', '10-Year Breakeven Inflation Rate minus T10Y2Y', 'https://fred.stlouisfed.org/series/T10YIE' ] ] RotStrat_stocks_df = pd.DataFrame(RotStrat_stocks, columns=['ticker', 'name', 'link']) col2.dataframe(RotStrat_stocks_df) st.markdown("## GEM") st.write( "Global equity momentum strategy. Needs only 4 assets of classes equity, equity_intl, bond_lt, money_market. " "example: `VEU,IVV,BIL,AGG equity_intl,equity,money_market,bond_lt`. " "See https://blog.thinknewfound.com/2019/01/fragility-case-study-dual-momentum-gem/" ) GEM_curves = utils.load_GEM_curves(date.today() - timedelta(365 * 3), date.today()) fig = px.line(GEM_curves, x="date", y="return", color="asset") st.plotly_chart(fig, use_container_width=True) col1, col2 = st.beta_columns(2) wd = os.path.dirname( os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) GEM_image_path = utils.find('GEM.png', wd) GEM_image = Image.open(GEM_image_path) col1.image(GEM_image) GEM_stocks = [[ 'VEU', 'Vanguard FTSE All-World ex-US Index Fund ETF Shares ', 'https://finance.yahoo.com/quote/VEU' ], [ 'IVV', 'iShares Core S&P 500 ETF', 'https://finance.yahoo.com/quote/IVV' ], [ 'BIL', 'SPDR Bloomberg Barclays 1-3 Month T-Bill ETF', 'https://finance.yahoo.com/quote/BIL' ]] GEM_stocks_df = pd.DataFrame(GEM_stocks, columns=['ticker', 'name', 'link']) col2.dataframe(GEM_stocks_df) st.markdown("## Accelerating dual momentum") st.write( "Accelerating Dual Momentum. Needs only 3 assets of classes equity, equity_intl, bond_lt. example: " "VFINX,VINEX,VUSTX, shareclass equity,equity_intl,bond_lt. " "See https://engineeredportfolio.com/2018/05/02/accelerating-dual-momentum-investing/" ) AccDualMom_curves = utils.load_AccDualMom_curves( date.today() - timedelta(365 * 2.5), date.today()) fig = px.line(AccDualMom_curves, x="date", y="score", color="asset") st.plotly_chart(fig, use_container_width=True) col1, col2 = st.beta_columns(2) wd = os.path.dirname( os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) AccDualMom_image_path = utils.find('acc_dualmom.png', wd) AccDualMom_image = Image.open(AccDualMom_image_path) col1.image(AccDualMom_image) AccDualMom_stocks = [ [ 'VFINX', 'Vanguard 500 Index Fund Investor Shares', 'https://finance.yahoo.com/quote/VFINX' ], [ 'VINEX', 'Vanguard International Explorer Fund Investor Shares', 'https://finance.yahoo.com/quote/VINEX' ], [ 'VUSTX', 'Vanguard Long-Term Treasury Fund Investor Shares', 'https://finance.yahoo.com/quote/VUSTX' ] ] AccDualMom_stocks_df = pd.DataFrame(AccDualMom_stocks, columns=['ticker', 'name', 'link']) col2.dataframe(AccDualMom_stocks_df) st.markdown("## Accelerating dual momentum (with GLD)") st.write( "Accelerating Dual Momentum, including TIPs as inflation hedge. Needs only 4 assets of classes equity, " "equity_intl, bond_lt, gold. example: " "VFINX,VINEX,VUSTX,GLD shareclass equity,equity_intl,bond_lt,gold. " "See https://engineeredportfolio.com/2018/05/02/accelerating-dual-momentum-investing/" ) AccDualMom_curves2 = utils.load_AccDualMom_curves2( date.today() - timedelta(365 * 2.5), date.today()) fig = px.line(AccDualMom_curves2, x="date", y="score", color="asset") st.plotly_chart(fig, use_container_width=True) #col1, col2 = st.beta_columns(2) #wd = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) #AccDualMom_image_path = utils.find('acc_dualmom.png', wd) #AccDualMom_image = Image.open(AccDualMom_image_path) #col1.image(AccDualMom_image) AccDualMom_stocks = [ [ 'VFINX', 'Vanguard 500 Index Fund Investor Shares', 'https://finance.yahoo.com/quote/VFINX' ], [ 'VINEX', 'Vanguard International Explorer Fund Investor Shares', 'https://finance.yahoo.com/quote/VINEX' ], [ 'VUSTX', 'Vanguard Long-Term Treasury Fund Investor Shares', 'https://finance.yahoo.com/quote/VUSTX' ], [ 'GLD', 'SPDR Gold Shares (GLD)', 'https://finance.yahoo.com/quote/GLD' ], [ 'GSG', 'iShares S&P GSCI Commodity-Indexed Trust (GSG)', 'https://finance.yahoo.com/quote/gsg' ], ] AccDualMom_stocks_df = pd.DataFrame(AccDualMom_stocks, columns=['ticker', 'name', 'link']) #col2.dataframe(AccDualMom_stocks_df) st.dataframe(AccDualMom_stocks_df)
def optimal_num_clustas(input_matrix, d_title, top_end=11, show_plot=False, write_image=False, output_path_full=None): # given 'input_matrix' as a pandas series containing a list / vector in each # row, find the optimal number of k_means clusters to cluster them using # the elbow method # 'top_end' is the max number of clusters. If having issues, look at the plot # and adjust accordingly if output_path_full is None: output_path_full = os.getcwd() scaler = StandardScaler() # texthero input data structure is weird. # stole the below if/else from the source code behind TH kmeans fn # https://github.com/jbesomi/texthero/blob/master/texthero/representation.py if isinstance(input_matrix, pd.DataFrame): # fixes weird issues parsing a texthero edited text pd series input_matrix_coo = input_matrix.sparse.to_coo() input_matrix_for_vectorization = input_matrix_coo.astype("float64") else: input_matrix_for_vectorization = list(input_matrix) scaled_features = scaler.fit_transform(input_matrix_for_vectorization) kmeans_kwargs = { "init": "random", "n_init": 30, "max_iter": 300, "random_state": 42 } # A list holds the SSE values for each k sse = [] for k in range(1, top_end): kmeans = KMeans(n_clusters=k, **kmeans_kwargs) kmeans.fit(scaled_features) sse.append(kmeans.inertia_) # plot to illustrate (viewing it is optional) title_k = 'Optimal k-means for' + d_title kmeans_opt_df = pd.DataFrame(list(zip(range(1, top_end), sse)), columns=['Number of Clusters', 'SSE']) f_k = px.line(kmeans_opt_df, x='Number of Clusters', y='SSE', title=title_k) # find optimum kl = KneeLocator(range(1, top_end), sse, curve="convex", direction="decreasing") onk = kl.elbow if onk is None: print("Warning - {} has no solution for optimal k-means".format(d_title)) print("Returning # of clusters as max allowed ( {} )".format(top_end)) return top_end if onk == top_end: print("Warning - {} opt. # equals max value searched ({})".format(d_title, top_end)) print("\nFor {}: opt. # of k-means clusters is {} \n".format(d_title, onk)) f_k.add_vline(x=onk) # add vertical line to plotly if show_plot: f_k.show() if write_image: f_k.write_image(join(output_path_full, title_k + ".png")) return onk
'converted'].cumsum() tidy_cumulative_conversions = tests_1_cumulative_conversions[[ 'timestamp', 'cumulative_conversions', 'test' ]].append(tests_0_cumulative_conversions[[ 'timestamp', 'cumulative_conversions', 'test' ]]) tidy_cumulative_conversions['test_condition'] = tidy_cumulative_conversions[ 'test'].replace({ 0: 'Old price (A)', 1: 'New price (B)' }) fig = px.line(tidy_cumulative_conversions, x='timestamp', y='cumulative_conversions', color='test_condition', template='plotly_white') fig.show() # Power calculations sample_conversion_rate = tests.loc[tests['test'] == 0, 'converted'].sum( ) / tests.loc[tests['test'] == 0, 'converted'].shape[0] alpha = 0.05 mde = 0.2 # Also called "desired lift" power_target = 0.9 # min improvement for B condition to be worthwhile from statsmodels.stats.power import GofChisquarePower analysis = GofChisquarePower()
# In[68]: plt.plot(k_range,scores_list) # In[ ]: # In[69]: px.line(x=k_range,y=scores_list) # In[ ]: # In[83]: algorithm=['Decision Tree','KNN Algorithm','RandomForestClassifier'] scores=[accuracy_dt,accuracy_dt_knn,accuracy_clf]
val_scaled, input_seq_length=TIMESTEPS, output_seq_length=1, is_rnn=True, batch_size=BATCH_SIZE, ) return X_train, X_val, y_train, y_val with left_col: bitcoin = get_last_8_days_hourly_bitcoin_data() bitcoin fig = px.line(bitcoin, x="date", y="price") st.plotly_chart(fig) with right_col: model_path = "../models/pretty-vortex-422/pretty-vortex-422-model-best.h5" model = tf.keras.models.load_model(model_path) # X_val, y_train, y_val are just placeholders # TO DO: Create Preprocessor, Plotter, Predictor etc. classes # or add a switch to functions like is_training to return relevant bits model_input_data, X_val, y_train, y_val = preprocess_data(bitcoin) # pred = model.predict(model_input_data)
def update_data(legenddropval, start_date, end_date, b2_graph_type, b2_btn_download_csv): df2 = df.copy() # Apply Date Range date_mask = (df2.index >= start_date) & (df2.index <= end_date) df2 = df2.loc[date_mask] # Apply Legends if (legenddropval == 'hour'): df2['TotalParkings'] = 1 pd.to_datetime(df2.index, errors='ignore') df2 = df2.resample('H').sum() df2 = df2[df2['TotalParkings'] > 0] if (legenddropval == 'day'): df2['TotalParkings'] = 1 pd.to_datetime(df2.index, errors='ignore') df2 = df2.resample('D').sum() if (legenddropval == 'month'): df2['TotalParkings'] = 1 pd.to_datetime(df2.index, errors='ignore') df2 = df2.resample('M').sum() df2['isWeekday'] = df2.apply((lambda x: 1 if x['isWeekday'] > 0 else 0), axis=1) df2['isWeekday'] = df2['isWeekday'].astype('bool') df2['isHoliday'] = df2.apply((lambda x: 1 if x['isHoliday'] > 0 else 0), axis=1) df2['isHoliday'] = df2['isHoliday'].astype('bool') if b2_graph_type == 'dist': mainchart = ff.create_distplot([df2['TotalParkings']], ['Distplot']) elif b2_graph_type == 'normal': mainchart = ff.create_distplot([df2['TotalParkings']], ['Distplot'], curve_type='normal') else: mainchart = px.line(df2, x=df2.index, y='TotalParkings', custom_data=['isWeekday', 'isHoliday']) mainchart.update_layout(title="All Parkings Grouped By " + legenddropval, xaxis_title="Date", yaxis_title="Total Parkings", font=dict(size=14, )) mainchart.update_traces(mode='lines+markers', hovertemplate='<b>%{x}</b><br><br>' + '<b>Total Parkings: %{y}</b><br><br>' + 'isWeekday: <b>%{customdata[0]}</b><br>' + 'isHoliday: <b>%{customdata[1]}</b>') total_records = len(df2) statistics = pd.DataFrame({ "Statistic": ['Total Records: ', 'Average Parkings'], "Value": [str(total_records), str(round(df2['TotalParkings'].mean(), 2))] }) b2_table_statistics = dbc.Table.from_dataframe(statistics, bordered=True, dark=True, hover=True, responsive=True, striped=True) # Check if Download Button has fired download_file = None changed_id = [p['prop_id'] for p in callback_context.triggered][0] if 'b2_btn_download_csv' in changed_id: download_file = send_data_frame( df2.to_csv, filename=datetime.now().strftime("%Y-%m-%d_%H-%M-%S") + ".csv") return (mainchart, b2_table_statistics, download_file)
# figure definition fig_scatter = px.scatter(df, x='sepal length (cm)', y='sepal width (cm)', color='clase', template="plotly_white", title='Scatter Plot') fig_box = px.box(df, x='clase', y='sepal length (cm)', color='clase', template='plotly_white', title='Multiple Boxplot') fig_line = px.line(df, x=[1, 2, 3, 4, 5], y=[1, 4, 9, 16, 25], template='plotly_white', title='Line Plot') fig_line.update_traces(mode='markers+lines') fig_bar = px.bar(df, x=['S1', 'S2', 'S3'], y=[1, 4, 9], title='Barplot', template='plotly_white') # layout app.layout = html.Div(children=[ html.H1(children='Dashboard template using Dash and plotly Libraries', className='text-center mt-5'), html.Hr(), html.Div([
import streamlit as st import plotly.express as px import plotly.graph_objects as go from urllib.request import urlopen import json import pandas as pd df = px.data.iris() fig= px.scatter(df, x="sepal_width", y = "sepal_length", color="species", size="petal_length", hover_data=['petal_width']) st.plotly_chart(fig) df= px.data.gapminder().query("continent == 'Oceania'") fig = px.line(df, x='year', y='lifeExp', color='country') st.plotly_chart(fig) fig = go.Figure(data=go.Scatter( x=[1, 2, 3, 4], y=[10, 11, 12, 13], mode='markers', marker=dict(size=[40, 60, 80, 100], color=[0, 1, 2, 3]) )) st.plotly_chart(fig) with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response: counties = json.load(response) df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv", dtype={"fips": str})
salida_Franja = lqh.groupby(['Franja'])['SH_C13', 'SC_C13'].agg(funciones) salida_Franja = salida_Franja.reset_index() #valor_x=list(salida_Franja.index) st.write(salida_Franja) #st.write(salida_Franja.columns) select = st.sidebar.selectbox( 'Evolucion Franjas', ['Prime', 'Off Prime PM', 'Prime Segunda Franja', 'Off Prime AM'], key='1') if st.sidebar.checkbox("Mostrar", True): st.markdown("## Rendimiento Share Hogar %s" % (select)) if select == 'Prime': fig = px.line(prime, x='Fecha', y=hogar_canales) elif select == 'Prime Segunda Franja': fig = px.line(prime2, x='Fecha', y=hogar_canales) elif select == 'Off Prime PM': fig = px.line(offprime, x='Fecha', y=hogar_canales) else: fig = px.line(off2, x='Fecha', y=hogar_canales) st.plotly_chart(fig) # fig_bar=px.bar(salida_Franja,x='Franja',y=('SH_C13','mean')) # st.plotly_chart(fig_bar) ########################## FUNCIONES DE DISTRIBUCION ###################################################################### cdf_p = Cdf.from_seq(prime['SH_C13']) cdf_o = Cdf.from_seq(offprime['SH_C13'])
'sex', 'year' ]).apply(lambda x: x.nchars_births.sum() / x.births.sum()).reset_index( name="avg_nchars")) nchars_by_sex_year.head() # OUTPUT # sex year avg_nchars # 0 F 1880 5.408653 # 1 F 1881 5.398921 # 2 F 1882 5.406761 # 3 F 1883 5.410331 # 4 F 1884 5.407614 # Plot the trend in average name length by sex and year fig = px.line(nchars_by_sex_year, x='year', y='avg_nchars', color='sex', height=400) fig.update_layout( title= 'Names got longer between 1960 and 1990 and then became shorter again!', xaxis_title=None, yaxis_title=None) fig.show(config={"displayModeBar": False}) # Letters in Names # Just when you thought there is not much else you could do with this dataset, here is yet another interesting question to explore. # How have the first and last letters in names changed over the years by sex? # What are the trends in percentage of names with a given first or last letter across years. # What are the most popular combinations of first and last letters?
async def _get_compare_img(self, guild_id: int, expression1: str, expression2: str, periode: int) -> Any: jour_debut = date.today() - timedelta(days=periode) jour_fin = date.today() - timedelta(days=1) tracking_cog = get_tracking_cog(self.bot) db = tracking_cog.tracked_guilds[guild_id] guild_name = self.bot.get_guild(guild_id) with db: with db.bind_ctx([Message]): # Messages de l'utilisateur dans la période query = (Message.select( fn.DATE(Message.timestamp).alias("date"), (fn.SUM(Message.content.contains(expression1)) / fn.COUNT(Message.message_id)).alias("expression1"), (fn.SUM(Message.content.contains(expression2)) / fn.COUNT(Message.message_id)).alias("expression2"), ).where(fn.DATE(Message.timestamp) >= jour_debut).where( fn.DATE(Message.timestamp) <= jour_fin).group_by( fn.DATE(Message.timestamp))) cur = db.cursor() query_sql = cur.mogrify(*query.sql()) df = pandas.read_sql(query_sql, db.connection()) # Si emote custom : simplifier le nom pour titre DW custom_emoji_str = emoji_to_str(expression1) if custom_emoji_str: expression1 = custom_emoji_str custom_emoji_str = emoji_to_str(expression2) if custom_emoji_str: expression2 = custom_emoji_str # Renommage des colonnes df = df.rename(columns={ "expression1": expression1, "expression2": expression2 }) # Remplir les dates manquantes df = df.set_index("date") df.index = pandas.DatetimeIndex(df.index) df.reset_index(level=0, inplace=True) df = df.rename(columns={"index": "date"}) # Rolling average df[expression1] = df.get(expression1).rolling(ROLLING_AVERAGE).mean() df[expression2] = df.get(expression2).rolling(ROLLING_AVERAGE).mean() title_lines = textwrap.wrap( f"<b>'{expression1}'</b> vs <b>'{expression2}'</b>") title_lines.append(f"<i style='font-size: 10px'>Sur {guild_name}.</i>") title = "<br>".join(title_lines) fig: go.Figure = px.line( df, x="date", y=[expression1, expression2], color_discrete_sequence=["yellow", "#4585e6"], template="plotly_dark", title=title, render_mode="svg", labels={ "date": "", "variable": "" }, ) # Hide y-axis fig.update_yaxes(visible=False, fixedrange=True) # Legend position fig.update_layout(legend=dict( title=None, orientation="h", y=1, yanchor="bottom", x=0.5, xanchor="center", )) fig.add_layout_image( dict( source="https://i.imgur.com/Eqy58rg.png", xref="paper", yref="paper", x=1.1, y=-0.22, sizex=0.25, sizey=0.25, xanchor="right", yanchor="bottom", opacity=0.8, )) return fig.to_image(format="png", scale=2)
xs_res = [list(idx.values()) for idx in xs_doc] df = pd.DataFrame(list(xs_doc)) df.to_csv("sensor_no.csv", sep=",") for index1, row in enumerate(xs_res): for index2, item in enumerate(row): try: xs_res[index1][index2] = (float(item)) except ValueError: pass df = pd.read_csv( 'C:/Users/halilerhan.orun/IdeaProjects/calisma1/sensor_no.csv') fig = px.line(df, x='Time', y='Temp', title='Date Series with Range Slider and Selectors') fig.update_xaxes( rangeslider_visible=True, rangeselector=dict(buttons=list([ dict(count=1, label="1m", step="month", stepmode="backward"), dict(count=6, label="6m", step="month", stepmode="backward"), dict(count=1, label="YTD", step="year", stepmode="todate"), dict(count=1, label="1y", step="year", stepmode="backward"), dict(step="all") ]))) def OnDoubleClick(event): item = tree.identify('item', event.x, event.y)
def bar_graph(option_slctd): if option_slctd != None: if option_slctd == "2017 vs 2018": dff = df.copy() dff = dff.groupby(['State', 'Year', 'state_code' ])[['Pct of Colonies Impacted']].mean() dff.reset_index(inplace=True) dff = dff[dff['Year'] != 2019] dff = dff[dff['Year'] != 2015] dff = dff[dff['Year'] != 2016] fig = px.line(dff, x='State', y='Pct of Colonies Impacted', color='Year') elif option_slctd == "2016 vs 2017": dff = df.copy() dff = dff.groupby(['State', 'Year', 'state_code' ])[['Pct of Colonies Impacted']].mean() dff.reset_index(inplace=True) dff = dff[dff['Year'] != 2019] dff = dff[dff['Year'] != 2015] dff = dff[dff['Year'] != 2018] dff = dff.groupby(['State', 'Year'])[['Pct of Colonies Impacted' ]].mean().reset_index() fig = px.line(dff, x='State', y='Pct of Colonies Impacted', color='Year') elif option_slctd == "2015 vs 2016": dff = df.copy() dff = dff.groupby(['State', 'Year', 'state_code' ])[['Pct of Colonies Impacted']].mean() dff.reset_index(inplace=True) dff = dff[dff['Year'] != 2019] dff = dff[dff['Year'] != 2017] dff = dff[dff['Year'] != 2018] dff = dff.groupby(['State', 'Year'])[['Pct of Colonies Impacted' ]].mean().reset_index() fig = px.line(dff, x='State', y='Pct of Colonies Impacted', color='Year') elif option_slctd == "2018 vs 2019(Jan-Mar)": dff = df.copy() dff = dff.groupby(['State', 'Year', 'state_code', 'Period'])[['Pct of Colonies Impacted']].mean() dff.reset_index(inplace=True) dff = dff[dff['Period'] == "JAN THRU MAR"] dff = dff[dff['Year'] != 2015] dff = dff[dff['Year'] != 2017] dff = dff[dff['Year'] != 2016] dff = dff.groupby(['State', 'Year'])[['Pct of Colonies Impacted' ]].mean().reset_index() fig = px.line(dff, x='State', y='Pct of Colonies Impacted', color='Year') else: fig = px.line() fig.update_layout(title="Impact on bees across USA", paper_bgcolor='#ebbd34', margin=dict(r=5, l=5), title_font_family='cursive') return fig
import plotly.express as px url = "https://api.covid19api.com/country/singapore/status/confirmed" payload = {} headers = {} # extract the data from the covidapi response = requests.request("GET", url, headers=headers, data=payload) if response.status_code == 200: print(f"Extraction successfull") print(f"Extraction Data: \n\n {response.text}") # Convert the response text to list of dictionary to make it usable for pandas dataframe singapore_covid_data = list(eval(response.text)) print(f"Singapore Covid Data over time: \n\n {singapore_covid_data}") # Convert the singapore covid data to a pandas dataframe singapore_covid_df = pd.DataFrame(singapore_covid_data) singapore_covid_df.head() # Plot the chart for confirmed cases over time line_chart = singapore_covid_df.loc[:, ['Date', 'Cases']] fig = px.line(line_chart, x='Date', y='Cases') fig.update_layout(xaxis=dict(tickformat='%d-%m-%Y')) fig.show()
countries = [] total_cases = [] total_deaths = [] total_confired = [] for i in dictionary_countries: countries.append(i) total_cases.append(dictionary_countries[i][0]) total_deaths.append(dictionary_countries[i][1]) total_confired.append(dictionary_countries[i][2]) df['Country'] = countries df['Total Confirmed'] = total_cases df['Total Deaths'] = total_deaths df['Total Recovered'] = total_confired df import plotly.express as py import plotly.graph_objects as go fig = go.Figure() fig.add_trace(go.Scatter(x = df['Country'],y = df['Total Deaths'])) fig.add_trace(go.Scatter(x = df['Country'],y = df['Total Confirmed'])) fig.add_trace(go.Scatter(x = df['Country'],y = df['Total Recovered'])) py.line(df,x = 'Country',y = 'Total Deaths') py.line(df,x = 'Country',y = 'Total Confirmed') py.line(df,x = 'Country',y = 'Total Recovered') sns.jointplot(df['Total Recovered'],df['Total Deaths'],kind = 'kde') df1 = df[df['Total Deaths'] > 1000] df1 sns.jointplot(df1['Total Confirmed'],df1['Total Deaths'],kind = 'kde') df2 = df[df['Total Confirmed'] > 50000] df2 sns.jointplot(df2['Total Confirmed'],df2['Total Deaths'],kind = 'kde')
) #fig.show() # Uploading on Plotly account #py.plot(fig, filename='deaths_by_state', auto_open=True) # print(tls.get_embed('https://plotly.com/~_adityadave/3/#/')) # Converting string date into datetime data['date'] = pd.to_datetime(data['date']) # Grouping the data by months data_datesGrouped = data.groupby( data['date'].dt.strftime('%B'))['cases'].sum().sort_values().reset_index() # Plotting the line graph fig = px.line(data_datesGrouped, x='date', y='cases') #fig.update_layout(title='NUMBER OF CASES BY MONTH') fig.update_layout( title='Total Cases in US States', xaxis_title="States", yaxis_title="Total Cases", ) # fig.show() # #Uploading on Plotly account #py.plot(fig, filename='cases_by_date', auto_open=True) # ############################## # # scope = ['USA']
def write(): udisp.title_awesome("Data Visualizations") st.write("Strange Fruits") keys = {'./data/lynchings.csv'} st.header("Dataset") pick = st.selectbox("Select Dataset: ", list(keys)) df = pd.read_csv(pick, encoding='utf8') df.columns = [ 'State', 'Year', 'Month', 'Day', 'Victim', 'County', 'Race', 'Sex', 'Mob', 'Offense', 'Note', '2nd Name', '3rd Name', 'Comments', 'Source' ] st.dataframe(df) group = {'State', 'Year', 'Month', 'Race', 'County', 'Offense', 'Sex'} st.header("") st.header("") st.header("") st.header("Choose an Attribute to Visualize") pick_grp = st.selectbox("Choose: ", list(group)) df3 = df.groupby(pick_grp).count() df3 = df3[['Victim']] df3.columns = ['Count'] df3['x-axis'] = df3.index st.header("") st.subheader("Line Graph") fig2 = px.line(df3, x="x-axis", y="Count", title="A graph of the number Lynchings grouped by " + str(pick_grp)) st.plotly_chart(fig2) fig = px.scatter(df3, x="x-axis", y="Count") st.header("") st.subheader("Scatter Diagram of the number of Lynchings grouped by " + str(pick_grp)) st.plotly_chart(fig) st.header("") st.subheader("Pie Chart of the number of Lynchings grouped by " + str(pick_grp)) fig1 = go.Figure(data=[go.Pie(labels=df3['x-axis'], values=df3['Count'])]) st.plotly_chart(fig1) words = df.Victim.tolist() word_could_dict = Counter(words) wordcloud = WordCloud( width=1000, height=500).generate_from_frequencies(word_could_dict) st.header("") st.subheader("WordCloud of the names of the Lynching Victims") plt.figure(figsize=[20, 10]) plt.imshow(wordcloud) plt.axis("off") plt.show() st.pyplot()
import dash import dash_core_components as dcc import dash_html_components as html import plotly.express as px import pandas as pd app = dash.Dash(__name__) # assume you have a "long-form" data frame # see https://plotly.com/python/px-arguments/ for more options df = pd.read_csv("cash-surplus-deficit/data/cash-surp-def.csv") df = df[df["Country Name"].isin([ "Hong Kong SAR China", "Hong Kong SAR China", "Afghanistan", "Bangladesh", "Bhutan", "Maldives", "Myanmar", "Nepal", "India" ])] fig = px.line(df, x="Year", y="Value", color="Country Name") app.layout = html.Div(children=[ html.H1(children='GDP Trends in South Asia'), html.Div(children=''' Dash: Sample Web application for Python. '''), dcc.Graph(id='example-graph', figure=fig) ]) if __name__ == '__main__': app.run_server(debug=True, host="0.0.0.0")
df_wc_wbs = df_iw73.groupby(["Main WorkCtr", "WBS element"]).sum().reset_index() df_wc_wbs = df_wc_wbs.loc[:, ["Main WorkCtr", "WBS element", "Total act.costs"]] cost_plot_wc_wbs = go.Figure( data=[ go.Bar( name="Total Actual Cost Per Work Centre", x=df_wc_wbs["Main WorkCtr"], y=df_wc_wbs["Total act.costs"], hovertext=df_wc_wbs["WBS element"] ), ], ) cost_plot_wc_wbs.update_layout( barmode='stack', title="Total Actual Cost per Work Centre (with WBS)", yaxis=dict( title="Total Actual Cost (AUD)" ), xaxis=dict( title="Work Centre" ) ) # Service Orders Over Time df_created = df_iw73.groupby("Created on").size().reset_index(name="Count") print(df_created) time_plot = px.line(df_created, x="Created on", y="Count")
for x in words_no_punc: if x not in stopwords: clean_words.append(x) pprint(len(clean_words)) fdist3 = FreqDist(clean_words) st.title("II - Frequency Distribution") if st.checkbox("Click here to display a frequency distribution graph"): most_common = pd.DataFrame(fdist3.most_common((10))) df = pd.DataFrame({"topic": most_common[0], "count": most_common[1]}) fig = px.line(df, x="topic", y="count", title="Your selected topic: " + option + "!") st.plotly_chart(fig) st.title("III - Wordcloud") if st.checkbox("Click here to generate wordcloud:"): st.write("Here is your wordcloud:") wordcloud = WordCloud().generate(str1) plt.imshow(wordcloud, interpolation='bilinear') plt.axis("off") plt.show() st.set_option('deprecation.showPyplotGlobalUse', False) st.pyplot() st.title("Part B - Most Popular Articles")
YEARS = [y for y in range(startYear, endYear, span)] # build all combinations of all variables df = pd.DataFrame(YEARS, columns=["year"]) \ .merge(pd.DataFrame(COUNTRIES, columns=["Country"]), how='cross') \ .merge(pd.DataFrame(COLLOCATES, columns=["Collocate"]), how='cross') # add column with virtual corpus specifications based on Country and year variables df['vc'] = [ f"textType=/Zeit.*/ & pubPlaceKey={df['Country'][i]} & pubDate since {df['year'][i]} & pubDate until {df['year'][i] + span - 1} " for i in range(0, len(df.index))] # add column with label for x axis df['Period'] = [f"{df['year'][i]}-{df['year'][i] + span - 1}" for i in range(0, len(df.index))] # connect to KorAP API server kcon = KorAPConnection(verbose=True) # perform the actual KorAP query results = kcon.collocationScoreQuery(NODE, df['Collocate'], df['vc'], lemmatizeNodeQuery=True, lemmatizeCollocateQuery=True) # join query result columns (axis=1 ...) with condition information columns # (why is reset_index needed?) df = pd.concat([df.reset_index(drop=True), results.reset_index(drop=True)], axis=1) fig = px.line(df, title=TITLE, x="Period", y="logDice", color="Country", line_dash="Collocate") fig.show() # fig.write_image(f"{NODE}_collocates_{startYear}-{endYear}_in_{'_'.join(COUNTRIES)}.png")