Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save maria-aguilera/db3c3eefa4c8f524dfe3506007a19f3f to your computer and use it in GitHub Desktop.
Save maria-aguilera/db3c3eefa4c8f524dfe3506007a19f3f to your computer and use it in GitHub Desktop.
Functions Used
# Function to print digits on top of Barplot
def show_values(axs, orient="v", space=.01):
def _single(ax):
if orient == "v":
for p in ax.patches:
_x = p.get_x() + p.get_width() / 2
_y = p.get_y() + p.get_height() + (p.get_height()*0.01)
value = '{:.2f}'.format(p.get_height())
ax.text(_x, _y, value, ha="center")
elif orient == "h":
for p in ax.patches:
_x = p.get_x() + p.get_width() + float(space)
_y = p.get_y() + p.get_height() - (p.get_height()*0.5)
value = '{:.2f}'.format(p.get_width())
ax.text(_x, _y, value, ha="left")
if isinstance(axs, np.ndarray):
for idx, ax in np.ndenumerate(axs):
_single(ax)
else:
_single(axs)
# Count Plot Function
def count_plot_function_categorical(df, feature_1, feature_2, title):
# Set plot style
sns.set_palette("deep")
sns.set_style("darkgrid")
fig, ax = plt.subplots(figsize=(24,12))
# Plot
sns.countplot(
x = feature_1,
hue = feature_2,
data = df,
ax = ax
).set_title(title)
show_values(ax)
# Bar plot function
def bar_plot_function_categorical(df, feature_1, feature_2, title, feature_3 = None):
# Set plot style
sns.set_palette("deep")
sns.set_style("darkgrid")
fig, ax = plt.subplots(figsize=(24,12))
# Plot
sns.barplot(
x = feature_1,
y= feature_2,
hue = feature_3,
data = df,
ci = None,
ax = ax
).set_title(title)
show_values(ax)
def dist_plot(df, feature_1, title):
# Set plot style
sns.set(font_scale = 1.5)
sns.set_palette("deep")
sns.set_style("darkgrid")
fig, ax = plt.subplots(figsize=(18,12))
f = sns.distplot(
df[feature_1],
bins=40,
ax=ax).set_title(title)
def dist_plot_survive(df, feature_1, title):
# Set plot style
sns.set(font_scale = 1.5)
sns.set_palette("deep")
sns.set_style("darkgrid")
fig, ax = plt.subplots(figsize=(18,12))
ax.set_title(title)
g = sns.kdeplot(
x=df["Age"],
shade= True,
hue=df["Survived"],
ax=ax,
).set_xlabel('Age')
def swarm_plot(df, feature_1, feature_2, title):
# Set plot style
sns.set(font_scale = 1.5)
sns.set_palette("deep")
sns.set_style("darkgrid")
fig, ax = plt.subplots(figsize=(18,12))
sns.swarmplot(
x=feature_1,
y=feature_2,
hue='Survived',
data=df
).set_title(title)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment