Python #5 | 3rd-Party Modules


Introduction



Matplotlib

  • Interface

    • pyplot interface : Rely on pyplot to automatically create and manage the figures and axes, and use pyplot functions for plotting. (MATLAB-like interface)

    • object-oriented interface : Explicitly create figures and axes, and call methods on them

import matplotlib.pyplot as plt
import numpy as np

# pyplot interface (MATLAB-like, rely on pyplot)
x = np.linspace(0,2,100)
plt.plot(x,x**2)
# Recommended) object-oriented interface (create figures and axes manually)
fig, ax = plt.subplots(figsize=(15,4))
ax.plot(x,x**2)
  • Plot
fig, ax = plt.subplots()
ax.plot(x,x**2)
  • Axes
# Axes 
fig, ax = plt.subplots()
# set axes
ax.set_title("Title")
ax.set_xlabel('xlabel')
# set limits
ax.set_ylim([0, 1e5])
# rotate tick
ax.tick_params(axis = 'x', labelrotation =90)


Pandas

  • Groupby
# select columns
df_channel = df[['COLUMN_A', "COLUMN_1", "COLUMN_2", "COLUMN_3", "COLUMN_4"]]
# groupby => will return aggregated list of each columns by COLUMN_A
df_channel_group = df_channel.groupby('COLUMN_A').agg(lambda x: list(x))