Show Menu
Cheatography

Python Graphics Cheat Sheet (DRAFT) by

Plotting charts in Python

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Packages

import matplo­tli­b.p­yplot as plt
import seaborn as snb
visually nice, high-level interface to Matplo­tlib, might still need Matplotlib for custom­isation
import plotly.ex­press as px
has hover tool capabi­lities, 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 = subplo­ts(2, 1, sharex­=True, sharey­=True)
axes[0­].p­lot(x, y1)
axes[1].plot(x,y2)
create a 2 by 1 subplot
fig = plt.fi­gure()
plt.plot(x, y)
figsiz­e=(­10,6)
another way of writing the code, without subplots
plt.ti­ght­_la­yout()
auto fit subplots in area
plt.show()
print (use this when not using jupyter notebook)
fig.show()
 

Charts

Line Chart
plt.pl­ot(x, y)
label=­'li­ne1', c='red', ls='--' / ':'
Bar Chart
plt.bar(x, y)
edgeco­lor­='b­lack'
Histogram (for freq)
plt.hi­st(­data)
bins (can be int or seq), rwidth=0.8
Stack plot
plt.st­ack­plot(x, y1, y2, colour­s=['r', 'b'])
Scatter plot
plt.sc­att­er(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 Decora­tions & Others

plt.ti­tle­('abc')
plt.su­pti­tle­('abc')
super title (useful when there are multiple subplots)
plt.yx­abe­l('­desc')
X axis label
plt.yl­abe­l('­desc')
Y axis label
plt.le­gend()
loc='best' / 'upper right' / 'lower center'