diff --git a/main.py b/main.py index 0e039701..d463fa22 100644 --- a/main.py +++ b/main.py @@ -397,8 +397,8 @@ def draw_a_plot(): #else: with st.beta_container(): #for lib in libs: - #show_plot(kind=kind,chart_type=chart_type,df=df) - show_plot(category,chart_x,chart_y,chart_z,kind=kind,chart_type=chart_type,df=df) + show_plot(kind=kind,chart_type=chart_type,df=df) + # notes @@ -471,25 +471,25 @@ def header(text): def load_data(dataset): return sns.load_dataset(dataset) -def show_plot(category,chart_x,chart_y,chart_z,kind: str,chart_type,df): +def show_plot(kind: str,chart_type,df): st.write(kind) if kind == "Matplotlib": - plot = matplotlib_plot(chart_type, df,category,chart_x,chart_y,chart_z) + plot = matplotlib_plot(chart_type, df) st.pyplot(plot) elif kind == "Seaborn": - plot = sns_plot(chart_type, df,category,chart_x,chart_y,chart_z) + plot = sns_plot(chart_type, df) st.pyplot(plot) elif kind == "Plotly Express": - plot = plotly_plot(chart_type, df,category,chart_x,chart_y,chart_z) + plot = plotly_plot(chart_type, df) st.plotly_chart(plot, use_container_width=True) elif kind == "Altair": - plot = altair_plot(chart_type, df,category,chart_x,chart_y,chart_z) + plot = altair_plot(chart_type, df) st.altair_chart(plot, use_container_width=True) elif kind == "Pandas Matplotlib": - plot = pd_plot(chart_type, df,category,chart_x,chart_y,chart_z) + plot = pd_plot(chart_type, df) st.pyplot(plot) elif kind == "Bokeh": - plot = bokeh_plot(chart_type, df,category,chart_x,chart_y,chart_z) + plot = bokeh_plot(chart_type, df) st.bokeh_chart(plot, use_container_width=True) if __name__ == "__main__": diff --git a/make_plots.py b/make_plots.py index c2772d67..81860471 100644 --- a/make_plots.py +++ b/make_plots.py @@ -7,84 +7,78 @@ import altair as alt from bokeh.plotting import figure -def matplotlib_plot(chart_type: str, df,category,chart_x,chart_y,chart_z): + +def matplotlib_plot(chart_type: str, df): """ return matplotlib plots """ - Xlable = chart_x - Ylable = chart_y - Zlable = chart_z fig, ax = plt.subplots() if chart_type == "Scatter": with st.echo(): - df["color"] = df[category].replace( + df["color"] = df["species"].replace( {"Adelie": 1, "Chinstrap": 2, "Gentoo": 3} ) - ax.scatter(x=df[Xlable], y=df[Ylable], c=df["color"]) + ax.scatter(x=df["bill_depth_mm"], y=df["bill_length_mm"], c=df["color"]) plt.title("Bill Depth by Bill Length") - plt.xlabel(Xlable) - plt.ylabel(Ylable) + plt.xlabel("Bill Depth (mm)") + plt.ylabel("Bill Length (mm)") elif chart_type == "Histogram": with st.echo(): plt.title("Count of Bill Depth Observations") - ax.hist(df[Xlable]) - plt.xlabel(Xlable) + ax.hist(df["bill_depth_mm"]) + plt.xlabel("Bill Depth (mm)") plt.ylabel("Count") elif chart_type == "Bar": with st.echo(): - df_plt = df.groupby(category, dropna=False).mean().reset_index() - ax.bar(x=df_plt[Xlable], height=df_plt[Ylable]) + df_plt = df.groupby("species", dropna=False).mean().reset_index() + ax.bar(x=df_plt["species"], height=df_plt["bill_depth_mm"]) plt.title("Mean Bill Depth by Species") - plt.xlabel(Xlable) - plt.ylabel(Ylable) + plt.xlabel("Species") + plt.ylabel("Mean Bill Depth (mm)") elif chart_type == "Line": with st.echo(): - ax.plot(df.index, df[Ylable]) + ax.plot(df.index, df["bill_length_mm"]) plt.title("Bill Length Over Time") - plt.ylabel(Ylable) + plt.ylabel("Bill Length (mm)") elif chart_type == "3D Scatter": ax = fig.add_subplot(projection="3d") with st.echo(): - df["color"] = df[category].replace( + df["color"] = df["species"].replace( {"Adelie": 1, "Chinstrap": 2, "Gentoo": 3} ) ax.scatter3D( - xs=df[Xlable], - ys=df[Ylable], - zs=df[Zlable], + xs=df["bill_depth_mm"], + ys=df["bill_length_mm"], + zs=df["body_mass_g"], c=df["color"], ) - ax.set_xlabel(Xlable) - ax.set_ylabel(Ylable) - ax.set_zlabel(Zlable) + ax.set_xlabel("bill_depth_mm") + ax.set_ylabel("bill_length_mm") + ax.set_zlabel("body_mass_g") plt.title("3D Scatterplot") return fig -def sns_plot(chart_type: str, df, category,chart_x,chart_y,chart_z): +def sns_plot(chart_type: str, df): """ return seaborn plots """ - Xlable = chart_x - Ylable = chart_y - Zlable = chart_z - fig, ax = plt.subplots() if chart_type == "Scatter": with st.echo(): sns.scatterplot( data=df, - x=Xlable, - y=Ylable, - hue=category, + x="bill_depth_mm", + y="bill_length_mm", + hue="species", ) plt.title("Bill Depth by Bill Length") elif chart_type == "Histogram": with st.echo(): - sns.histplot(data=df, x=Xlable) + sns.histplot(data=df, x="bill_depth_mm") plt.title("Count of Bill Depth Observations") elif chart_type == "Bar": with st.echo(): - sns.barplot(data=df, x=Xlable, y=Ylable) + sns.barplot(data=df, x="species", y="bill_depth_mm") plt.title("Mean Bill Depth by Species") elif chart_type == "Boxplot": with st.echo(): @@ -92,7 +86,7 @@ def sns_plot(chart_type: str, df, category,chart_x,chart_y,chart_z): plt.title("Bill Depth Observations") elif chart_type == "Line": with st.echo(): - sns.lineplot(data=df, x=df.index, y=Ylable) + sns.lineplot(data=df, x=df.index, y="bill_length_mm") plt.title("Bill Length Over Time") elif chart_type == "3D Scatter": st.write("Seaborn doesn't do 3D ☹️. Here's 2D.") @@ -101,69 +95,63 @@ def sns_plot(chart_type: str, df, category,chart_x,chart_y,chart_z): return fig -def plotly_plot(chart_type: str, df, category,chart_x,chart_y,chart_z): +def plotly_plot(chart_type: str, df): """ return plotly plots """ - - Xlable = chart_x - Ylable = chart_y - Zlable = chart_z + if chart_type == "Scatter": with st.echo(): fig = px.scatter( data_frame=df, - x= Xlable, - y= Ylable, - color= category, + x="bill_depth_mm", + y="bill_length_mm", + color="species", title="Bill Depth by Bill Length", ) elif chart_type == "Histogram": with st.echo(): fig = px.histogram( data_frame=df, - x= Xlable, + x="bill_depth_mm", title="Count of Bill Depth Observations", ) elif chart_type == "Bar": with st.echo(): fig = px.histogram( data_frame=df, - x= Xlable, - y= Ylable, + x="species", + y="bill_depth_mm", title="Mean Bill Depth by Species", histfunc="avg", ) # by default shows stacked bar chart (sum) with individual hover values elif chart_type == "Boxplot": with st.echo(): - fig = px.box(data_frame=df, x= Xlable, y=Ylable) + fig = px.box(data_frame=df, x="species", y="bill_depth_mm") elif chart_type == "Line": with st.echo(): fig = px.line( data_frame=df, x=df.index, - y= Ylable, + y="bill_length_mm", title="Bill Length Over Time", ) elif chart_type == "3D Scatter": with st.echo(): fig = px.scatter_3d( data_frame=df, - x= Xlable, - y= Ylable, - z= Zlable, - color= category, + x="bill_depth_mm", + y="bill_length_mm", + z="body_mass_g", + color="species", title="Interactive 3D Scatterplot!", ) return fig -def altair_plot(chart_type: str, df, category,chart_x,chart_y,chart_z): +def altair_plot(chart_type: str, df): """ return altair plots """ - Xlable = chart_x - Ylable = chart_y - Zlable = chart_z if chart_type == "Scatter": with st.echo(): fig = ( @@ -172,7 +160,7 @@ def altair_plot(chart_type: str, df, category,chart_x,chart_y,chart_z): title="Bill Depth by Bill Length", ) .mark_point() - .encode(x= Xlable, y= Ylable, color= category) + .encode(x="bill_depth_mm", y="bill_length_mm", color="species") .interactive() ) elif chart_type == "Histogram": @@ -180,31 +168,31 @@ def altair_plot(chart_type: str, df, category,chart_x,chart_y,chart_z): fig = ( alt.Chart(df, title="Count of Bill Depth Observations") .mark_bar() - .encode(alt.X(Xlable, bin=True), y="count()") + .encode(alt.X("bill_depth_mm", bin=True), y="count()") .interactive() ) elif chart_type == "Bar": with st.echo(): fig = ( alt.Chart( - df.groupby(category, dropna=False).mean().reset_index(), + df.groupby("species", dropna=False).mean().reset_index(), title="Mean Bill Depth by Species", ) .mark_bar() - .encode(x= Xlable, y= Ylable) + .encode(x="species", y="bill_depth_mm") .interactive() ) elif chart_type == "Boxplot": with st.echo(): fig = ( - alt.Chart(df).mark_boxplot().encode(x=Xlable, y=Ylable) + alt.Chart(df).mark_boxplot().encode(x="species:O", y="bill_depth_mm:Q") ) elif chart_type == "Line": with st.echo(): fig = ( alt.Chart(df.reset_index(), title="Bill Length Over Time") .mark_line() - .encode(x="index:T", y=Ylable) + .encode(x="index:T", y="bill_length_mm:Q") .interactive() ) elif chart_type == "3D Scatter": @@ -218,53 +206,50 @@ def altair_plot(chart_type: str, df, category,chart_x,chart_y,chart_z): return fig -def pd_plot(chart_type: str, df, category,chart_x,chart_y,chart_z): +def pd_plot(chart_type: str, df): """ return pd matplotlib plots """ - Xlable = chart_x - Ylable = chart_y - Zlable = chart_z fig, ax = plt.subplots() if chart_type == "Scatter": with st.echo(): - df["color"] = df[category].replace( + df["color"] = df["species"].replace( {"Adelie": "blue", "Chinstrap": "orange", "Gentoo": "green"} ) ax_save = df.plot( kind="scatter", - x= Xlable, - y= Ylable, - c= "color", + x="bill_depth_mm", + y="bill_length_mm", + c="color", ax=ax, title="Bill Depth by Bill Length", ) elif chart_type == "Histogram": with st.echo(): - ax_save = df[Xlable].plot( + ax_save = df["bill_depth_mm"].plot( kind="hist", ax=ax, title="Count of Bill Depth Observations" ) - plt.xlabel(Xlable) + plt.xlabel("Bill Depth (mm)") elif chart_type == "Bar": with st.echo(): ax_save = ( - df.groupby(category, dropna=False) + df.groupby("species", dropna=False) .mean() .plot( kind="bar", - y=Ylable, + y="bill_depth_mm", title="Mean Bill Depth by Species", ax=ax, ) ) - plt.ylabel(Ylable) + plt.ylabel("Bill Depth (mm)") elif chart_type == "Boxplot": with st.echo(): ax_save = df.plot(kind="box", ax=ax) elif chart_type == "Line": with st.echo(): - ax_save = df.plot(kind="line", use_index=True, y=Ylable, ax=ax) + ax_save = df.plot(kind="line", use_index=True, y="bill_length_mm", ax=ax) plt.title("Bill Length Over Time") - plt.ylabel(Ylable) + plt.ylabel("Bill Length (mm)") elif chart_type == "3D Scatter": st.write("Pandas doesn't do 3D ☹️. Here's 2D.") ax_save = df.plot(kind="scatter", x="bill_depth_mm", y="bill_length_mm", ax=ax) @@ -272,22 +257,19 @@ def pd_plot(chart_type: str, df, category,chart_x,chart_y,chart_z): return fig -def bokeh_plot(chart_type: str, df, category,chart_x,chart_y,chart_z): +def bokeh_plot(chart_type: str, df): """ return bokeh plots """ - Xlable = chart_x - Ylable = chart_y - Zlable = chart_z if chart_type == "Scatter": with st.echo(): - df["color"] = df[category].replace( + df["color"] = df["species"].replace( {"Adelie": "blue", "Chinstrap": "orange", "Gentoo": "green"} ) fig = figure(title="Bill Depth by Bill Length") - fig.circle(source=df, x=Xlable, y=Ylable, color="color") + fig.circle(source=df, x="bill_depth_mm", y="bill_length_mm", color="color") elif chart_type == "Histogram": with st.echo(): - hist, edges = np.histogram(df[Xlable].dropna(), bins=10) + hist, edges = np.histogram(df["bill_depth_mm"].dropna(), bins=10) fig = figure(title="Count of Bill Depth Observations") fig.quad( top=hist, bottom=0, left=edges[:-1], right=edges[1:], line_color="white" @@ -301,16 +283,16 @@ def bokeh_plot(chart_type: str, df, category,chart_x,chart_y,chart_z): ) fig.vbar( - source=df.groupby(category, dropna=False).mean(), - x=Xlable, - top=Ylable, + source=df.groupby("species", dropna=False).mean(), + x="species", + top="bill_depth_mm", width=0.8, ) elif chart_type == "Line": with st.echo(): fig = figure(title="Bill Length Over Time", x_axis_type="datetime") - fig.line(source=df.reset_index(), x="index", y=Ylable) + fig.line(source=df.reset_index(), x="index", y="bill_length_mm") elif chart_type == "3D Scatter": st.write("Bokeh doesn't do 3D ☹️. Here's 2D.")