Packages
import matplotlib.pyplot as plt |
import seaborn as snb |
visually nice, high-level interface to Matplotlib, might still need Matplotlib for customisation |
import plotly.express as px |
has hover tool capabilities, visually attractive |
Basics
fig, ax = subplots() ax.plot(x, y1, c='b', ls='-') ax.plot(x,y2, c='r', ls=':')
|
plot on the same subplot |
fig, axes = subplots(2, 1, sharex=True, sharey=True) axes[0].plot(x, y1) axes[1].plot(x,y2) |
create a 2 by 1 subplot |
fig = plt.figure() plt.plot(x, y) |
figsize=(10,6) another way of writing the code, without subplots |
plt.tight_layout() |
auto fit subplots in area |
plt.show() |
print (use this when not using jupyter notebook) |
fig.show() |
|
|
Charts
Line Chart |
plt.plot(x, y) |
label='line1', c='red', ls='--' / ':' |
Bar Chart |
plt.bar(x, y) |
edgecolor='black' |
Histogram (for freq) |
plt.hist(data) |
bins (can be int or seq), rwidth=0.8 |
Stack plot |
plt.stackplot(x, y1, y2, colours=['r', 'b']) |
Scatter plot |
plt.scatter(X, Y) |
marker='x' |
Scatter plot (colour diff group) |
colors=['red' if v==0 else 'blue' if v==1 else 'green' for v in y] plt.scatter(X[:,0], X[:,1]), c=colors) |
y is the label here [0, 1, 2] X[:,0] is the first col of the features, X[:,1] is the second, they are the x and y axis of the scatter plot |
Plot Decorations & Others
plt.title('abc') |
plt.suptitle('abc') |
super title (useful when there are multiple subplots) |
plt.yxabel('desc') |
X axis label |
plt.ylabel('desc') |
Y axis label |
plt.legend() |
loc='best' / 'upper right' / 'lower center' |
|