def update_scatter(value, selection): lower = value[0] upper = value[1] current_data_m = df_m[(df_m["Age"] >= lower) & (df_m["Age"] <= upper)] current_data_f = df_f[(df_f["Age"] >= lower) & (df_f["Age"] <= upper)] fig2 = go.Figure() fig2.add_trace(go.Scattergl( x=current_data_m["Age"], y=current_data_m[selection], mode='markers', name='Men', marker=dict(color='#29A982', size=10, colorscale='Viridis') )) fig2.add_trace(go.Scattergl( x=current_data_f["Age"], y=current_data_f[selection], mode='markers', name='Women', marker=dict(color='#44367F', size=10, colorscale='Viridis') )) fig2.update_layout(template=template) return fig2
def expvar_evaluation(explained_variances, title='Explained Variance Evaluation', out_path=None): # Evaluate by the variance, and try to preserve variance as high as 90% expvar_percentages = explained_variances / np.sum(explained_variances) cumexpvar_percentages = np.cumsum(expvar_percentages) data = [] # Scree plot data.append( go.Scattergl(x=[x for x in range(1, len(expvar_percentages) + 1)], y=expvar_percentages, mode='lines+markers')) # Cumulative explained variance % data.append( go.Scattergl(x=[x for x in range(1, len(cumexpvar_percentages) + 1)], y=cumexpvar_percentages, mode='lines+markers')) subplot_titles = ['Scree Plot', 'Cumulative Explained Variance (%)'] vp.plot_subplots(data, max_col=2, title=title, subplot_titles=subplot_titles, out_path=out_path)
def hf_plot(df_high, bore): ''' high frequency pressure and oil rate plot using plotly Parameters: df_high: list of pd.DataFrames list of high frequency data frames, [3] is expected to be oil rate bore: list of str bore names ''' fig = make_subplots(specs=[[{"secondary_y": True}]]) fig.update_layout( autosize=True, height=800) for i in range(3): fig.add_trace(go.Scattergl(x=df_high[i]['*DATE'], y=df_high[i]['*DHP '+bore[i]], name=bore[i], mode='markers')) fig.add_trace(go.Scattergl(x=df_high[3]['*DATE'], y=df_high[3]['*rOIL'], name='Oil Rate', mode='lines+markers',\ marker_color='black'), secondary_y=True) fig.update_layout( title="High Frequency Data", xaxis_title="Date", yaxis_title='Pressure (bar)', yaxis2_title="Rate (m\u00b3)", font=dict( family="Courier New, monospace", size=26, color="#7f7f7f")) fig.show()
def get_line_overlay2(idx, title, xs, ys0, ys1, names, x_label, y_label): bottom_y=[] color1 = 'rgba(3, 128, 166, 0.5)' color2 = 'rgba(217, 44, 22, 0.5)' for i in range(len(xs)): if ys0[i] > ys1[i]: bottom_y.append(ys1[i]) else: bottom_y.append(ys0[i]) fig = go.Figure() fig.add_trace( go.Scattergl( x=xs, y= ys0, mode= 'lines', fill='tozeroy', fillcolor=color1, name=names[0] ) ) fig.add_trace( go.Scattergl( x=xs, y= ys1, mode='lines', fill='tozeroy', fillcolor=color2, name=names[1] ) ) fig.add_trace( go.Scattergl( # THIS IS THE WHITE FILL HERE x=xs, y=bottom_y , mode= 'none', fill='tozeroy', fillcolor='rgb(255, 255, 255)', showlegend=False ) ) fig.add_trace( go.Scattergl( x=xs, y=ys0, mode='lines', line=dict(color=color1), showlegend=False ) ) fig.add_trace( go.Scattergl( x=xs, y=ys1, mode='lines', line=dict(color=color2), showlegend=False ) ) fig.update_layout( title = title, xaxis_title = x_label, yaxis_title = y_label, plot_bgcolor = 'rgb(255, 255, 255)', paper_bgcolor = 'rgb(245, 245, 245)' ) return dcc.Graph( id='overlay' + str(idx), figure=fig )
def prob(df, title='Probability', out_path=None, max_col=2, layout_kwargs={}, to_image=False): columns = df.select_dtypes(include='number') data_groups = [] colors = DEFAULT_PLOTLY_COLORS for column in columns: (osm, osr), (slope, intercept, r) = probplot(df[column]) line_points = np.array([osm[0], osm[-1]]) data = [] data.append(go.Scattergl( x=osm, y=osr, mode='markers', showlegend=False, marker={'color': colors[0]} )) data.append(go.Scattergl( x=line_points, y=intercept + slope * line_points, mode='lines', showlegend=False, marker={'color': 'red'}, )) data_groups.append(data) datagroups_subplots(data_groups, max_col=max_col, title=title, out_path=out_path, xaxis_titles=['Theoretical Quantiles' for _ in columns], yaxis_titles=['Ordered Values' if i % max_col == 0 else None for i,_ in enumerate(columns)], subplot_kwargs={'subplot_titles': list(columns)}, layout_kwargs=layout_kwargs, to_image=to_image)
def plot_the_result(dict_mdp, mdp_challenge): edge_list=topology_to_edge_list(mdp_challenge['T']) g = ig.Graph(edge_list) g.vs["name"] = dict_mdp['S'] g.vs["reward"] = dict_mdp['R'] g.vs["label"] = g.vs["name"] P_2D=list(mdp_challenge['P'].values()) x_vec = [wlt[0] for wlt in P_2D] y_vec = [wlt[1] for wlt in P_2D] layout = ig.Layout(P_2D) g.vs["vertex_size"] = 20 visual_style = {} visual_style["edge_curved"] = False colors = [(1, 0, 1) for i in range(0, len(dict_mdp['S']))] g.vs["color"] = colors fig = go.Figure() fig.add_trace(go.Scattergl(x=x_vec, y=y_vec, text=dict_mdp['S'], mode='markers', name='grid_points')) fig.add_trace(go.Scattergl(x=x_vec, y=y_vec, mode='markers', name='value_markers', marker=dict(size=dict_mdp['U'], color=dict_mdp['U']) )) fig.show()
def plotAudio(self, fig): if self.audioData is None: return recordOffset = 1.0 - 0.06 t0 = self.t0 / 1000000 mcuSampleLength = (self.meta.getElapsedNsMcu() / 1000000000) readRange = (recordOffset, recordOffset + mcuSampleLength) displayRange = (t0, t0 + mcuSampleLength) first = int(readRange[0] * self.audioFs) last = int(readRange[1] * self.audioFs) tWav = np.linspace(displayRange[0] * 1000, displayRange[1] * 1000, num=(last - first)) lowcut = keyFrequencies[self.sensorName] * (1.0 - maxKeyFreqDeviation) highcut = keyFrequencies[self.sensorName] * (1.0 + maxKeyFreqDeviation) fig.add_trace(go.Scattergl( name='{} wav (l)'.format(self.getIdentifier()), x=tWav, y=butter_bandpass_filter(self.audioData[first:last,0], lowcut, highcut, self.audioFs), yaxis=plotAxisWav, )) fig.add_trace(go.Scattergl( name='{} wav (r)'.format(self.getIdentifier()), x=tWav, y=butter_bandpass_filter(self.audioData[first:last,1], lowcut, highcut, self.audioFs), yaxis=plotAxisWav, ))
def plot_probs(df, probs, midpoint=0.5, add_labeled_points=None, soft_labels=True, subset=None): """Plot data points with hard labels or estimated probability of one class""" if soft_labels: probs = probs[:, 1] if subset is not None: df = df.iloc[subset, :] probs = probs[subset] df["label"] = probs fig = go.Figure( go.Scattergl( x=df["x1"], y=df["x2"], mode="markers", hovertext=df["label"], hoverinfo="text", marker=dict( size=6, color=df["label"], colorscale=px.colors.diverging.Geyser, colorbar=dict(title="Labels"), cmid=midpoint), showlegend=False)) if add_labeled_points is not None: fig.add_trace( go.Scattergl( x=df["x1"].values[add_labeled_points], y=df["x2"].values[add_labeled_points], mode="markers", marker=dict( size=6, color=df["y"].values[add_labeled_points], colorscale=px.colors.diverging.Geyser, line=dict(width=1.5), opacity=1, cmid=midpoint), showlegend=False)) fig.update_layout( yaxis=dict(scaleanchor="x", scaleratio=1), width=700, height=700, xaxis_title="x1", yaxis_title="x2", template="plotly_white") return fig
def plot_statistics(self): y = np.array([ word['alpha'] for word in self.word_statistics.values() if word['alpha'] ]) x = np.array([i for i in range(len(y))]) fig = go.Figure(data=go.Scattergl(x=x, y=y, mode='markers')) # fig.show() y = np.array([ beta for word in self.word_statistics.values() if word['beta'] for beta in word['beta'] ]) x = np.array([ idx for idx, word in enumerate(self.word_statistics.values()) if word['beta'] for beta in word['beta'] ]) fig = go.Figure(data=go.Scattergl(x=x, y=y, mode='markers')) # fig.show() y = np.array([ beta - word['alpha'] / beta for word in self.word_statistics.values() if word['beta'] and word['alpha'] for beta in word['beta'] ]) x = np.array([i for i in range(len(y))]) fig = go.Figure(data=go.Scattergl(x=x, y=y, mode='markers')) fig.show()
def arc(adjacency_list): N = len(adjacency_list) vcoords = { ID: (idx / (N - 1), 0) for idx, ID in enumerate(adjacency_list.keys()) } fig = go.Figure() for vertex, neighbour, attributes in edge_iterator(adjacency_list): if vertex < neighbour: scx, scy = semi_circle(vcoords[vertex][0], vcoords[neighbour][0]) fig.add_trace( go.Scattergl(x=scx, y=scy, mode='lines', line_width=1, line_color='red', showlegend=False)) fig.add_trace( go.Scattergl(x=np.arange(N) / (N - 1), y=np.zeros(N), mode='markers', marker_size=10, marker_color='blue', showlegend=False, text='a')) fig.update_xaxes(tickvals=[], zeroline=False) fig.update_yaxes(tickvals=[], zeroline=False, scaleanchor='x') fig.update_layout(height=HEIGHT, width=HEIGHT - 20) fig.show() return
def plot_timeline(df: "data frame"): """ Note that this graph does not use xAxis nor indices params Only there as part of graph standardization in creating """ # Pull Specific Variables fs = df['fs'].tolist()[0] head = df['Heading'].tolist() p = df['Depth'].tolist() roll = df['Roll'].tolist() pitch = df['Pitch'].tolist() # Calculate time numData = len(p) t = [x/fs for x in range(numData)] t_hr = [x/3600 for x in t] ''' Added code to reduce the lag of the Figure ''' # Scaling Factor to reduce amount of data # A factor of 10 will reduce the data for example from 50 Hz to 5 Hz scale = 10 # Reduce Data sP = sg.decimate(p,scale).copy() sRoll = sg.decimate(roll,scale).copy() sPitch = sg.decimate(pitch,scale).copy() sHead = sg.decimate(head,scale).copy() # Calculate time - Reduced numData = len(sP) sT = [x/(fs/scale) for x in range(numData)] sT_hr = [x/3600 for x in sT] # Make Widget Figure fig = go.Figure( make_subplots( # Deifne dimensions of subplot rows = 2, cols=1, # Define what plot goes where and the type of plot specs = [[{}], [{}]], shared_xaxes = True ) ) # Create traces for the data and add to figure fig.add_trace(go.Scattergl(x = sT_hr, y = sP, mode = "lines", name = "Depth"), row = 1, col = 1) fig.add_trace(go.Scattergl(x = sT_hr, y = sHead, mode = "lines", name = "Head"), row = 2, col = 1) fig.add_trace(go.Scattergl(x = sT_hr, y = sPitch, mode = "lines", name = "Pitch"), row = 2, col = 1) fig.add_trace(go.Scattergl(x = sT_hr, y = sRoll, mode = "lines", name = "Roll" ), row = 2, col = 1) # Update x-axis fig.update_xaxes(title = "Time (hr)", rangeslider = dict(visible = True), row = 2, col = 1) # Update y-axis fig.update_yaxes(title = "Depth (m)", autorange = "reversed", row = 1, col = 1) return [fig]
def threefigs(df): fig = make_subplots( rows=3, cols=1, specs=[[{ 'secondary_y': True }], [{}], [{}]], shared_xaxes=True, ) # Top # NH4 trace_nh4 = go.Scattergl( x=df.index, y=df['pilEAUte-Pilote effluent-Varion_002-NH4_N'] * 1000, name='Ammonia', mode='lines', line=dict(dash='solid', color='blue'), ) fig.add_trace(trace_nh4, row=1, col=1, secondary_y=False) # NO3 trace_no3 = go.Scattergl( x=df.index, y=df['pilEAUte-Pilote effluent-Varion_002-NO3_N'] * 1000, name='Nitrate', mode='lines', line=dict(dash='solid', color='turquoise')) fig.add_trace(trace_no3, row=1, col=1, secondary_y=False) # AvN trace_avn = go.Scattergl( x=df.index, y=df['pilEAUte-Pilote effluent-Varion_002-NH4_N'] / df['pilEAUte-Pilote effluent-Varion_002-NO3_N'], name='AvN ratio', mode='lines+markers', line=dict(dash='solid', color='orange'), marker={'opacity': 0}) fig.add_trace(trace_avn, row=1, col=1, secondary_y=True) # Middle df_mid = df.sort_index().rolling('300s').mean() flow_trace = go.Scattergl( x=df_mid.index, y=df_mid['pilEAUte-Pilote reactor 5-FIT_430-Flowrate (Gas)'] * 1000 * 60, name='Aeration flow', mode='lines', line=dict(dash='solid', color='red'), ) fig.add_trace(flow_trace, row=2, col=1) fig.add_trace(flow_trace, row=3, col=1) # layout layout = dict(title='') return fig
def abnormal_outliner(valueName1, valueName2): dataFrame = df[[valueName1, valueName2]] dataFrame.replace("", np.nan, inplace=True) dataFrame.dropna(inplace=True) valueName1_out = dataFrame[ (dataFrame[valueName1] < blood_normals_male[valueName1][0]) | (dataFrame[valueName1] > blood_normals_male[valueName1][1])] valueName2_out = dataFrame[ (dataFrame[valueName2] < blood_normals_male[valueName2][0]) | (dataFrame[valueName2] > blood_normals_male[valueName2][1])] valueName1_valueName2_out = dataFrame[ ((dataFrame[valueName2] < blood_normals_male[valueName2][0]) | (dataFrame[valueName2] > blood_normals_male[valueName2][1])) & ((dataFrame[valueName1] < blood_normals_male[valueName1][0]) | (dataFrame[valueName1] > blood_normals_male[valueName1][1]))] print( len(valueName1_out), len(valueName2_out), len(valueName1_valueName2_out), len(dataFrame), ) trace0 = go.Scattergl( x=dataFrame[valueName1], y=dataFrame[valueName2], mode="markers", name="Both Normal", marker=dict(color="#28AD28"), ) trace1 = go.Scattergl( x=valueName1_out[valueName1], y=valueName1_out[valueName2], mode="markers", name=valueName1 + " Not Normal", marker=dict(color="#16DED1"), ) trace2 = go.Scattergl( x=valueName2_out[valueName1], y=valueName2_out[valueName2], mode="markers", name=valueName2 + " Not Normal", marker=dict(color="#B510EA"), ) trace3 = go.Scattergl( x=valueName1_valueName2_out[valueName1], y=valueName1_valueName2_out[valueName2], mode="markers", name="Both Not Normal", marker=dict(color="#CD1616"), ) return [trace0, trace1, trace2, trace3]
def update_graph(click, ranking, checkbox_checked): top_rank = ranking df_in = df[df["rank"] <= top_rank] df_out = df[df["rank"] > top_rank] fig = go.Figure() # conjunto do ranking for index, row in df_in.iterrows(): fig.add_trace(go.Scattergl( x=[row[x_text]], y=[row[y_text]], mode='markers', text=[row['Player']], name=row['Player'], marker_size=20, hovertemplate=hover, customdata=[[row['Team']]], marker={'color': row['rank'], 'sizeref': .305, })) # conjunto fora do ranking if checkbox_checked: for index, row in df_out.iterrows(): fig.add_trace(go.Scattergl( x=[row[x_text]], y=[row[y_text]], mode='markers', text=[row['Player']], hovertemplate=hover, marker_size=10, customdata=[[row['Team']]], opacity=0.3, name=row['Player'], marker={'sizemode': 'area', 'sizeref': .305, 'color': 'gray'})) # Set axes ranges fig.update_xaxes(range=[x_min, x_max], title_text=x_text) fig.update_yaxes(range=[y_min, y_max], title_text=y_text) fig.update_layout(title_text=title, title_x=0.5) fig.add_shape(type='line', x0=0, y0=df_in[y_text].mean(), x1=x_max, y1=df_in[y_text].mean(), line=dict(color="gray", width=1.5, dash='dot')) fig.add_shape(type='line', x0=df_in[x_text].mean(), y0=0, x1=df_in[x_text].mean(), y1=y_max, line=dict(color="gray", width=1.5, dash='dot')) return fig
def create_trace_fxd(fxd_data): trace = [] print("Creating Slice Fixation Trace") end_time = max(fxd_data['time']) trace.append( go.Scattergl(name='Total Fixation', visible=False, hoverinfo='none', x=fxd_data['x'], y=fxd_data['y'], mode='markers', marker=dict(opacity=0.20, color='white', size=fxd_data['duration'] / 4, line=dict(width=0)))) for initial_time in np.arange(0, end_time, TIMESTEP): final_time = initial_time + TIMESTEP fxd_subset = fxd_data[fxd_data['time'].between(initial_time, final_time)] start_time = datetime.datetime.fromtimestamp(initial_time / 1000).strftime("%M:%S") end_time = datetime.datetime.fromtimestamp(final_time / 1000).strftime("%M:%S") trace.append( go.Scattergl( name=start_time + '-' + end_time, visible=False, #hoverinfo = 'none', x=fxd_subset['x'], y=fxd_subset['y'], #mode = 'lines+markers', mode='markers+lines', marker=dict( #opacity = 1.0, color='limegreen', #color = 'black', line=dict(color='limegreen', width=1), size=fxd_subset['duration'] / 4, #size = 25 ), line=dict( #color = '#708090', color='red', width=2), text=fxd_subset['duration'], hoverinfo='text', hovertemplate="<b> Duration: %{text}ms</b><extra></extra>", )) return trace
def plot_accelerations(self): """ Plots the unsorted acceleration signals for a given trip. """ fig = make_subplots(rows=8, cols=1, shared_xaxes=True, vertical_spacing=0) for index_side, side in enumerate(self.sides): for index_sensor, sensor in enumerate(self.bearings_labels[side]): # Odd Part # Complete Shifted Signal fig.add_trace( go.Scattergl( x=self.positions["S"][0], y=self.accel[side][index_sensor], mode='lines', name='Signal_{}{}'.format(side, sensor), line=dict(color='blue', width=1), ), row=index_sensor + 1 + 4 * index_side, col=1, ) # Shifted Weldings fig.add_trace( go.Scattergl( x=self.positions["S"][0][self.shifted_weldings[side] [index_sensor]], y=self.accel[side][index_sensor][ self.shifted_weldings[side][index_sensor]], mode='markers', name='North Signal {}'.format(sensor), line=dict(color='black', width=20), ), row=index_sensor + 1 + 4 * index_side, col=1, ) fig.update_layout( height=1024, width=2048, title_text="Vibration Collection: T:{} D:{} S:{} C:{} UUID:{}". format(self.train, self.direction, self.avg_speed, self.component, self.uuid), showlegend=False) fig.write_image( "private/images/Vibration_T_{}_D_{}_S_{}_C_{}_U{}.png".format( self.train, self.direction, self.avg_speed, self.component, self.uuid))
def make_scatterplot(reduced_df, ports_dfs_clean, leads_dfs_clean): fig = go.Figure() # For large data volumes, use Scattergl fig.add_trace( go.Scattergl( x=reduced_df['x'], y=reduced_df['y'], mode='markers', name='Base', marker={ 'color': 'rgba(150, 150, 150, 0.5)', 'size': 16, }, )) for i in range(n_of_portfolios): fig.add_trace( go.Scattergl( x=ports_dfs_clean[i]['x'], y=ports_dfs_clean[i]['y'], mode='markers', name=f'Portfólio {i+1}', opacity=0.7, marker={ 'size': 16, 'line': { 'width': 1, 'color': 'DarkSlateGrey' }, }, )) fig.add_trace( go.Scattergl( x=leads_dfs_clean[i]['x'], y=leads_dfs_clean[i]['y'], mode='markers', name=f'Leads {i+1}', opacity=0.5, marker={ 'size': 16, 'line': { 'width': 1, 'color': 'DarkSlateGrey' }, }, )) return fig
def kde(df, title='KDE', color=None, out_path=None, max_col=2, layout_kwargs={}, to_image=False): columns = df.select_dtypes(include='number') columns = [x for x in columns if x != color] data_groups = [] groups = [1] if color is None else np.unique(df[color]) for column in columns: data = [] colors = DEFAULT_PLOTLY_COLORS for index, group in enumerate(groups): tmp_df = df if color is None else df[df[color] == group] kde = sm.nonparametric.KDEUnivariate(tmp_df[column].astype('float')) kde.fit() data.append(go.Scattergl( x=kde.support, y=kde.density, name=column if color is None else f'{column}: {group}', marker={'color': colors[index % len(colors)]} )) data_groups.append(data) if color is None: layout_kwargs['showlegend'] = False datagroups_subplots(data_groups, max_col=max_col, title=title, out_path=out_path, xaxis_titles=columns, layout_kwargs=layout_kwargs, to_image=to_image)
def _get_figure_data(self, df: pd.DataFrame) -> List[go.Scattergl]: """ casts data within a pandas object into a list of dash object. iterate over each all planet slots available for that universe and create a scatter char with for each slot (same slot -> same radius). TODO change color intensity with eco score of each planet to represent juiciness """ query_str = 'planet == @planet_slot' data = [ go.Scattergl({ 'x': (df.query(query_str)['x'] * df.query(query_str)['r']), 'y': (df.query(query_str)['y'] * df.query(query_str)['r']), 'mode': 'markers', 'marker': { 'symbol': 'circle', 'size': [6 for elm in range(len(df.query(query_str)))], 'color': df.query(query_str)['taken'].apply( lambda x: '#66ff66' if x == 0 else '#ff6666'), }, 'name': f'{planet_slot}. slot', 'hoverinfo': 'text', 'hovertext': df.query(query_str)['coords'], 'showlegend': False, }) for planet_slot in self.planets_range ] return data
def build_figure(lv5, sample, lidar, camera, overlay): points, coloring, im = compute_pointcloud_for_image( lv5, sample["token"], pointsensor_channel=lidar, camera_channel=camera) if "boxes" in overlay: im = render_box_in_image(lv5, im, sample, camera_channel=camera) fig = px.imshow(im, binary_format="jpeg", binary_compression_level=2) if "pointcloud" in overlay: fig.add_trace( go.Scattergl( x=points[0, ], y=points[1, ], mode="markers", opacity=0.4, marker_color=coloring, marker_size=3, )) fig.update_layout( margin=dict(l=10, r=10, t=0, b=0), paper_bgcolor="rgba(0,0,0,0)", plot_bgcolor="rgba(0,0,0,0)", hovermode=False, ) fig.update_xaxes(showticklabels=False, showgrid=False, range=(0, im.size[0])) fig.update_yaxes(showticklabels=False, showgrid=False, range=(im.size[1], 0)) return fig
def _get_plots(): """Generate plots based on the dataframe df, yaxis and xaxis values present within the respective widgets and flags in seperate colors""" plots = [] for flag_name, flag_value in flags.items(): if type(flag_value) is dict and "Color" in flag_value: flag_color = flag_value["Color"] flag_meaning = flag_value["Meaning"] else: flag_color = flag_value flag_meaning = flag_value df_temp = get_filtered_data(df) df_flag = df_temp.loc[df_temp[yaxis.value + review_flag] == flag_name] plots += [ go.Scattergl( x=df_flag[xaxis.value], y=df_flag[yaxis.value], mode="markers", name=flag_meaning, marker={ "color": flag_color, "opacity": 1 }, ) ] return tuple(plots)
def yearPlot(): """ Overview: This function takes in a list of dicoese and plots a series of bar charts according to entrants per year """ year = input("Enter the year you would like to view.") fig = go.Figure([go.Scattergl()]) #extract needed sections of file and add a frequency column fileFiltered = file.drop().where(file["YearEntered"] == year) #create a new trace for each dicoese for i in range(0, len(fileFiltered)): fig.add_trace(go.Bar(x=fileFiltered["Diocese"])) # Add range slider fig.update_layout(xaxis=go.layout.XAxis(rangeselector=dict( buttons=list([ dict(count=100, label="100y", step="year", stepmode="backward"), dict(count=50, label="50y", step="year", stepmode="backward"), dict(count=20, label="20y", step="year", stepmode="backward"), dict(count=10, label="10y", step="year", stepmode="backward"), dict(step="all") ])), rangeslider=dict(visible=True), type="date")) fig.show()
def fig_tsne(dataframe): """Figure of topics projection on to 2D space using t-SNE""" fig = go.Figure(data=go.Scattergl(x=dataframe['X-tsne'], y=dataframe['Y-tsne'], mode='markers', marker_color=dataframe['Topic'], text=dataframe['Full_text'], hoverinfo=['text'])) fig.update_layout(template='plotly_dark', plot_bgcolor='rgba(0, 0, 0, 0)', paper_bgcolor='rgba(0, 0, 0, 0)') return fig
def updateTempPlot(standardDate, customStart, customEnd, tempUnit, n): records = ph.fetchSensorData(connPool, tempUnit, standardDate, [customStart, customEnd]) weather = ph.fetchWeatherDataNewTimeRange(connPool, tempUnit, standardDate, [customStart, customEnd]) records = ph.correctTemp(records, tempUnit) fig = ph.temp_vs_time(records, tempUnit) fig.add_trace( go.Scattergl(x=weather.ts, y=weather[tempUnit], mode='markers+lines', line={"color": "rgb(175,175,175)"}, hovertemplate='%{y:.1f}', name='Official outside')) currentRecords = ph.fetchSensorData(connPool, tempUnit, '1 day') currentWeather = ph.fetchWeatherDataNewTimeRange(connPool, tempUnit, '1 day') currentRecords = ph.correctTemp(currentRecords, tempUnit) try: currSensorStatement = 'Current sensor temperature: {:.0f}°'.format( currentRecords.iloc[0][tempUnit]) currWeatherStatement = 'Current outside temperature: {:.1f}°'.format( currentWeather.iloc[0][tempUnit]) except IndexError as e: print(e) currSensorStatement = 'Current sensor temperature: Unknown' currWeatherStatement = 'Current outside temperature: Unknown' return fig, currSensorStatement, currWeatherStatement
def _plot_obs_timeseries(fig, timeseries_value_df, timeseries_meta_df): # construct graph objects in random hash order. collect them in a list # along with the pair index. Then add traces in order of pair index. gos = [] # construct graph objects in random hash order for obs_hash in np.unique(timeseries_meta_df['observation_hash']): metadata = _extract_metadata_from_df( timeseries_meta_df, obs_hash, 'observation_hash') pair_idcs = timeseries_value_df['pair_index'] == metadata['pair_index'] plot_kwargs = plot_utils.line_or_step_plotly( metadata['interval_label']) data = _fill_timeseries( timeseries_value_df[pair_idcs], metadata['interval_length'], ) if data['observation_values'].isnull().all(): continue go_ = go.Scattergl( y=data['observation_values'], x=data.index, name=_legend_text(metadata['observation_name']), legendgroup=metadata['observation_name'], showlegend=True, marker=dict(color=metadata['observation_color']), connectgaps=False, **plot_kwargs) # collect in list gos.append((metadata['pair_index'], go_)) # Add traces in order of pair index for idx, go_ in sorted(gos, key=lambda x: x[0]): fig.add_trace(go_)
def line(df, xy_tuples, title='Line', out_path=None, max_col=2, layout_kwargs={}, to_image=False, line_kwargs={}, scattergl=False): data_groups = [] colors = DEFAULT_PLOTLY_COLORS for index, (x, y) in enumerate(xy_tuples): if scattergl: data = [] data.append(go.Scattergl( x=df[x].values, y=df[y].values, showlegend=False, marker={'color': colors[0]}, **line_kwargs )) data_groups.append(data) else: fig = px.line(df, x=x, y=y, **line_kwargs) if index != 0: for data in fig['data']: data['showlegend'] = False data_groups.append(fig['data']) datagroups_subplots(data_groups, max_col=max_col, title=title, out_path=out_path, xaxis_titles=[xy[0] for xy in xy_tuples], yaxis_titles=[xy[1] for xy in xy_tuples], layout_kwargs=layout_kwargs, to_image=to_image)
def dioceseTime(ax, diocese): """ Overview: This function takes in a list of dicoese and plots a series of bar charts according to entrants per year """ fig = go.Figure([go.Scattergl()]) dicoese = diocese.split(",") #extract needed sections of ax and add a frequency column ax = ax.groupby(["YearEntered", "Diocese"]).size().reset_index(name="Diocese Frequency") #create a new trace for each dicoese for i in range(0, len(dicoese)): dio = ax.where(ax["Diocese"] == dicoese[i]) year = dio["YearEntered"] freq = dio["Diocese Frequency"] fig.add_trace(go.Bar(x=year, y=freq, name=dicoese[i])) # Add range slider fig.update_layout(xaxis=go.layout.XAxis(rangeselector=dict( buttons=list([ dict(count=100, label="100y", step="year", stepmode="backward"), dict(count=50, label="50y", step="year", stepmode="backward"), dict(count=20, label="20y", step="year", stepmode="backward"), dict(count=10, label="10y", step="year", stepmode="backward"), dict(step="all") ])), rangeslider=dict(visible=True), type="date")) fig.show()
def plot_cummulative_sampling_fraction( df ): df["epiweek"] = df["date"].apply( lambda x: Week.fromdate(x).startdate() ) plot_df = df.groupby( "epiweek" ).agg( new_cases = ("new_cases", "sum"), new_sequences = ("new_sequences", "sum" ) ) plot_df = plot_df.loc[plot_df["new_sequences"]>0] plot_df["fraction"] = plot_df["new_sequences"] / plot_df["new_cases"] plot_df = plot_df.reset_index() fig = go.Figure() fig.add_trace( go.Scattergl( x=plot_df["epiweek"], y=plot_df["fraction"], mode='lines', name='Fraction', line={ "color" : '#767676', "width" : 4 } ) ) _add_date_formating( fig ) fig.update_layout( yaxis_tickformat='.1%' ) cleaned_array = np.log10( plot_df.loc[plot_df["fraction"] > 0, "fraction"] ) cleaned_array = cleaned_array[~np.isinf( cleaned_array )] min_lim = np.floor( cleaned_array.min() ) max_lim = np.ceil( cleaned_array.max() ) fig.update_yaxes( type="log", title="<b>Cases sequenced (%)</b>" ) fig.update_xaxes( range=get_date_limits( plot_df["epiweek"] ) ) return fig
def vis_2d(vec_2d, lbs): fig = go.Figure() for i, cl in enumerate(set(lbs)): cluster_samples = np.argwhere(lbs == cl) features = vec_2d[cluster_samples] features = features.squeeze() # normalize the features between 0 and 1 min_ = features.min(axis=0) max_ = features.max(axis=0) features = (features - min_) / (max_ - min_) fig.add_trace( go.Scattergl(x=features[:, 0], y=features[:, 1], mode='markers', showlegend=True, marker=dict(size=3, color=i), name="cluster_" + str(cl + 1))) fig.update_layout(title='Distribution of jobs over clusters in 2d Space', legend=dict(x=0.6, y=1.4, traceorder='normal', font=dict(size=10))) fig.show()
def _plot_raster_plotly(x, y, ids, fig=None, labels=None, show=True, **kwargs): if not fig: fig = go.Figure() if labels: # Turn into one label for each line labels = [labels[i] for i in y[::3]] ht = np.vstack( (labels, labels, [None] * len(labels))).T.flatten().tolist() else: ht = None fig.add_trace( go.Scattergl(x=x, y=y, mode='lines', hovertext=ht, hoverinfo='text' if labels else 'x+y', line=dict(color=kwargs.get('color', 'black'), width=kwargs.get('width', 2.5)))) fig.update_layout(paper_bgcolor='rgba(0,0,0,0)', height=kwargs.get('height', 1000), xaxis_title="time [ms]", plot_bgcolor='rgba(0,0,0,0)') if show: fig.show() return fig