Data Visualization with Matplotlib

11 / 20

Getting Stared with Matplotlib - Creating Bar Graphs using Pyplot

Now we will learn how to create bar graphs in Matplotlib using Pyplot.

Bar graphs are one of the most common types of graphs. A bar graph is used to compare sets of data between different groups. The graph represents categories on one axis and a discrete value in the other. The goal is to show the relationship between the two axes. They can also be used to track changes over time. However, when trying to measure change over time, bar graphs are best when the changes are larger.

Pyplot provides a method bar to make bar graphs which take arguments: categorical variables, their values and color (if you want to specify any).

  • First, we will import Pyplot:

    import << your code goes here >> as plt
  • Let us first create 2 arrays; subjects, and their respective marks:

    subjects = ["Maths", "Biology", "Chemistry", "Physics", "English", "Computers"]
    marks = [97, 68, 59, 81, 77, 92]
  • Now let us describe the plot as a bar graph:

    plt.<< your code goes here >>(subjects, marks, color='blue')
  • Next, we will define the title, and axis labels:

    plt.<< your code goes here >>("Bar Graph Example")
    plt.<< your code goes here >>("Subjects")
    plt.<< your code goes here >>("Marks")
  • And finally, we will show the plot:

    plt.<< your code goes here >>

We can create horizontal bar graphs using the barh method instead of the bar method.

Get Hint See Answer

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

Loading comments...