Skip to content

Instantly share code, notes, and snippets.

@randymi01
Last active May 14, 2024 04:15
Show Gist options
  • Save randymi01/79964de5d977978b8c72b18bdb1f639d to your computer and use it in GitHub Desktop.
Save randymi01/79964de5d977978b8c72b18bdb1f639d to your computer and use it in GitHub Desktop.
imports and subplot stuff that I always forget
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
import seaborn as sns
sns.set_palette(sns.color_palette("Spectral"))
sns.set_style("whitegrid")
from matplotlib.ticker import StrMethodFormatter
# ax.xaxis.set_major_formatter(StrMethodFormatter("{x:.2f}"))
import matplotlib as mpl
mpl.rcParams['figure.dpi'] = 200
mpl.rcParams["axes.labelsize"] = 15
pd.set_option('display.max_columns', 999)
pd.set_option('display.max_rows', 100)
# How subplots work. Fig contains subplots
fig = plt.figure()
# add_subplot(#nrows, #ncols, index [top left down]) -> can use three integer representation
ax = fig.add_subplot(111)
# or can do oneline
fig, (ax1, ax2, ax3) = plt.subplots(1, 3)
ax.pie(enviornment_counts.values,labels = enviornment_counts.index, autopct='%1.1f%%',textprops={'fontsize': 8})
ax.set_title("School-Age BVI Student Instructional Enviornment")
fig.savefig("bvi_enviornment.png",bbox_inches='tight')
# pandas plot to ax
df.plot(... ax = ax)
# quickplot pandas series value counts
df["Product Type"].value_counts().sort_values().plot.barh()
# virtual env
python3 -m venv env
source env/bin/activate #linux
env\Scripts\activate.bat #windwos
(add env folder to gitignore)
# string formatting decimal places. %f indicates float, .2 indicates two decimal places
"%.2f"% (2.225) -> 2.23
"{:.2f}".format(2.225) -> 2.23
# Add Image in jupyter markdown cell (same as r-markdown)
![alternative text](path-to-image)
# instead of using apply, use vectorize
vectorized_func = np.vectorize(func_to_be_applied)
output_col = vectorized_func(df["col1"],...)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment