rosacastillo's picture
Fixing FacetGrid figure
5e99630
raw
history blame
2.38 kB
import pandas as pd
import gradio as gr
import matplotlib.pyplot as plt
import seaborn as sns
from seaborn import FacetGrid
def plot_daily_dist_invalid_trades(invalid_trades: pd.DataFrame):
"""Function to paint the distribution of daily invalid trades, no matter which market"""
plot = sns.histplot(data=invalid_trades, x="creation_date", kde=True)
plt.xticks(rotation=45, ha="right")
plt.xlabel("Creation date")
plt.ylabel("Daily number of invalid trades")
plt.title("Distribution of daily invalid trades over time")
return gr.Plot(value=plot.get_figure())
def plot_daily_nr_invalid_markets(invalid_trades: pd.DataFrame):
"""Function to paint the number of invalid markets over time"""
daily_invalid_markets = (
invalid_trades.groupby("creation_date")
.agg(trades_count=("title", "count"), nr_markets=("title", "nunique"))
.reset_index()
)
sns.set_theme(palette="viridis")
plot = sns.lineplot(data=daily_invalid_markets, x="creation_date", y="nr_markets")
plt.xticks(rotation=45, ha="right")
plt.xlabel("Creation date")
plt.ylabel("Daily number of invalid markets")
plt.title("Evolution of daily invalid markets over time")
return gr.Plot(value=plot.get_figure())
def plot_ratio_invalid_trades_per_market(invalid_trades: pd.DataFrame):
"""Function to paint the number of invalid trades that the same market accummulates"""
cat = invalid_trades["title"]
codes, uniques = pd.factorize(cat)
# add the IDs as a new column to the original dataframe
invalid_trades["title_id"] = codes
plot: FacetGrid = sns.displot(invalid_trades, x="title_id")
plt.xlabel("market id")
plt.ylabel("Total number of invalid trades by market")
plt.title("Distribution of invalid trades per market")
return gr.Plot(value=plot.figure)
def plot_top_invalid_markets(invalid_trades: pd.DataFrame):
"""Function to paint the top markets with the highest number of invalid trades"""
top_invalid_markets = invalid_trades.title.value_counts().reset_index()
top_invalid_markets.rename(columns={"count": "nr_invalid_trades"}, inplace=True)
plt.figure(figsize=(25, 10))
plot = sns.barplot(
top_invalid_markets,
x="nr_invalid_trades",
y="title",
hue="title",
dodge=False,
)
return gr.Plot(value=plot.get_figure())