AhmedKhaled1122/Data-Analysis-and-Machine-Learning
0
1import numpy as np2import pandas as pd3import plotly.express as px4 5def bar_chart(df, x, y, sort, color=None):6 df_grouped = df.groupby([x, color], as_index=False)[y].sum() if color else df.groupby(x, as_index=False)[y].sum()7 if sort in ["ascending", "descending"]:8 df_grouped = df_grouped.sort_values(by=y, ascending=(sort == "ascending"))9 10 title = f'{y} by {x}' + (f" and {color}" if color else "")11 fig = px.bar(df_grouped, x=x, y=y, color=color, title=title, barmode='group')12 fig.update_layout(xaxis_title=x, yaxis_title=y, template="plotly_white", margin=dict(l=20, r=2, t=21))13 return fig14 15def barh_chart(df, x, y, sort, color=None):16 df_grouped = df.groupby([x, color], as_index=False)[y].sum() if color else df.groupby(x, as_index=False)[y].sum()17 18 if sort in ["ascending", "descending"]:19 df_grouped = df_grouped.sort_values(by=y, ascending=(sort == "ascending"))20 21 title = f'{y} by {x}' + (f" and {color}" if color else "")22 fig = px.bar(df_grouped, x=y, y=x, color=color, title=title, orientation="h", barmode='group')23 fig.update_layout(xaxis_title=y, yaxis_title=x, template="plotly_white", margin=dict(l=20, r=2, t=21))24 return fig25 26def line_chart(df, x, y, color=None):27 group = [x, color] if color else x28 df_grouped = df.groupby(group, as_index=False)[y].sum()29 30 title = f'{y} by {x}' + (f" and {color}" if color else "")31 fig = px.line(df_grouped, x=x, y=y, color=color, title=title, markers=True)32 fig.update_layout(xaxis_title=x, yaxis_title=y, template="plotly_white", margin=dict(l=20, r=2, t=21))33 return fig34 35def scatter_chart(df, x, y, color=None):36 title = f"{y} vs {x}" + (f" by {color}" if color else "")37 fig = px.scatter(df, x=x, y=y, color=color, title=title)38 fig.update_layout(xaxis_title=x, yaxis_title=y, template="plotly_white", margin=dict(l=20, r=2, t=21))39 return fig40 41def violin_chart(df, x, y, color=None):42 title = f'{y} by {x}' + (f" and {color}" if color else "")43 fig = px.violin(df, x=x, y=y, color=color, title=title, box=True, points="all")44 fig.update_layout(xaxis_title=x, yaxis_title=y, template="plotly_white", margin=dict(l=20, r=2, t=21))45 return fig46 47def histogram_chart(df, x, color=None):48 title = f"Distribution of {x}" + (f" by {color}" if color else "")49 fig = px.histogram(df, x=x, color=color, barmode="overlay", title=title)50 fig.update_layout(xaxis_title=x, yaxis_title="Count", template="plotly_white", margin=dict(l=20, r=2, t=21))51 return fig52 53def pie_chart(df, names, values):54 title = f"{values} distribution by {names}"55 fig = px.pie(df, names=names, values=values, title=title)56 fig.update_layout(template="plotly_white", margin=dict(l=20, r=2, t=21))57 return fig58 59def donut_chart(df, names, values):60 title = f"{values} distribution by {names}"61 fig = px.pie(df, names=names, values=values, hole=0.4, title=title)62 fig.update_layout(template="plotly_white", margin=dict(l=20, r=2, t=21))63 return fig64 65def treemap_chart(df, path, values, color=None):66 title = f"{values} treemap by {' - '.join(path)}"67 fig = px.treemap(df, path=path, values=values, color=color, title=title)68 fig.update_layout(template="plotly_white", margin=dict(l=20, r=2, t=21))69 return fig70 71def sunburst_chart(df, path, values, color=None):72 title = f"{values} sunburst by {' - '.join(path)}"73 fig = px.sunburst(df, path=path, values=values, color=color, title=title)74 fig.update_traces(textinfo="label+percent entry")75 fig.update_layout(template="plotly_white", margin=dict(l=20, r=2, t=21))76 return fig77 