How to add different graphs (as an inset) in another Python graph?


To add different graphs (as an inset) in another Python graph, we can take the following steps −

  • Create and data points using numpy.

  • Using subplots() method, create a figure and a set of subplots, i.e., fig and ax.

  • To create a new axis, add axis to the existing figure (Step 2).

  • Plot and on the axis (Step 2).

  • Plot and on the new axis (Step 3).

  • To display the figure, use show() method.

Example

import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.linspace(-1, 1, 100)
y = np.sin(x)
fig, ax = plt.subplots()
left, bottom, width, height = [.30, 0.6, 0.2, 0.25]
ax_new = fig.add_axes([left, bottom, width, height])
ax.plot(x, y, color='red')
ax_new.plot(x, y, color='green')
plt.show()

Output

Updated on: 06-May-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements