CoolFace
Apppublic

TwinklData/Community_Collections_App

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
px_charts.py85 linesDownload Raw Back to src
1# ========= CONFIGURATION ==========2import pandas as pd3import plotly.express as px4 5 6title_font_size=207title_font_color='#808393'8xaxis_title_font_size=169yaxis_title_font_size=1610 11 12# ======== FUNCTIONS ========13 14def plot_histogram(df: pd.DataFrame, col_to_plot: str, bins: int, height: int = 500, title:str = None):15 16    plt = px.histogram(17            df,18            x=col_to_plot,19            nbins=bins,20            title=title,21            color_discrete_sequence=['#646DEF']22            )23 24    plt.update_layout(25            bargap=0.1,26            height=height,27            title_font_size=title_font_size,28            title_font_color=title_font_color,29            xaxis_title_font_size=xaxis_title_font_size,30            yaxis_title_font_size=yaxis_title_font_size,31 32            )33 34    return plt35 36 37# =========== TOPIC DISTRIBUTION CHART  ===========38 39 40def plot_topic_countplot(topics_df: pd.DataFrame, topic_id_col: str, topic_name_col: str, representation_col: str, height: int = 500, title:str = None):41    """42    This functions plots a count chart for Bertopic topics,43    extracting the 5 words of each topic's representation44    in order to provide more context45    """46 47    ## ----- Extract top 5 words ----48    topics_df['top_5_words'] = topics_df[representation_col].apply(lambda x: ", ".join(x[:5]) if isinstance(x, list) else x)49 50    plt = px.bar(51            topics_df,52            x=topic_id_col,53            y='Count',54            custom_data=["top_5_words", topic_name_col],55            title=title,56            )57 58    plt.update_xaxes(type='category')59 60    plt.update_traces(61        marker_color='#EF64B3',62        textposition='outside',63        hovertemplate=(64            '<b>Topic Name</b>: %{customdata[1]}<br>'65            '<b>Frequency:</b> %{y}<br>'66            '<b>Top 5 words:</b> %{customdata[0]}<extra></extra>'67            )68        )69 70    plt.update_layout(71        height=height,72        hoverlabel=dict(73            font_size=13,74            align="left"75        ),76        title_font_size=title_font_size,77        title_font_color=title_font_color,78        xaxis_title_font_size=xaxis_title_font_size,79        yaxis_title_font_size=yaxis_title_font_size,80    )81 82 83 84    return plt85