image_output_1.png
import matplotlib.pyplot as plt

# create blank figure

fig, ax = plt.subplots()
plt.show()

# resize figure

fig, ax = plt.subplots(figsize=(10,10))
plt.show()

# set axis with xlim and ylim, title, labels

fig,ax = plt.subplots()

ax.set(xlim=[0.5, 4.5], ylim=[-2, 8], 
       title='An Example Axes',
       ylabel='Y-Axis', xlabel='X-Axis')

plt.show()

# create multiple subplots along rows

fig, ax = plt.subplots(nrows=2)

# create multiple subplots along columns

fig, ax = plt.subplots(ncols=2)

# create multiple subplots along rows and columns

fig, ax = plt.subplots(nrows=2,ncols=2)
plt.show()

# create multiple subplots without overlapping

fig, ax = plt.subplots(nrows=2,ncols=2)
plt.tight_layout() # avoid overlapping
plt.show()

# define the index of subplots

fig, axes = plt.subplots(nrows=2, ncols=2)

axes[0,0].set(title='Upper Left [0,0]')
axes[0,1].set(title='Upper Right [0,1]')
axes[1,0].set(title='Lower Left [1,0]')
axes[1,1].set(title='Lower Right [1,1]')

plt.tight_layout()
plt.show()

x = [10, 15, 20, 25, 30, 35, 40]
y = [20, 24, 28, 32, 36, 40, 44]

fig, ax = plt.subplots(nrows=2,ncols=2,figsize=(10,10))

ax[0,0].plot(x,y)
ax[0,0].bar(x,y) # can create multiple plots in same subplot
ax[0,1].scatter(x,y)
ax[1,0].bar(x,y)
ax[1,1].barh(x,y)

plt.show()

# line plot with artist element

fig, ax = plt.subplots()

ax.plot(x,y, color='red',linewidth=2,
        marker='o', linestyle='--',
        label = 'sales')
ax.set(xlim=[0, 50], ylim=[0, 60], 
       title='Line Chart',
       ylabel='Y-Axis', xlabel='X-Axis')

ax.legend()

plt.show()

fig, ax = plt.subplots()
ax.bar(x, y, color='green', label='sales')
ax.set(xlim=[0, 50], ylim=[0, 60], title='Bar Chart', ylabel='Y-Axis', xlabel='X-Axis')
ax.legend()
plt.show()