def plot_data_browser(df, title, new_cases, new_deaths): # Browser group plots: fig = go.Figure() fig.add_trace( go.Bar(x=df['date'], y=df['cases'], name='Total Cases', marker_color='sandybrown')) fig.add_trace( go.Bar(x=df['date'], y=new_cases, name='New Cases', marker_color='indianred')) fig.add_trace( go.Bar(x=df['date'], y=df['deaths'], name='Deaths', marker_color='black')) fig.add_trace( go.Bar(x=df['date'], y=new_deaths, name='New Deaths', marker_color='gray')) # Here we modify the tickangle of the xaxis, resulting in rotated labels. fig.update_layout(title=title, barmode='overlay', xaxis_tickangle=-45) # fig.show() import plotly.io as pio pio.write_html(fig, file='index.html', auto_open=True) import chart_studio.tools as tls tls.get_embed( 'file:///Users/tim/code/bitbucket/twfenwick/corona_tracker/index.html')
def analysis_playlist(): sp = spotipy.Spotify(auth=session.get('token_info').get('access_token')) username = session.get('username') df = md.get_playlist_data(sp, username) # return render_template('analysis.html',extract='Successfully Extracted') predicted_df = md.predict_data(df) predicted_df.to_csv(os.path.join(fileloc, 'predicted_data.csv'), index=False) no_of_songs = len(predicted_df['name']) session['predicted_playlist_data'] = predicted_df.to_json() plot_df = pd.DataFrame( predicted_df["Label"].value_counts().reset_index().values, columns=["Label", "No. of Songs"]) plot1 = px.pie( plot_df, values=plot_df['No. of Songs'], names=plot_df['Label'], title=( 'Distribution of Playlist according to Emotion\nTotal Songs: {0}'. format(no_of_songs))) plot1 = py.plot(plot1, filename='Songs in each emotions', auto_open=False) fig1 = tls.get_embed(plot1) plot2 = px.bar(predicted_df, x=predicted_df['name'], y=predicted_df['Score'], color=predicted_df['Label'], title='Category of Songs with Prediction of Score') plot2 = py.plot(plot2, filename='Category of Songs with Prediction Score', auto_open=False) fig2 = tls.get_embed(plot2) features = [ predicted_df['acousticness'], predicted_df['danceability'], predicted_df['liveness'], predicted_df['loudness'], predicted_df['speechiness'] ] plot3 = px.bar(predicted_df, x=predicted_df['name'], y=features, title='All Songs with its Features') plot3 = py.plot(plot3, filename='All Songs with its Features', auto_open=False) fig3 = tls.get_embed(plot3) return render_template('analysis.html', extract='Success', plot1=fig1, plot2=fig2, plot3=fig3)
def catCount(): jobs = pd.read_csv( r'C:\Users\lilyk\Desktop\Capstone_project-master\output.csv') cat = jobs['category'] count = (cat.value_counts()) a = [ 'Artificial Inetlligence', 'Software Engineer', 'Deep Learning', 'Machine Learning' ] t = [] t.append(count['Artificial Intelligence']) t.append(count['Software Engineer']) t.append(count['Deep Learning']) t.append(count['Machine Learning']) fig2 = px.bar(jobs, x=a, y=t, color=a, labels={ 'y': 'Frequency', 'x': 'Category' }, title=' Frequency of Job Categories') fig2.show() #This puts it in your cloud for your Chart Studios Account #You can switch it to be your account/api_key from your account. tls.set_credentials_file(username='******', api_key='3ztc7kdqWPHtPtkhusiy') url = py.plot(fig2, filename='categoryFig', auto_open=True) return (tls.get_embed(url))
def api(key, fig): username = '******' api_key = str(key) chart_studio.tools.set_credentials_file(username=username, api_key=api_key) py.plot(fig, filename='nlp_protein_papers', auto_open=False) print( tls.get_embed('https://plotly.com/~tadorfer/1/')) #change to your url
def test_get_embed_url_with_share_key(self): # Check the embed url for url with share_key included get_embed_return = tls.get_embed( "https://plot.ly/~neda/6572" + "?share_key=AH4MyPlyDyDWYA2cM2kj2m" ) expected_get_embed = ( '<iframe id="igraph" scrolling="no" ' 'style="border:none;" seamless="seamless" ' 'src="{plotly_rest_url}/' "~{file_owner}/{file_id}.embed?" 'share_key={share_key}" ' 'height="{iframe_height}" ' 'width="{iframe_width}">' "</iframe>" ).format( plotly_rest_url="https://" + "plot.ly", file_owner="neda", file_id="6572", share_key="AH4MyPlyDyDWYA2" + "cM2kj2m", iframe_height=525, iframe_width="100%", ) self.assertEqual(get_embed_return, expected_get_embed)
def plotly_url(data,user, freq='D'): """ Takes list of dicts from django model.objects.values iterable and turns it into a dataframe for use with plotly plotting. :param data: list containing all values :param freq: timeperiod, default H:hour :type data: list :type freq: str """ try: user_plotly = os.environ.get('PLOTLY_USER') api = os.environ.get('PLOTLY_API') assert len(api) > 0 assert len(user_plotly) > 0 # Make dataframe and group by month df = pd.DataFrame(data) df['created_at'] = pd.to_datetime(df['created_at']) if 'user_id' in df.columns: df.rename(columns={'user_id':'user'}) df['user'] = user # Set's up series for plotting by grouping datetime df_u = df.groupby(['user','sentiment',pd.Grouper(key='created_at',freq=freq)])['sentiment'].count().reset_index(name='total') # Get rolling avgs df_u['nroll_7'] = df_u[df_u['sentiment']=='Negative'].total.rolling(7,win_type='triang').mean() df_u['proll_7'] = df_u[df_u['sentiment']=='Positive'].total.rolling(7,win_type='triang').mean() df_u['nroll_7'] = df_u['nroll_7'].fillna(method='bfill').fillna(method='ffill') df_u['proll_7'] = df_u['proll_7'].fillna(method='bfill').fillna(method='ffill') # Plotly tls.set_credentials_file(username=user_plotly,api_key=api) user_bar = px.bar(df_u, x="created_at", y="total", color="sentiment",barmode="group", category_orders=co,labels={'created_at':'Date','total':'Total'}, title=f'{user}: Tweets',hover_data=df_u.columns) # Plot rolling averages user_bar.add_trace(go.Scatter(x=df_u[df_u['sentiment']=='Negative']['created_at'],y=df_u[df_u['sentiment']=='Negative']['nroll_7'], name='nroll_7')) user_bar.add_trace(go.Scatter(x=df_u[df_u['sentiment']=='Positive']['created_at'],y=df_u[df_u['sentiment']=='Positive']['proll_7'], name='proll_7')) url = py.plot(user_bar, filename=f'{user}_tweets_{freq}',auto_open=False) time.sleep(5) return tls.get_embed(url) except Exception as e: logger.error(f'plotly_url error: {e}')
def create_portfolio_pie(portfolio, portfolio_id): if portfolio.shape[0]: df = pd.DataFrame({ "assets": portfolio.iloc[-1]["assets"], "weights": portfolio.iloc[-1]["weights"] }) df = df[df["weights"] > 0] fig = go.Figure(data=go.Pie(labels=df["assets"], values=df["weights"])) fig_url = py.plot( fig, filename=f"portfolio_pie_{portfolio_id}", auto_open=False, ) plot_html = tls.get_embed(fig_url) print(fig_url) # print("Portfolio ID (pie):", portfolio_id) return plot_html
def salaries(): jobs = pd.read_csv( r'C:\Users\lilyk\Desktop\Capstone_project-master\output.csv') salary = jobs['salary'].dropna() category = jobs['category'].dropna().head(200) salary = np.array(salary) titles = jobs['jobTitle'].dropna().head(200) sals = [] for i in range(len(salary)): test = (salary[i].split()) nums = [] for i in test: if i.startswith('$'): number = (i.strip('$')) number = number.replace(",", '') number = float(number) nums.append(number) else: sals.append(0.0) sals.append(statistics.mean(nums)) fig3 = px.bar(jobs, x=category, y=sals, color=sals, labels={ 'y': 'Salaries', 'x': 'Category' }, title='Salaries for Different Job Categories', height=400) tls.set_credentials_file(username='******', api_key='3ztc7kdqWPHtPtkhusiy') url = py.plot(fig3, filename='salFig', auto_open=True) return (tls.get_embed(url))
def test_get_embed_url_with_share_key(self): # Check the embed url for url with share_key included get_embed_return = tls.get_embed('https://plot.ly/~neda/6572' + '?share_key=AH4MyPlyDyDWYA2cM2kj2m') expected_get_embed = ("<iframe id=\"igraph\" scrolling=\"no\" " "style=\"border:none;\" seamless=\"seamless\" " "src=\"{plotly_rest_url}/" "~{file_owner}/{file_id}.embed?" "share_key={share_key}\" " "height=\"{iframe_height}\" " "width=\"{iframe_width}\">" "</iframe>").format(plotly_rest_url="https://" + "plot.ly", file_owner="neda", file_id="6572", share_key="AH4MyPlyDyDWYA2" + "cM2kj2m", iframe_height=525, iframe_width="100%") self.assertEqual(get_embed_return, expected_get_embed)
def edus(): jobs = pd.read_csv( r'C:\Users\lilyk\Desktop\Capstone_project-master\output.csv') edus = (jobs['education'].dropna()) edus = edus.str.split(',') #GETS THE UNIQUE EDUS u = [] for x in edus: for i in x: i = i.replace('bachelors', "bachelor's") u.append(i) countDict = (Counter(u)) vals = countDict.values() keys = countDict.keys() keys, values = zip(*countDict.items()) fig = px.bar(jobs, x=keys, y=values, color=keys, labels={ 'y': 'Frequency', 'x': 'Education' }, title='Most Desired Education Levels') #fig.show() #This puts it in your cloud for your Chart Studios Account #You can switch it to be your account/api_key from your account. tls.set_credentials_file(username='******', api_key='3ztc7kdqWPHtPtkhusiy') url = py.plot(fig, filename='eduFig', auto_open=True) return (tls.get_embed(url))
def create_portfolio_graph(portfolio, spy, etf, rfr, portfolio_id): # print(portfolio) if portfolio.shape[0]: # Render a graph and return the URL # layout = go.Layout(yaxis=dict(tickformat=".2%")) spy["close"] = spy["close"] * portfolio.iloc[0]["value"] / spy.iloc[0][ "close"] spy = spy.sort_values(by="date").groupby("date").last().reset_index() etf["adj_close"] = etf["close"] * 0.6 + rfr["close"] * 0.4 etf["adj_close"] = etf["adj_close"] * portfolio.iloc[0][ "value"] / etf.iloc[0]["adj_close"] fig = go.Figure() # fig = go.Figure(filename=f"portfolio_value_{portf_name}") fig.add_trace( go.Scattergl(x=portfolio["date"], y=portfolio["value"], mode="lines", name="Portfolio Value")) #, layout=layout) fig.add_trace( go.Scattergl(x=spy["date"], y=spy["close"], mode="lines", name="SPY")) #, layout=layout) fig.add_trace( go.Scattergl(x=etf["date"], y=etf["adj_close"], mode="lines", name="40% Risk Free/60% Dow Jones")) fig.update_xaxes(title_text='Date') fig.update_yaxes(title_text='Portfolio Value') portfolio_graph_url = get_portfolio_graph_url(fig, name=portfolio_id) print(portfolio_graph_url) plot_html = tls.get_embed(portfolio_graph_url) return plot_html
def test_get_invalid_embed(): url = 'https://plot.ly/~PlotBot/a/' tls.get_embed(url)
import chart_studio.plotly as py import chart_studio.tools as tls import plotly.graph_objects as go # Create random data with numpy import numpy as np ''' x = np.arange(10) fig = go.Figure(data=go.Scatter(x=x, y=x**2)) fig.show() ''' N = 500 random_x = np.linspace(0, 1, N) random_y = np.random.randn(N) # Create a trace data = go.Scatter(x=random_x, y=random_y) tls.get_embed("https://plot.ly/~blaesting/0") fig = go.Figure(data) #fig.show()
def edu_sal(): jobs = pd.read_csv( r'C:\Users\lilyk\Desktop\Capstone_project-master\output.csv') #makes it so u can graph, but messes up data edus = (jobs['education'].fillna('Not Specified').head(235)) edus = edus.str.split(',') #GETS THE UNIQUE EDUS u = [] for x in edus: for i in x: i = i.replace('bachelors', "bachelor's") u.append(i) print(len(u)) #keys,values = zip(*countDict.items()) ##KEYS IS EDUS salary = jobs['salary'].dropna() salary = np.array(salary) sals = [] for i in range(len(salary)): # print (salary[i]) test = (salary[i].split()) nums = [] for i in test: if i.startswith('$'): number = (i.strip('$')) number = number.replace(",", '') #print (number) number = float(number) #print (number) nums.append(number) else: pass try: sals.append(statistics.mean(nums)) except: sals.append(nums) #print (sals) print(len(sals)) fig = px.scatter(jobs, x=u, y=sals, color=u, labels={ 'y': 'Sal', 'x': 'Education' }, title='edu/sal', size=sals) tls.set_credentials_file(username='******', api_key='3ztc7kdqWPHtPtkhusiy') url = py.plot(fig, filename='scatEduSal', auto_open=True) return (tls.get_embed(url))
def test_get_invalid_embed(): url = "https://plotly.com/~PlotBot/a/" with pytest.raises(PlotlyError): tls.get_embed(url)
def test_get_valid_embed(): url = "https://plotly.com/~PlotBot/82/" tls.get_embed(url)
URL = 'https://scholar.google.com/citations?user=RgxdCXQAAAAJ&hl=en' page = requests.get(URL) soup = BeautifulSoup(page.content, 'html.parser') df = pd.DataFrame() df['Year'] = pd.to_numeric( [i.get_text() for i in soup.find_all(class_='gsc_g_t')]) df['Citations'] = pd.to_numeric( [i.get_text() for i in soup.find_all(class_='gsc_g_al')]) total_citations = df['Citations'].sum() ''' ========================================================================================= ''' fig = px.bar(df, x='Year', y='Citations', title='Total Citations: {}'.format(total_citations)) fig.update_traces(marker_color='#990A00', marker_line_width=1) fig.update_layout({'plot_bgcolor': '#ACC1D5'}) fig.show() ''' ========================================================================================= ''' chart_studio.tools.set_credentials_file(username='******', api_key='zrVsh0tcfILLshqesfga') name = py.plot(fig, filename='Citations', auto_open=False) url = str(name) a = tls.get_embed(url) print(a)
def visualize_audio_similarities(df, cs_username, cs_api_key, similar_song_ids): songs = [] artists = [] for i in similar_song_ids[::-3]: artists.append(i) artist_cleaned = [] for i in artists: res = str(i)[1:-1] artist_cleaned.append(res) for i in similar_song_ids[1::3]: songs.append(i) """ """ vals = [36, 72, 108, 144, 180, 216, 252, 288, 324, 360] theta = [23, 59, 95, 131, 167, 203, 239, 275, 311, 347] k = 4 k2 = 8 k3 = 12 k4 = 16 k5 = 20 k6 = 24 theta2 = [x + k for x in theta] theta3 = [x + k2 for x in theta] theta4 = [x + k3 for x in theta] theta5 = [x + k4 for x in theta] theta6 = [x + k5 for x in theta] theta7 = [x + k6 for x in theta] width = 7 fig = go.Figure() fig.add_trace( go.Barpolar( r=df.iloc[0], theta=theta2, width=width, marker_color='red', marker_line_color="black", marker_line_width=2, opacity=0.9, name='Searched Song', hovertext=df.iloc[12], hoverinfo="text", )) fig.add_trace( go.Barpolar( r=df.iloc[2], theta=theta3, width=width, marker_color='gray', marker_line_color="black", marker_line_width=2, opacity=0.9, name=(f"{songs[0]} by {artist_cleaned[0]}"), hovertext=df.iloc[7], hoverinfo="text", )) fig.add_trace( go.Barpolar( r=df.iloc[3], theta=theta4, width=width, marker_color='violet', marker_line_color="black", marker_line_width=2, opacity=0.9, name=(f"{songs[1]} by {artist_cleaned[1]}"), hovertext=df.iloc[8], hoverinfo="text", )) fig.add_trace( go.Barpolar( r=df.iloc[4], theta=theta5, width=width, marker_color='purple', marker_line_color="black", marker_line_width=2, opacity=0.9, name=(f"{songs[2]} by {artist_cleaned[2]}"), hovertext=df.iloc[9], hoverinfo="text", )) fig.add_trace( go.Barpolar( r=df.iloc[5], theta=theta6, width=width, marker_color='blue', marker_line_color="black", marker_line_width=2, opacity=0.9, name=(f"{songs[3]} by {artist_cleaned[3]}"), hovertext=df.iloc[10], hoverinfo="text", )) fig.add_trace( go.Barpolar( r=df.iloc[6], theta=theta7, width=width, marker_color='orange', marker_line_color="black", marker_line_width=2, opacity=0.9, name=(f"{songs[4]} by {artist_cleaned[4]}"), hovertext=df.iloc[11], hoverinfo="text", )) fig.update_layout( template='none', width=800, height=800, hoverlabel=dict( namelength=-1, bgcolor="white", bordercolor='black', font_size=16, font_family="Rockwell", ), polar=dict( radialaxis=dict( range=[df[0:7].values.min(), df[0:7].values.max() + .1], showticklabels=False, ticks=''), angularaxis=dict(showticklabels=True, tickmode='array', tickvals=vals, ticktext=df.columns, ticks=''), ), title={ 'text': "<b>Comparing Audio Features</b>", 'y': 0.95, 'x': 0.51, 'xanchor': 'center', 'yanchor': 'top', 'font': { 'size': 20 } }, legend={ 'y': -0.33, 'x': 0.49, 'xanchor': 'center', 'yanchor': 'bottom' }, ) chart_studio.tools.set_credentials_file(username=cs_username, api_key=cs_api_key) embed_var = py.plot(fig, filename='pleasedonotbreakkk', auto_open=True, fileopt='new') embed_link = tls.get_embed(embed_var) return embed_link
def plot_pie(dataframe, name, value, title): plot1 = px.pie(dataframe, names=name, values=value, title=title) fig1 = py.plot(plot1, filename=title, auto_open=False) return tls.get_embed(fig1)
def plot_bar(dataframe, x, y, title): plot1 = px.bar(dataframe, x=x, y=y, color=x, title=title) fig1 = py.plot(plot1, filename=title, auto_open=False) return tls.get_embed(fig1)
import plotly.express as px import pandas as pd import csv import plotly.graph_objects as go import plotly import chart_studio.tools as tls ''' tls.get_embed('https://plot.ly/~test/1') #request filename #filen #file = '../files/{}'.format(filename) #file = f'../files/{filename}' #df = pd.read_csv('df.csv') df = pd.read_csv('dftest.csv', na_values=['#VALUE!', '#DIV/0!', '#NUM!', '#ERROR!']) #r = lambda x: x.isnumeric() #df[df.logFC.apply(r)] fc = df['logFC'] df = df['logpv'] def r(file, col): l = file[col] for i in l: if i == '#DIV/0!': l[l.index(i)] = 5 return l n = r(df, 'logFC') df['logFC1'] = n
# Using graph_objects import plotly.graph_objects as go import pandas as pd df = pd.read_csv( 'https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv' ) fig = go.Figure([go.Scatter(x=df['Date'], y=df['AAPL.High'])]) fig.show() py.plot(fig, filename='dummy_graph', auto_open=True) import chart_studio.tools as tls tls.get_embed('https://plotly.com/~abbybatinga/3/') df = px.data.tips() fig = px.pie(df, values='tip', names='day', color='day', color_discrete_map={ 'Thur': 'peachpuff', 'Fri': 'honeydew', 'Sat': 'mediumspringgreen', 'Sun': 'lavender' }) fig.show() py.plot(fig, filename='dummy_pie', auto_open=True)
def mapping(): df = pd.read_csv("https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/vaccinations/vaccinations.csv", usecols = ["location","iso_code","date","total_vaccinations"]) df = df[df["location"] != "World"] df.fillna(method="ffill",inplace=True) df.reset_index(drop=True,inplace=True) str_min_date = df["date"].min() df["date"] = df["date"].apply(lambda x: datetime.datetime.strptime(x,"%Y-%m-%d")) min_date = df["date"].min() max_date = df["date"].max() country_list = list(dict.fromkeys(zip(df["location"],df["iso_code"]))) for i in country_list: date_temp = min_date country_df = df[df["location"]==i[0]] country_min = country_df["date"].min() country_max = country_df["date"].max() vaccine_max = int(country_df[df["date"]==country_max]["total_vaccinations"]) while date_temp <= max_date: if date_temp < country_min: df.loc[len(df)] = [i[0],i[1],date_temp,0] elif date_temp > country_max: df.loc[len(df)] = [i[0],i[1],date_temp, vaccine_max] else: if date_temp not in country_df["date"].unique(): df.loc[len(df)] = [i[0],i[1],date_temp, np.nan] date_temp += datetime.timedelta(days=1) df.fillna(method="ffill",inplace=True) df.reset_index(drop=True,inplace=True) df = df.sort_values(by=["location","date"]).reset_index(drop=True) people = df[df["date"]==max_date]["total_vaccinations"].sum() country = len(df["location"].unique()) days = (max_date - min_date).days ppd = round(int(people)/int(days),1) df["date"] = df["date"].apply(lambda x: str(x)[:10]) fig = px.scatter_geo(df, locations="iso_code", locationmode="ISO-3", size="total_vaccinations", hover_name="location", animation_frame="date", animation_group="location", range_color=[833471,833471], width=1200, height=900) fig.layout.updatemenus[0].buttons[0].args[1]['frame']['duration'] = 10 fig.layout.updatemenus[0].buttons[0].args[1]['transition']['duration'] = 10 fig.update_layout({ "plot_bgcolor": "rgba(0,0,0,0)", "paper_bgcolor": "rgba(0,0,0,0)" }) fig.update_layout(geo=dict(bgcolor= 'rgba(0,0,0,0)')) username = '******' # your username api_key = 'A9iEh0PmfbcEmki2cptp' # your api key - go to profile > settings > regenerate key chart_studio.tools.set_credentials_file(username=username, api_key=api_key) a = py.plot(fig, filename = 'tempp', auto_open=False) iframe = tls.get_embed(a, height = "100%") return str_min_date, iframe, people, country, ppd
bandyaxis = go.layout.YAxis( title="$ E - E_f \quad / \quad \\text{eV} $", # 费米能级对其 titlefont=dict(size=20), range=[emin, emax], showgrid=True, showline=True, zeroline=True, mirror="ticks", ticks="inside", tickfont=dict(family='Times New Roman', size=20), linewidth=2, tickwidth=2, zerolinewidth=2) bandlayout = go.Layout(title="Bands diagram of Al", titlefont=dict(size=22, family='Times New Roman', color='grey'), xaxis=bandxaxis, yaxis=bandyaxis, annotations=annotations) bandfig = go.Figure(data=bandTraces + vlines, layout=bandlayout) plot_url = pltly.plot(bandfig, filename="Bands_Al", auto_open=True) print(tls.get_embed(plot_url)) # plotly.offline.iplot(bandfig, filename='Bands_Al', image='jpeg') # plotly.offline.iplot(bandfig, filename='Bands_Al') print(bandfig) plotly.io.write_image(bandfig, 'Bands_Al.jpeg')