I am attempting to create a function I can reuse to loop through a list of categorical columns for any given pandas data frame and generate a series of plots to visually get the value_counts()
for each column.
I have written a function for this
def get_count_plot(cols_list):
for col in cols_list:
fig = sns.countplot(data=df[col], x=df[col].value_counts())
return fig
This function only returns one plot from a list of several columns. I get no errors on completion of the function.
I have tried adding enumerate()
to this function with no luck.
I have used enumerate
to do this but with a list of dataframes instead of a list of column names.
def generate_box_plot(list_of_dfs):
for idx, df in enumerate(list_of_dfs):
fig = px.box(df, x='Year', y='Transportation_Cnt', color='Month')
fig.update_xaxes(tickmode='linear')
fig.show()
This script worked just fine, however I am not working with a list of pandas
dataframes.
Here is the amended get_count_plot()
import seaborn as sns
import matplotlib.pyplot as plt
def get_count_plots(df, cols_list):
for col in cols_list:
plt.figure(figsize=(8, 6))
sns.countplot(data=df, x=col)
plt.title(f'Value Counts of {col}')
plt.xticks(rotation=45)
plt.show()
And this the amended get_count_plot()
import plotly.express as px
def generate_box_plot(dataframe, x_col, y_col, color_col):
fig = px.box(dataframe, x=x_col, y=y_col, color=color_col)
fig.update_xaxes(tickmode='linear')
fig.show()
I hope you know to use them and Hope They work for you as expected.