def generate_parcats(dataset, dimensions, title): df = dataset.copy() df = df.sort_values(by=dimensions[0]) dimensions_dict = [] for i in dimensions_dict: dimensions.append({'label': i, 'values': list(df[i])}) fig = go.Figure(go.Parcats(dimensions=dimensions_dict)) dimensions_parcast = [] for i in dimensions: dimensions_parcast.append(go.parcats.Dimension(values=df[i], label=i)) fig = go.Figure(data=[ go.Parcats(dimensions=dimensions_parcast, hoveron='color', hoverinfo='count+probability', labelfont={ 'size': 18, 'family': 'Times' }, tickfont={ 'size': 16, 'family': 'Times' }, arrangement='freeform') ], layout=make_title(title)) return fig
def update_figure(selected_category,selected_date): filtered_df = df[df['Date'] == selected_date ] dff=filtered_df[filtered_df['Category']== selected_category] category_dim = go.parcats.Dimension( values=dff['PdDistrict'].tolist(), label="District" ) resolution_dim = go.parcats.Dimension( values=dff['Resolution'].tolist(), label="Resolution" ) day_dim=go.parcats.Dimension( values=dff['Period'].tolist(), label="Time Period" ) data=[go.Parcats(dimensions=[category_dim,resolution_dim,day_dim], #line={'shape': 'hspline','colorbar':{'lenmode':'pixels','len':30}}, line={'shape':'hspline'}, hoverinfo = 'count+probability',arrangement='freeform')] return { 'data':data }
def update_expl_vis_parcats(features, df_input): # print('Draw parcats') order = ['Country'] + [feature for feature in features] group_by = ['Region', 'Country', 'Attack Type', 'Weapon Type', 'Suicide', 'Success'] agg_on = {'eventid': ['size'], 'Killed': ['sum'], 'Wounded': ['sum']} df_tmp = df_input.groupby(group_by).agg(agg_on).reset_index() df_tmp.columns = ['Region', 'Country', 'Attack Type', 'Weapon Type', 'Suicide', 'Success', 'Attack', 'Killed', 'Wounded'] dimensions = [dict(values=df_tmp[label], label=label) for label in order] # Build color scale parcats_length = len(df_tmp) color = np.zeros(parcats_length, dtype='uint8') colorscale = [[0, 'gray'], [1, 'firebrick']] # Build figure as FigureWidget fig = go.FigureWidget( data=[go.Scatter(x=df_tmp['Killed'], y=df_tmp['Wounded'], marker={'color': 'gray'}, mode='markers', selected={'marker': {'color': 'firebrick'}}, unselected={'marker': {'opacity': 0.3}}), go.Parcats(domain={'y': [0, 0.4]}, dimensions=dimensions, line={'colorscale': colorscale, 'cmin': 0, 'cmax': 1, 'color': color, 'shape': 'hspline'}, labelfont={'size': 18, 'family': 'Times'}, tickfont={'size': 16, 'family': 'Times'}) ]) fig.update_layout(margin={'l': 40, 'b': 40, 't': 40, 'r': 40}, height=800, xaxis={'title': 'Killed'}, yaxis={'title': 'Wounded', 'domain': [0.6, 1]}, dragmode='lasso', hovermode='closest') return fig, len(df_tmp)
def display_parcat(selectData, df_s, n_clicks): if not df_s: df_s = df else: df_s = pd.read_json(df_s, orient='split') if n_clicks: df_s = df color = np.zeros(len(df_s), dtype='uint8') colorscale = [[0, 'gray'], [1, 'royalblue']] categorical_dimensions = ['Generation', 'isLegendary', 'Color'] dimensions = [ dict(values=df_s[label], label=label) for label in categorical_dimensions ] parcat_plot = go.Parcats(dimensions=dimensions, line={ 'colorscale': colorscale, 'cmin': 0, 'cmax': 1, 'color': color, 'shape': 'hspline' }) # Update parcats colors if selectData: new_color = np.zeros(len(df_s), dtype='uint8') for point in selectData['points']: new_color[point['pointIndex']] = 1 parcat_plot.line.color = new_color return [{'data': [parcat_plot]}]
def visualize_categories( self, categories: list, color_by: str, colorscale: list = None, title: str = "Metadata categories distribution", output_file: str = "", ): """ Visualize category metadata with parallel categories diagram. :param categories: list of column to display :param color_by: perform coloration on the given category """ if colorscale is None: colorscale = self.DEFAULT_COLORSCALE dimensions = self._get_dimensions(categories) color = self._get_color(color_by) fig = go.Figure(data=[ go.Parcats( dimensions=dimensions, line={ "color": color, "colorscale": colorscale }, hoverinfo="count", arrangement="freeform", ) ]) fig.update_layout(title=title) fig.show() if output_file: plotly.io.write_html(fig, output_file)
def _get_figure_widget(self): config = Config() trace = go.Parcats( dimensions=[{ "label": col, "values": self.data_source.data[col] } for col in self.selected_columns], line=dict( color=config.color_scale[1][1], colorscale=config.color_scale, cmin=0, cmax=1, shape="hspline", ), ) figure_widget = go.FigureWidget( data=[trace], layout=go.Layout( margin=dict(l=20, r=20, b=20, t=20, pad=5), autosize=True, showlegend=False, ), ) figure_widget.data[0].on_click(self.on_selection) return trace, figure_widget
def build_bubble_figure(width, height): # Build figure as FigureWidget fig = go.Figure( data=[ go.Scatter( x=md["Freedom to make life choices"], y=md["Generosity"], text=md["City"], customdata=customdata, hovertemplate="""<extra></extra> <em>%{customdata[1]}</em><br> GDP per capita = %{customdata[2]} €""", marker={ "color": "#986EA8", "sizeref": sizeref, "sizemin": 0.005, "sizemode": "area", "size": gdp, }, mode="markers", selected={"marker": {"color": "rgb(243,203,70)"}}, unselected={"marker": {"opacity": 0.3}}, ), go.Parcats( domain={"y": [0, 0.4]}, dimensions=dimensions, line={ "colorscale": colorscale, "cmin": 0, "cmax": 1, "color": color, "shape": "hspline", }, ), ] ) fig.update_layout( font_color="white", font_size=9, xaxis={"title": "Freedom to make life choices"}, yaxis={"title": "Generosity", "domain": [0.6, 1]}, dragmode="lasso", hovermode="closest", paper_bgcolor="rgba(0,0,0,0)", plot_bgcolor="rgba(0,0,0,0)", autosize=False, bargap=0.35, height=int(height * 0.35), width=int(width * 0.45), margin=dict( l=15, # left margin r=15, # right margin b=0, # bottom margin t=0, # top margin ), ) return fig
def update_advanced_figure(selected_year_group): # selects the year group's data filtered_dataset = dataset[dataset['Year Group'] == selected_year_group] # creates the pass or fail column's dimension pass_dim = go.parcats.Dimension(values=filtered_dataset['Pass or Fail'], label='Pass or Fail') # creates the by column's dimension by_dim = go.parcats.Dimension(values=filtered_dataset['By'], label='Placed on Ballot By') # creates the type measure column's dimension type_dim = go.parcats.Dimension(values=filtered_dataset['Type Measure'], label='Ballot Type of Measure') # converts the pass or fail column into a binary list color_list = [ 0 if result == 'Failed' else 1 for result in filtered_dataset['Pass or Fail'] ] # returns the components needed to update the alluvial diagram return { 'data': [ go.Parcats(dimensions=[pass_dim, by_dim, type_dim], line={ 'color': color_list, 'colorscale': colorscale }, hoveron='color', hoverinfo='probability', labelfont={ 'size': 17, 'family': 'Times' }, tickfont={ 'size': 12, 'family': 'Times' }, arrangement='freeform') ], 'layout': dict(title={ 'text': "Advanced Graph - Ballot Measure Alluvial Diagram By Decade", 'y': 0.9, 'x': 0.5, 'xanchor': 'center', 'yanchor': 'top' }, margin={'b': 20}, titlefont={'size': 20}) }
def build_figure(): # Build figure as FigureWidget fig = go.Figure(data=[ go.Scatter( x=md["Food"], y=md["Health"], marker={ "color": "gray", "size": size }, mode="markers", selected={"marker": { "color": "rgb(243,203,70)" }}, unselected={"marker": { "opacity": 0.3 }}, ), go.Parcats( domain={"y": [0, 0.4]}, dimensions=dimensions, line={ "colorscale": colorscale, "cmin": 0, "cmax": 1, "color": color, "shape": "hspline", }, ), ]) fig.update_layout( height=800, xaxis={"title": "Employment"}, yaxis={ "title": "Health", "domain": [0.6, 1] }, dragmode="lasso", hovermode="closest", paper_bgcolor="rgba(0,0,0,0)", plot_bgcolor="rgba(0,0,0,0)", autosize=False, bargap=0.35, ) return fig
def build_graphBA1(colorinput): x_axis = "ProbPercent" y_axis = "PotentialValue" dff = df_num dimensions = [dict(values=dff[label], label=label) for label in ["New","Type"]] # Build colorscale color = np.zeros(len(dff), dtype='uint8') colorscale = [[0, 'gray'], [1, 'firebrick']] for i in colorinput: color[i] = 1 # Build figure as FigureWidget fig = go.FigureWidget( data=[go.Scatter(x=dff[x_axis], y=dff[y_axis], customdata=[i for i in dff.index], marker={'color': 'gray'}, mode='markers', selected={'marker': {'color': 'firebrick'}}, unselected={'marker': {'opacity': 0.3}}), go.Parcats( domain={'y': [0, 0.4]}, dimensions=dimensions, line={'colorscale': colorscale, 'cmin': 0, 'cmax': 1, 'color': color, 'shape': 'hspline'}) ]) fig.data[0].selectedpoints = colorinput fig.update_layout( height=800, xaxis={'title': '{}'.format(x_axis)}, yaxis={'title': '{}'.format(y_axis), 'domain': [0.6, 1]}, dragmode='lasso', hovermode='closest',) #fig.data[1].line.color = color fig.update_layout( margin=dict(l=0, r=15, t=0, b=10), #paper_bgcolor="lightcyan", #plot_bgcolor='lightsteelblue' #gainsboro, lightsteelblue lightsalmon lightgreen lightpink lightcyan lightblue black ) return fig
def ParallelCoordinate(data, cate, feature_list, dict_features): n_features = 31 selected_header = selection_first_n(data, feature_list, cate, n_features) dimensions = [go.parcats.Dimension( values = data[cate], categoryorder = 'array', categoryarray = sorted(data[cate].unique()), label = dict_features[cate])] for feature in selected_header: if data[feature].dtypes == 'object': dimensions.append(go.parcats.Dimension( values = data[feature], categoryorder = 'category ascending', label = dict_features[feature])) else: dimensions.append(go.parcats.Dimension( values = data[feature], categoryorder = 'array', categoryarray = sorted(data[feature].unique()), label = dict_features[feature])) color = data[cate] colorscale = ['rgb(255, 223, 61)','rgb(235, 168, 36)', 'rgb(194, 127, 0)', 'rgb(153, 86, 0)', 'rgb(113, 46, 0)'] fig = go.Figure( data = [go.Parcats(dimensions = dimensions, line = {'color': color, 'colorscale': colorscale, 'showscale': False}, labelfont={'size': 14, 'family': 'Courier New, monospace', 'color':'rgba(245, 240, 214, 1)'}, tickfont={'size': 14, 'family': 'Courier New, monospace', 'color':'rgba(245, 240, 214, 1)'} )] ) fig.update_layout( autosize = False, width = n_features * 220, height = 800, plot_bgcolor='rgba(0, 0, 0, 0)', paper_bgcolor='rgba(0, 0, 0, 0)', ) return fig
def produce_tour_progression_parcat(fn): df = pd.read_excel(fn, sheet_name='RawStr') fig = go.Figure(go.Parcats( dimensions=[ {'label': '2015', 'values': df['2015'].to_list()}, {'label': '2016', 'values': df['2016'].to_list()}, {'label': '2017', 'values': df['2017'].to_list()}, {'label': '2018', 'values': df['2018'].to_list()}, {'label': '2019', 'values': df['2019'].to_list()}, {'label': '2020', 'values': df['2020'].to_list()}] )) return fig
def update_sankey(selected_dimensions, selected_year): filtered_df = df_NAICS_counts[df_NAICS_counts["YEAR"] == selected_year] if selected_year <= 2014: try: selected_dimensions.remove("FOREIGN_WORKER_EDUCATION") except: pass dims=[] for dim in selected_dimensions: dims.append({'label': dim, 'values': filtered_df[dim]}) fig = go.Figure(go.Parcats(dimensions=dims, counts=filtered_df["COUNT"])) fig.update_layout(height=1200) fig.update_layout(transition_duration=500) return fig
def topic_by_daf(chosen_book= "Sukkah", step = 10, num_most_common = 1): my_topics = topics[topics["book"]==chosen_book] book_daf_min = min(my_topics["daf_int"]) book_daf_max = max(my_topics["daf_int"]) l = split_book_to_chunks(book_daf_min, book_daf_max, step, my_topics, num_most_common) source = [] target = [] count = [] for x in l: source.append(x['chunk']) target.append(x['topic']) count.append(x['count']) color_lookup = {} for i,x in enumerate(set(target)): color_lookup[x] = i color_list = [] for x in target: color_list.append(color_lookup[x]) fig = go.Figure( go.Parcats( dimensions=[ {'label': 'Dapim', 'values': source}, {'label': 'Topics', 'values': target}, ], counts=count, line={'color': color_list} ) ) fig.update_layout( title = chosen_book ) st.plotly_chart(fig)
def adjacency_parallel_category_plot(rules: List): ''' Visualizes the antecedents and consequents of each association rules by drawing lines representing each rule across identical vertical axes representing the potential items in the entire dataset Similar to parallel coordinate plot but more readible for small numbers of categorical points ''' unique_entities_by_axis_count = [] rules_by_axis_count = [] for rule in rules: axis_required = len(rule.lhs) + 1 #Filter out rules with multiple consequents #TODO consider allowing multiple consequents if len(rule.rhs) == 1: #If the rules_by_axis_count list lacks a slot for the current number of antacedents, add them while len(rules_by_axis_count) < axis_required - 1: rules_by_axis_count.append([]) unique_entities_by_axis_count.append(set()) #Add the rule and the entities found in its atecedents and consequents to their respective structure rules_by_axis_count[axis_required - 2].append(rule) unique_entities_by_axis_count[axis_required - 2] = unique_entities_by_axis_count[ axis_required - 2].union( set(rule.lhs), set(rule.rhs)) axis_counter = 2 for rules, unique_entities in zip(rules_by_axis_count, unique_entities_by_axis_count): line_color = list(map(lambda rule: round(rule.confidence, 2), rules)) dimensions = _parallel_category_builder(rules, axis_counter) fig = go.Figure(data=go.Parcats(dimensions=dimensions, )) fig.update_layout(plot_bgcolor='white', paper_bgcolor='white') fig.show() axis_counter += 1
def sankey_graph(sankey_df): living_dim = go.parcats.Dimension( values=sankey_df.living_category_var_names, categoryorder="category ascending", label="I am living with... ", ) household_dim = go.parcats.Dimension( values=sankey_df.housework_mapped, label="I take care of ...", categoryarray=[0, 1, 2], ticktext=[ "all or most<br>of the <br>housework", "as much<br>housework<br>as others", "less then others <br>or none of <br>the housework", ], ) color = sankey_df.housework_mapped colorscale = ["#9F8CF3", "#f2f2f2", "#FFAECA"] fig = go.Figure( data=[ go.Parcats( dimensions=[living_dim, household_dim], line={"color": color, "colorscale": colorscale}, hoveron="color", hovertemplate="%{bandcolorcount} Women" + "<extra></extra>", ) ] ) fig.update_layout( paper_bgcolor="rgba(0,0,0,0)", height=500, # height of the graph plot_bgcolor="rgba(0,0,0,0)", margin=dict(l=60, r=60, t=20, b=20), ) return fig
def generate_parcats_2(dataset, group, filter=None, filter_value=None, year=None, title=None): df = dataset.copy() if filter_value != None: df = df[df[filter] == filter_value] if year != None: df = df[df['Año'] == year] dimensions = [] for i in group: dimensions.append({'label': i, 'values': df[i]}) fig = go.Figure( data=[go.Parcats(dimensions=dimensions, counts=df['Total'])], layout=make_title(title)) return fig
def parcat_analysis(TaXon_table_xlsx, path_to_outdirs, template, height, width, meta_data_to_test, plotly_colors, available_taxonomic_levels_list, taxonomic_level, theme, font_size, color_discrete_sequence): import PySimpleGUI as sg import pandas as pd import numpy as np import plotly.graph_objects as go from pathlib import Path import webbrowser, random color1 = theme[0] color2 = theme[1] opacity_value = theme[2] TaXon_table_xlsx = Path(TaXon_table_xlsx) Meta_data_table_xlsx = Path( str(path_to_outdirs) + "/" + "Meta_data_table" + "/" + TaXon_table_xlsx.stem + "_metadata.xlsx") TaXon_table_df = pd.read_excel(TaXon_table_xlsx, header=0).fillna("unidentified") TaXon_table_samples = TaXon_table_df.columns.tolist()[10:] Meta_data_table_df = pd.read_excel(Meta_data_table_xlsx, header=0).fillna("nan") Meta_data_table_samples = Meta_data_table_df['Samples'].tolist() metadata_loc = Meta_data_table_df.columns.tolist().index(meta_data_to_test) ## drop samples with metadata called nan (= empty) drop_samples = [ i[0] for i in Meta_data_table_df.values.tolist() if i[metadata_loc] == "nan" ] if drop_samples != []: ## filter the TaXon table TaXon_table_df = TaXon_table_df.drop(drop_samples, axis=1) TaXon_table_samples = TaXon_table_df.columns.tolist()[10:] ## also remove empty OTUs row_filter_list = [] for row in TaXon_table_df.values.tolist(): reads = set(row[10:]) if reads != {0}: row_filter_list.append(row) columns = TaXon_table_df.columns.tolist() TaXon_table_df = pd.DataFrame(row_filter_list, columns=columns) Meta_data_table_df = pd.DataFrame( [ i for i in Meta_data_table_df.values.tolist() if i[0] not in drop_samples ], columns=Meta_data_table_df.columns.tolist()) Meta_data_table_samples = Meta_data_table_df['Samples'].tolist() metadata_list = Meta_data_table_df[meta_data_to_test].values.tolist() ## create a y axis title text taxon_title = taxonomic_level ## adjust taxonomic level if neccessary if taxonomic_level in ["ASVs", "ESVs", "OTUs", "zOTUs"]: taxon_title = taxonomic_level taxonomic_level = "ID" if len(set(metadata_list)) == 1: sg.PopupError("Please choose more than one meta data category.") else: if sorted(TaXon_table_samples) == sorted(Meta_data_table_samples): ## define variables samples = Meta_data_table_samples OTU_abundances_dict = {} samples_metadata_list = [] ## extract the relevant data TaXon_table_df = TaXon_table_df[[taxonomic_level] + samples] ## define an aggregation function to combine multiple hit of one taxonimic level aggregation_functions = {} ## define samples functions for sample in samples: ## 'sum' will calculate the sum of p/a data aggregation_functions[sample] = 'sum' ## define taxon level function aggregation_functions[taxonomic_level] = 'first' ## create condensed dataframe TaXon_table_df = TaXon_table_df.groupby( TaXon_table_df[taxonomic_level]).aggregate( aggregation_functions) if 'unidentified' in TaXon_table_df.index: TaXon_table_df = TaXon_table_df.drop('unidentified') ## create a list of samples for each category category_dict = {} for sample, category in zip(samples, metadata_list): if category not in category_dict.keys(): category_dict[category] = [sample] else: category_dict[category] = category_dict[category] + [ sample ] ## create a color dict color_dict = {} color_discrete_sequence = color_discrete_sequence * len( TaXon_table_samples) for i, category in enumerate(sorted(set(metadata_list))): color_dict[category] = color_discrete_sequence[i] ## sum up the reads of all samples of a respective category ## store them in a dict sum_dict = {} for key, value in category_dict.items(): sum_dict[key] = TaXon_table_df[value].sum( axis=1).values.tolist() ## collect all available taxa taxa = TaXon_table_df[taxonomic_level].values.tolist() ## store taxa and labels taxon_list = [] label_list = [] color_list = [] ## loop through all categories for category, reads in sum_dict.items(): ## loop through all taxa (in terms of read numbers) for i, entry in enumerate(reads): if entry != 0: taxon_list.append(taxa[i]) label_list.append(category) color_list.append(color_dict[category]) if (taxonomic_level == "Species" or taxonomic_level == "Genus"): taxon_list = ["<i>" + taxon + "</i>" for taxon in taxon_list] ## create the parcats fig = go.Figure() fig = go.Figure( go.Parcats(dimensions=[{ 'label': taxon_title, 'values': taxon_list }, { 'label': meta_data_to_test, 'values': label_list }], line={ 'color': color_list, 'shape': 'hspline' })) fig.update_layout(height=int(height), width=int(width), template=template, yaxis_title=meta_data_to_test, showlegend=False, font_size=font_size, title_font_size=font_size) fig.update_layout(margin=dict(l=200, r=200, t=50, b=50)) # finish script output_pdf = Path( str(path_to_outdirs) + "/" + "ParCat_plots" + "/" + TaXon_table_xlsx.stem + "_" + taxon_title + "_" + meta_data_to_test + "_parcat.pdf") output_html = Path( str(path_to_outdirs) + "/" + "ParCat_plots" + "/" + TaXon_table_xlsx.stem + "_" + taxon_title + "_" + meta_data_to_test + "_parcat.html") fig.write_image(str(output_pdf)) fig.write_html(str(output_html)) ## ask to show plot answer = sg.PopupYesNo('Show plot?', keep_on_top=True) if answer == "Yes": webbrowser.open('file://' + str(output_html)) ## write to log file sg.Popup("Parallel category plots are found in", path_to_outdirs, "/ParCat_plots/", title="Finished", keep_on_top=True) from taxontabletools.create_log import ttt_log ttt_log("parallel category analysis", "analysis", TaXon_table_xlsx.name, output_pdf.name, "", path_to_outdirs) else: sg.PopupError( "Error: The samples between the taxon table and meta table do not match!", keep_on_top=True)
theme_list = [] for colName, colData in theme_set.iloc[:, 1:].iteritems(): sumCol = theme_set[colName].sum() theme_list.extend(repeat(colName, sumCol)) theme_list project_list = [] for colName, colData in theme_set.iloc[:, 1:].iteritems(): counter = 0 for num in colData: if num == 1: project_list.append(theme_set['Project Names'][counter]) counter += 1 project_list fig = go.Figure( go.Parcats(dimensions=[ { 'label': 'Theme', 'values': theme_list }, { 'label': 'Project', 'values': project_list }, ])) fig.show()
def report_plots(hyperparam_df, history_df): ''' ''' # Generate plot for comparing the CNN histories fig1 = make_subplots(subplot_titles=('CNN Loss', 'CNN Accuracy'), horizontal_spacing=0.15, rows=1, cols=2) # Assigned title to the overall figure, the subplots and their axes fig1.update_layout(dict(font=dict(size=12))) fig1.update_layout(title_text="CNN Performance History Comparison", height=600) fig1.update_xaxes(title_text='Epochs', row=1, col=1) fig1.update_xaxes(title_text='Epochs', row=1, col=2) fig1.update_yaxes(title_text='Loss', row=1, col=1) fig1.update_yaxes(title_text='Accuracy', row=1, col=2) # Define the color palette to use color = [ '#a50026', '#d73027', '#f46d43', '#fdae61', '#fee090', '#ffffbf', '#e0f3f8', '#abd9e9', '#74add1', '#4575b4', '#313695', '#8e0152', '#c51b7d', '#de77ae', '#f1b6da', '#fde0ef', '#f7f7f7', '#e6f5d0', ' #b8e186', '#7fbc41', '#4d9221', '#276419' ] # Let's plot the trainig and validation loss and accuracy for i in range(len(history_df)): fig1.add_scatter(x=history_df['epochs'][i], y=history_df['train_loss'][i], mode='lines', legendgroup=history_df['report_name'][i], name=history_df['report_name'][i], marker=dict(size=8, color=color[i], colorscale='Electric'), row=1, col=1) fig1.add_scatter(x=history_df['epochs'][i], y=history_df['train_accuracy'][i], mode='lines', legendgroup=history_df['report_name'][i], name=history_df['report_name'][i], marker=dict(size=8, color=color[i], colorscale='Electric'), row=1, col=2, showlegend=False) fig1.add_scatter(x=history_df['epochs'][i], y=history_df['val_loss'][i], mode='markers', legendgroup=history_df['report_name'][i], name=history_df['report_name'][i], marker=dict(size=8, color=color[i], colorscale='Electric'), row=1, col=1, showlegend=False) fig1.add_scatter(x=history_df['epochs'][i], y=history_df['val_accuracy'][i], mode='markers', legendgroup=history_df['report_name'][i], name=history_df['report_name'][i], marker=dict(size=8, color=color[i], colorscale='Electric'), row=1, col=2, showlegend=False) fig1.update_layout(plot_bgcolor='lightslategray') # Generate Parallel Coordinates plot fig2 = go.Figure(data=go.Parcats( line=dict(color=[ '#a50026', '#d73027', '#f46d43', '#fdae61', '#fee090', '#ffffbf', '#e0f3f8', '#abd9e9', '#74add1', '#4575b4', '#313695', '#8e0152', '#c51b7d', '#de77ae', '#f1b6da', '#fde0ef', '#f7f7f7', '#e6f5d0', ' #b8e186', '#7fbc41', '#4d9221', '#276419' ], colorscale='Electric'), dimensions=list([ dict(label='Report Name', values=hyperparam_df['report_name']), dict(label='Num layers', values=hyperparam_df['layers'], categoryorder='category descending'), dict(label='Kernel_Size', values=hyperparam_df['kernel_size'], categoryorder='category descending'), dict(label='Activation', values=hyperparam_df['activation_function']), dict(label='Optimizer', values=hyperparam_df['optimizer']), dict(label='Pooling', values=hyperparam_df['pooling'].values), dict(label='Accuracy', values=np.round(hyperparam_df['test_accuracy'], 3), categoryorder='category descending'), ]))) fig2.update_layout(dict(font=dict(size=12)), title='Parallel Coordinate Plot Comparison', plot_bgcolor='lightblue', paper_bgcolor='white') return fig1, fig2
import plotly.graph_objects as go import pandas as pd titanic_df = pd.read_csv("titanic.csv") # Create dimensions class_dim = go.parcats.Dimension( values=titanic_df.Pclass, categoryorder='category ascending', label="Class" ) gender_dim = go.parcats.Dimension(values=titanic_df.Sex, label="Gender") survival_dim = go.parcats.Dimension( values=titanic_df.Survived, label="Outcome", categoryarray=[0, 1], ticktext=['perished', 'survived'] ) # Create parcats trace color = titanic_df.Survived; colorscale = [[0, 'lightsteelblue'], [1, 'mediumseagreen']]; fig = go.Figure(data = [go.Parcats(dimensions=[class_dim, gender_dim, survival_dim], line={'color': color, 'colorscale': colorscale}, hoveron='color', hoverinfo='count+probability', labelfont={'size': 18, 'family': 'Times'}, tickfont={'size': 16, 'family': 'Times'}, arrangement='freeform')]) fig.show()
# Build parcats dimensions categorical_dimensions = ['Resolution', 'PdDistrict', 'DayOfWeek']; dimensions = [dict(values=df2[label], label=label) for label in categorical_dimensions] # Build colorscale color = np.zeros(len(df2), dtype='uint8') colorscale = [[0, 'gray'], [1, 'red']] # Build figure as FigureWidget fig = go.FigureWidget( #data=[go.Scatter(x=cars_df.X[1:1000], y=cars_df.Y[1:1000], data=[go.Scatter(x=df2.X, y=df2.Y, marker={'color': 'gray'}, mode='markers', selected={'marker': {'color': 'red'}}, unselected={'marker': {'opacity': 0.3}}), go.Parcats( domain={'y': [0, 0.4]},dimensions=dimensions, line={'colorscale': colorscale, 'cmin': 0, 'cmax': 1, 'color': color, 'shape': 'hspline'}) ]) fig.update_layout( height=800, xaxis={'title': 'longitude'}, yaxis={'title': 'latitude', 'domain': [0.6, 1]}, dragmode='lasso', hovermode='closest') def update_color(trace, points, state): # Update scatter selection fig.data[0].selectedpoints = points.point_inds # Update parcats colors new_color = np.zeros(len(df2), dtype='uint8') new_color[points.point_inds] = 1