x = np.arange(-10, 11)
plt.figure(figsize=(12, 6))

plt.title('My Nice Plot')

plt.plot(x, x ** 2)
plt.plot(x, -1 * (x ** 2))

image.png

fig, axes = plt.subplots(figsize=(12, 6))

axes.plot(x, x + 0, linestyle='solid')
axes.plot(x, x + 1, linestyle='dashed')
axes.plot(x, x + 2, linestyle='dashdot')
axes.plot(x, x + 3, linestyle='dotted');

axes.set_title("My Nice Plot")

image.png

We can enhance this graph by giving it markers such as the following :

fig, axes = plt.subplots(figsize=(12, 6))

axes.plot(x, x + 0, '-og', label="solid green")
axes.plot(x, x + 1, '--c', label="dashed cyan")
axes.plot(x, x + 2, '-.b', label="dashdot blue")
axes.plot(x, x + 3, ':r', label="dotted red")

axes.set_title("My Nice Plot")

axes.legend()

Other type of plots -

We can call the subplots() function where we get a tuple containing a figure and a axes element

plot_objects = plt.subplots()

fig, ax = plot_objects

#Row,column,left or right
ax.plot([1,2,3], [1,2,3])

plot_objects

We can also define how many elements we want inside our figure. To do this we can set the number of rows and number of columns params

plot_objects = plt.subplots(nrows=2, ncols=2,figsize=(14,6))

figm ((ax1, ax2)), ((ax3, ax4)) = plot_objects

plot_objects

image.png

ax4.plot(np.random.randn(50), c='yellow')
ax1.plot(np.random.randn(50), c='red', linestyle='--')
ax2.plot(np.random.randn(50), c='green', linestyle=':')
ax3.plot(np.random.randn(50), c='blue', marker='o', linewidth=3.0)

fig

image.png

The subplot2grid command -