Commonly used operations and notes for scientific computing and data visualization in Python.
https://numpy.org/doc/stable/user/quickstart.html
Quickstart Jupyter notebook:
https://github.com/carbonatezero/np_plt_pd_abs_basics/blob/main/quick_starts_numpy.ipynb
https://matplotlib.org/stable/tutorials/introductory/usage.html
Shortest example:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(a, b) # a and b are NumPy arraysfig = plt.figure(): create an empty figure with no Axes (pyplot-style)fig, ax = plt.subplots(): create a figure with a single Axes (OO-style)fig, ax = plt.subplots(2, 2): create a figure with a 2×2 grid of Axes
- The Axes class and its member functions are the main entry point for the OO interface.
- For each Axes method, there is a corresponding function in
matplotlib.pyplotthat operates on the current axes.
-
ax.set_title() -
ax.legend() -
ax.set_xlabel(),ax.set_ylabel() -
ax.set_yscale('log') -
ax.tick_params(**kwargs)Example:kwargs = {'labelsize': 14} -
Recommended helper function:
def my_plotter(ax, data1, data2, param_dict): out = ax.plot(data1, data2, **param_dict) return out
-
Convert all inputs (e.g. from
pandas.DataFrame) tonumpy.arrayobjects before plotting. -
Prefer the OO-style interface.
https://pandas.pydata.org/docs/getting_started/intro_tutorials/index.html
- Series: one-dimensional container of scalars
- DataFrame: two-dimensional container of Series
Use functions such as pd.read_csv(), pd.to_csv(), pd.read_excel(), etc.
-
Select specific columns
df["Age"]→ returns a Seriesdf[["Age"]]→ returns a DataFrame
-
Filter specific rows
df[df["Age"] > 35](returns rows where the condition is true)
-
Select specific rows and columns
df.loc[df["Age"] > 35, "Name"]df.iloc[9:25, 2:5]