Zhiguang Huo (Caleb)
Thursday Nov 16th, 2023
A toy example
fig = plt.figure()
ax = fig.add_subplot()
ax.plot()
fig, ax = plt.subplots()
ax.plot()
plt.figure()
plt.subplot()
plt.plot()
Single figure
Multiple subfigures
Single figure
Multiple subfigures
## array([[<Axes: >, <Axes: >],
## [<Axes: >, <Axes: >]], dtype=object)
fig, axes = plt.subplots(2,3,sharex=True, sharey=True)
axes[0,0].plot(data_cumsum, marker = "o")
axes[0,1].plot(data_cumsum, marker = "v")
axes[0,2].plot(data_cumsum, marker = "^")
axes[1,0].plot(data_cumsum, marker = "D")
axes[1,1].plot(data_cumsum, marker = "X")
axes[1,2].plot(data_cumsum, marker = "s")
plt.show()
also works for y axis (set_yticks, set_xticklabels, etc)
df = pd.DataFrame({"names":['A', 'B', 'C'], "values":[1,2,3]})
plt.figure(figsize=(9, 3))
plt.subplot(131)
plt.bar("names", "values", data=df)
plt.subplot(132)
plt.scatter("names", "values", data=df)
plt.subplot(133)
plt.plot("names", "values", data=df)
plt.suptitle('Categorical Plotting')
plt.show()
pd1 = pd.DataFrame(np.random.rand(4,3), index = list("abcd"), columns = ["Florida", "Texax", "Utah"])
pd1.columns.name="Columns"
pd1.index.name="States"
fig = plt.figure(figsize=(6, 3))
ax = fig.add_subplot(1,2,1)
pd1.plot.bar(ax = ax)
bx = fig.add_subplot(1,2,2)
pd1.plot.barh(ax = bx, stacked=True)
plt.show()
np.random.seed(32608)
plt.subplot(211)
plt.imshow(np.random.random((100, 100)))
plt.subplot(212)
plt.imshow(np.random.random((100, 100)))
cax = plt.axes([0.85, 0.1, 0.075, 0.8])
plt.colorbar(cax=cax)
## <matplotlib.colorbar.Colorbar object at 0x2a4cd1b10>