import pandas as pd import gradio as gr import matplotlib.pyplot as plt import seaborn as sns from seaborn import FacetGrid import plotly.express as px def get_top_best_behaviour_markets(markets_data: pd.DataFrame): """Function to paint the top markets with the lowest metric of distribution gap""" sorted_data = markets_data.sort_values(by="dist_gap_perc", ascending=False) top_best_markets = sorted_data[["title", "sample_datetime", "dist_gap_perc"]].head( 5 ) return gr.DataFrame(top_best_markets) def get_distribution_plot(markets_data: pd.DataFrame): """Function to paint the density plot of the metric distribution gap percentage""" # A kernel density estimate (KDE) plot is a method for visualizing the distribution of # observations in a dataset, analogous to a histogram. KDE represents the data using a # continuous probability density curve in one or more dimensions. sns.set_theme(palette="viridis") plt.figure(figsize=(10, 5)) plot = sns.kdeplot(markets_data, x="dist_gap_perc", fill=True) # TODO Add title and labels # Display the plot using gr.Plot return gr.Plot(value=plot.get_figure()) def get_kde_with_trades(markets_data: pd.DataFrame): """Function to paint the density plot of the metric in terms of the number of trades""" plot = sns.kdeplot(markets_data, x="dist_gap_perc", y="total_trades", fill=True) plt.ylabel("Total number of trades per market") return gr.Plot(value=plot.get_figure()) def get_regplot_with_mean_trade_size(markets_data: pd.DataFrame): """Function to Plot data and a linear regression model fit between the metric and the mean trade size""" regplot = sns.regplot(markets_data, x="dist_gap_perc", y="mean_trade_size") plt.ylabel("Mean trade size in USD") return gr.Plot(value=regplot.get_figure()) def get_correlation_map(markets_data: pd.DataFrame): """Function to paint the correlation between different variables""" columns_of_interest = [ "total_trades", "dist_gap_perc", "liquidityMeasure", "mean_trade_size", ] data = markets_data[columns_of_interest] # Compute the correlation matrix correlation_matrix = data.corr() # Create the heatmap heatmap = sns.heatmap( correlation_matrix, annot=True, # Show the correlation values cmap="coolwarm", # Color scheme vmin=-1, vmax=1, # Set the range of values center=0, # Center the colormap at 0 square=True, # Make each cell square-shaped linewidths=0.5, # Add lines between cells cbar_kws={"shrink": 0.8}, ) # Adjust the size of the colorbar # Set the title plt.title("Correlation Heatmap") # Rotate the y-axis labels for better readability plt.yticks(rotation=0) # Show the plot plt.tight_layout() return gr.Plot(value=heatmap.get_figure())