#NoPayJan Offer - Access all CloudxLab Courses for free between 1st to 31st Jan

  Enroll Now >>

Getting Stared with Matplotlib - Creating Clustered Bar Graphs using Pyplot

Now we will create a horizontal Clustered bar graph.

The clustered bar graph extends the standard bar graph from looking at numeric values across one categorical variable to two or more categorical variables. Each bar in a standard bar graph is divided into a number of sub-bars stacked side to side, each one corresponding to a level of the subsequent categorical variables.

Continuing the example from the previous assessment, here we will compare the marks of 3 students instead of a single student for all the subjects.

  • Let's start by importing Pyplot, and Numpy:

    import << your code goes here >> as plt
    import << your code goes here >> as np
  • Now let us define the subjects, and the marks for the 3 students:

    subjects = ["Maths", "Biology", "Chemistry", "Physics", "English", "Computers"]
    student1 = [97, 68, 59, 81, 77, 92]
    student2 = [88, 61, 80, 40, 62, 52]
    student3 = [54, 62, 77, 54, 71, 98]
  • Next we will define an array called index, and a variable called width. Here we mention the index and width of our bar graphs in order to horizontally stack them together.

    index = np.arange(6)
    width = 0.03
  • Note: Please write all the following codes till the end in a single cell. Now let's plot the bars. We will use the same bar method in this case, but a bit differently:

    plt.<< your code goes here >>(index, student1, width, color="aqua", label="Student 1")
    plt.<< your code goes here >>(index + width, student2, width, color="green", label="Student 2")
    plt.<< your code goes here >>(index + (width*2), student3, width, color="blue", label="Student 3")
  • Now let us set the title, and axis labels:

    plt.<< your code goes here >>("Stacked Bar Graph Example")
    plt.<< your code goes here >>("Students")
    plt.<< your code goes here >>("Marks")
  • Next, we will use the xticks method to label our x-axis based on the position of our bars.

    plt.<< your code goes here >>(index + width/2, subjects)
  • And we will plot the legends:

  • Finally, we will show our plot:

    plt.<< your code goes here >>

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...