def _plotly_viz_plot_time_series(df, col, result=None, decompose=False, title="Time Series"): """Create timeseries visualization. Args: df: The dataframe col (str or [str]): Column of interest. Column datatype must be numerical. result: The statsmodels.tsa.seasonal.DecomposeResult object. Defaults to None. decompose: Set as True to decompose the timeseries with moving average. result must not be None. Defaults to False. title: Title of the plot. Defaults to "Time Series". Returns: The visualization """ if isinstance(col, list): data = [go.Scatter(x=df.index, y=df[c], name=c) for c in col] ylabel = "Variable" if len(col) > 1 else col[0] fig = go.Figure(data=data, layout=figure_layout(title=title, ylabel=ylabel)) elif isinstance(col, str) and not decompose: fig = go.Figure( data=go.Scatter(x=df.index, y=df[col], name=col), layout=figure_layout(title=title, ylabel=col), ) elif decompose: fig = _plotly_viz_decomposition(result, dates=df.index) if _in_notebook(): init_notebook_mode(connected=True) return iplot(fig, config={"displayModeBar": False}) else: return fig
def _pyldavis_viz_visualize_topic_summary( model: "gensim.models.ldamodel.LdaModel", # type: ignore corpus: List[List[Tuple[int, int]]], dictionary: "gensim.corpora.dictionary.Dictionary", # type: ignore ): """Displays interactive pyLDAvis visual to understand topic model and documents. Args: model: LDA topic model corpus: Bag of Words (BoW) representation of documents (token_id, token_count) dictionary: Gensim Dictionary encapsulates the mapping between normalized words and their integer ids. Raises: EnvironmentError: Must be in a Notebook. Returns: A visual to understand topic model and/or documents relating to model """ if not _in_notebook(): raise EnvironmentError("Not in Jupyter Notebook") _compat["pyLDAvis"].enable_notebook() # type: ignore with warnings.catch_warnings(): warnings.filterwarnings( "ignore", category=FutureWarning, module="pyLDAvis", message="Sorting because non-concatenation axis is not aligned.", ) vis = _compat["pyLDAvis"].gensim.prepare(model, corpus, dictionary) # type: ignore return vis
def _plotly_viz_correlation_matrix(association_matrix): """Plot the heatmap for the association matrix. Args: association_matrix (DataFrame): The association matrix Returns: The plotly figure """ # Plot lower left triangle x_ind, y_ind = np.triu_indices(association_matrix.shape[0]) corr = association_matrix.to_numpy() for x, y in zip(x_ind, y_ind): corr[x, y] = None # Set up the color scale cscale = mpl_to_plotly_cmap(get_p_RdBl_cmap()) # Generate a custom diverging colormap fig = go.Figure( data=[ go.Heatmap( z=np.flip(corr, axis=0), x=association_matrix.columns.values, y=association_matrix.columns.values[::-1], connectgaps=False, xgap=2, ygap=2, zmid=0, colorscale=cscale, colorbar={"title": "Strength"}, ) ], layout=go.Layout( autosize=False, width=get_option("display.plotly.fig_width"), height=get_option("display.plotly.fig_height"), title={ "text": "Correlation Matrix", "font": { "size": get_option("display.plotly.title_size") }, }, xaxis=go.layout.XAxis(automargin=True, tickangle=270, ticks="", showgrid=False), yaxis=go.layout.YAxis(automargin=True, ticks="", showgrid=False), plot_bgcolor="rgb(0,0,0,0)", paper_bgcolor="rgb(0,0,0,0)", ), ) if _in_notebook(): po.init_notebook_mode(connected=True) return po.iplot(fig, config={"displayModeBar": False}) else: return fig
def _plotly_viz_data_heatmap(data, colnames: List[str], missing: bool = False, **kwargs): """Plots the data heatmap. Args: data: The dataframe colnames (List[str]): The column names, used for tick labels missing (bool): If True, plots missing values instead **kwargs: Keyword arguments. Returns: The data heatmap as a Plotly figure. """ data_fig = go.Heatmap( z=np.flip(data.values, axis=0), x=list(range(data.shape[0])), y=list(colnames[::-1]), ygap=1, zmin=-3 if not missing else 0, zmax=3 if not missing else 1, colorscale="viridis" if not missing else "greys", colorbar={"title": "z-score (bounded)" if not missing else "Missing"}, ) figure = go.Figure( data=[data_fig], layout=go.Layout( autosize=False, title={ "text": "Data Heatmap", "font": { "size": get_option("display.plotly.title_size") }, }, width=get_option("display.plotly.fig_width"), height=get_option("display.plotly.fig_height"), xaxis=go.layout.XAxis(ticks="", title="Record #", showgrid=False), yaxis=go.layout.YAxis(ticks="", title="Variable", automargin=True, showgrid=False), plot_bgcolor="rgb(0,0,0,0)", paper_bgcolor="rgb(0,0,0,0)", ), ) if _in_notebook(): init_notebook_mode(connected=True) return iplot(figure, config={"displayModeBar": False}) else: return figure
def _plotly_viz_cluster( data, method: str, xlabel: Optional[str] = None, ylabel: Optional[str] = None, **kwargs, ): """Visualize clusters using Plotly. Args: data (DataFrame): The data method (str): The clustering method, to be used as the plot title xlabel (str, optional): The x-axis label. Defaults to "Reduced Dimension 1". ylabel (str, optional): The y-axis label. Defaults to "Reduced Dimension 2". **kwargs: Keyword arguments. Returns: Plotly plot """ xlabel = xlabel or "Reduced Dimension 1" ylabel = ylabel or "Reduced Dimension 2" labels = data["clusters"].unique() trace_list = [] for i in labels: if int(i) < 0: trace = go.Scatter( x=data.loc[data["clusters"] == i, "x"], y=data.loc[data["clusters"] == i, "y"], name="Noise", mode="markers", marker=dict(size=10, color="grey", line=None, opacity=0.7), ) trace_list.append(trace) else: trace = go.Scatter( x=data.loc[data["clusters"] == i, "x"], y=data.loc[data["clusters"] == i, "y"], name=f"Cluster #{i}", mode="markers", marker=dict(size=10, colorscale="earth", line=None, opacity=0.7), ) trace_list.append(trace) layout = dict( yaxis=dict(zeroline=False, title=data.columns[0]), xaxis=dict(zeroline=False, title=data.columns[1]), yaxis_title=ylabel, xaxis_title=xlabel, autosize=False, width=int(get_option("display.plotly.fig_width")), height=int(get_option("display.plotly.fig_height")), title={ "text": "{} Cluster".format(method), "font": { "size": get_option("display.plotly.title_size") }, }, ) fig = go.Figure(dict(data=trace_list, layout=layout)) if _in_notebook(): po.init_notebook_mode(connected=True) return po.iplot(fig) else: return fig