Enrollments Open for Certification Course on Artificial Intelligence and Deep Learning by IIT Roorkee

  Apply Now

Getting Stared with Matplotlib - Subplots in Matplotlib

At times, we might want to display more than one chart at a time. We can achieve this by using subplots.

  • First, import Pyplot

    import matplotlib.<< your code goes here >> as plt
  • Now we will create a sample plot


    We did this to demonstrate that creating a subplot will delete any pre-existing subplot that overlaps with it beyond sharing a boundary.

  • Now, in the same cell as before, we will create a subplot using the subplot() function with top plot of a grid with 2 rows and 1 column. Since this subplot will overlap the first, the plot (and its axes) previously created, will be removed

    plt.<< your code goes here >>(2,1,1)
    plt.subplot(2,1,2, facecolor='red')

    The third line creates a second subplot with red background.

  • You can add an insert plot in the same figure by adding another axes object in the same figure canvas. The matplotlib.figure module contains the Figure class. It is a top-level container for all plot elements. The Figure object is instantiated by calling the figure() function of the pyplot module. You can create it as follows:

    import matplotlib.pyplot as plt
    import numpy as np
    import math
    x = np.arange(0, math.pi*2, 0.09)
    axes1 = fig.add_axes([0.1, 0.1, 0.9, 0.9])
    axes2 = fig.add_axes([0.62, 0.62, 0.3, 0.3])
    y = np.sin(x)
    axes1.plot(x, y, 'b')

    Here, axes1 is the main axes, and axes2 is the inset axes. You can test with different values to move and resize the charts.

No hints are availble for this assesment

Answer is not availble for this assesment

Note - Having trouble with the assessment engine? Follow the steps listed here

Loading comments...