def plot_raw_data(): fig = go.Figure() fig.add_trace(go.scatter(x=data['Date'], y=data['Open'], name='stock_open')) fig.add_trace( go.scatter(x=data['Date'], y=data['Close'], name='stock_close')) fig.layout.update(title_text="Time Series Data", xaxis_rangeslider_visible=True) st.plotly_chart(fig) plot_raw_data()
def update_graph(): global X global Y X.append(X[-1] + 1) Y.append(Y[-1] + (Y[-1] * random.uniform(-0.1, 0.1))) data = go.scatter(x=list(X), y=list(Y), name='scatter', mode='lines+markers') return { 'data': [data], 'layout': go.layout(xaxis=dict(range=[min(X), max(X)]), yaxis=dict(range=[min(Y), max(Y)])) }
def update_graph(): global X global Y X.append(X[-1] + 1) Y.append(Y[-1] + Y[-1] * random.uniform(-0.1, 0.1)) #scatter plot #takes a scatter graph and adds lines data = go.scatter(x=list(X), y=list(Y), name='Scatter', mode='lines+markers') #makes it so that the axis updates and increases as values are added return { 'data': [data], 'layout': go.Layout(xaxis=dict(xaxis=dict(range=[min(X), max(X)]), yaxis=dict(range=[min(Y), max(Y)]))) }
def new_graph(action, plotting): df = data.groupby(action).count()['action_taken'] # a = { # 'data': [go.Bar(x = [i for i in range(1,np.size(df.unique())+1)],y = df)], # 'layout': # go.Layout( # title='Showing the count of number of occurences of values of column {} in the dataset'.format(action), # barmode='stack') # } b = { 'data': [go.scatter(x=data[plotting], y=data[action_taken])], 'layout': go.Layout( title= 'Showing the count of number of occurences of values of column {} in the dataset' .format(action), barmode='stack') } return b
import plotly.graph_objs as go init_notebook_mode(connected=true) sys.path.append("".join([os.environ["home"]])) from sklearn.datasets import load_iris iris_data = load_iris() iris_data.feature_names x = [v[0] for v in iris_data.data] y = [v[1] for v in iris_data.data] trace = go.scatter( x =x, y =y, mode = 'makers' ) layout = go.layout(title="iris dataset", hovermode='closest', xaxis= dict(title='sepal length(cm)', ticklen=5, zeroline=false, gridwidth=2, ), yaxis=dict(title='sepal width (cm)', ticklen=5,gridwidth=2,), showlegend=false ) data = [trace]
""" PLotly is a modern platform for plotting and data visualization. Useful for producing a variety of plots, espacially for data sciences, Plotly is available for Python, R, JavaScript, Julia and Matlab. """ import plotly.graph_objs as go import plotly as ply import numpy as np N = 100 random_x = np.linspace(0, 1, N) random_y0 = np.random.randn(N) + 5 random_y1 = np.random.randn(N) random_y2 = np.random.randn(N) - 5 # Crete traces trace0 = go.scatter(x=random_x, y=random_y0, mode='lines', name='lines') trace1 = go.scatter(x=random_x, y=random_y1, mode='lines+markers', name='lines+markers') trace2 = go.scatter(x=random_x, y=random_y2, mode='markers', name='markers') data = [trace0, trace1, trace2] ply.offline.plot(data, filename='line-mode')
app = dash.Dash(__name__, external_stylesheets=external_stylesheets) df = pd.read_csv('sp500_1.csv') app.layout = html.Div([ dcc.Graph( id='rolling average', figure={ 'data': [ go.scatter(x=df[df['ticker'] == i]['High'], y=df[df['ticker'] == i]['Adj Close'], text=df[df['ticker'] == i]['Date'], mode='markers', opacity=0.7, marker={ 'size': 15, 'line': { 'width': 0.5, 'color': 'black' } }, name=i) #for i in df.Date.unique() ], 'layout': go.Layout(xaxis={ 'type': 'Log', 'title': 'Rolling Stock Average' }, yaxis={'title': 'rolling averag'}, margin={ 'i': 40,
all_data = pd.concat([train_df, test_df], axis=0, ignore_index=True).reset_index() # %% fig, ax = plt.subplots() t_df = all_data[['galactic year', 'y', 'galaxy']].set_index('galactic year').sort_index() t_df.groupby('galaxy')['y'].plot(ax=ax) # %% from plotly import graph_objs as go # %% fig = go.Figure() for name, group in t_df.groupby('galaxy'): trace = go.scatter() trace.name = name trace.x = group['y'] fig.add_trace(trace) # %% import plotly.express as px # %% test_df['y'] = np.nan all_data = pd.concat( [train_df, test_df], axis=0, ignore_index=True).sort_values('galactic year').reset_index() t_df = all_data[['galactic year', 'y', 'galaxy']] fig = px.scatter()(all_data, x='galactic year',
def plotly_scatter(a_df, a_title, a_columns=None): """:arg The figure data type for plotly is a root like structure with the 3 core elements in the first tier. - 'data': A list of the traces to add to the chart - 'layout': A plotly layout object with info on what to draw - 'frame': """ # Create the plot #chart = TraceList() #chart.columnOrder = a_df.columns #chart.create_traces_from_list(a_df.columns) print("plotly_scatter") ACCEPTED_COLUMN_TYPES = [np.float64, np.int64, float] _col_count = len(a_df.columns) _cols_loaded = 0 _data_points = 0 _axes = {"y1_min": 0, "y1_max": 0, "y2_min": 0, "y2_max": 0} # Create traces #fig = go.Figure() fig = sp.make_subplots(rows=2, cols=1) print( "plotly_scatter, initialise figure with 2 subplots. Parse over {} columns" .format(_col_count)) # Begin plotting for x in range(_col_count): _column = a_df.columns[x] if a_columns is not None and _column not in a_columns: print("plotly_scatter, Skipping {}".format(_column)) continue if a_df[_column].dtype not in ACCEPTED_COLUMN_TYPES: print("Column dtype {} for {} not valid for plotting.".format( a_df[_column].dtype, _column)) continue # Check the datatype of the column is valid # if a_df[_column].dtype == np.float64 or a_df[_column].dtype == np.int64 or a_df[_column].dtype is float: if a_df[_column].max() > 20000: print("plotly_scatter, add trace {} to subplot 1".format(_column)) _trace = go.scatter(x=a_df.index, y=a_df[_column], mode='lines', name=_column) fig.add_trace(_trace, row=1, col=1) # data.append(go.Scatter(x=a_df.index, y=a_df[_column], name=_column, yaxis='y2', mode='lines')) if a_df[_column].min() < _axes['y1_min']: _axes['y1_min'] = a_df[_column].min() if a_df[_column].max() < _axes['y1_max']: _axes['y1_max'] = a_df[_column].max() else: print("plotly_scatter, add trace {} to subplot 2".format(_column)) _trace = go.scatter(x=a_df.index, y=a_df[_column], mode='lines', name=_column) fig.append_trace(_trace, row=2, col=1) # data.append(go.Scatter(x=a_df.index, y=a_df[_column], name=_column, mode='lines')) if a_df[_column].min() < _axes['y2_min']: _axes['y2_min'] = a_df[_column].min() if a_df[_column].max() < _axes['y2_max']: _axes['y2_max'] = a_df[_column].max() _cols_loaded += 1 _data_points += len(a_df[_column]) #layout = go.Layout(title=a_title, yaxis=dict(title='Various'), xaxis=dict(title='Quantity'), # yaxis2=dict(title='Price', titlefont=dict(color='rgb(148, 103, 189)'), # tickfont=dict(color='rgb(148, 103, 189)'), overlaying='y', side='right')) #else: #print("reject dtype for ", _column, a_df[_column].dtype) #continue # logger.info("Added column {} to chart. {:.1f}% complete.".format(_column, (x/_col_count)*100)) print("{} data points ready to plot across {} columns and {} rows.".format( _data_points, _cols_loaded, len(a_df.index))) #fig = {'data': data, 'layout': layout, 'frame': None} return fig