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

  Enroll Now >>

Getting Stared with Matplotlib - Creating Pie Charts using Pyplot

Now we will learn how to create pie charts using Matplotlib.

Pie charts are generally used to show percentage or proportional data and usually the percentage represented by each category is provided next to the corresponding slice of pie. Graphs such as pie charts and bar graphs show descriptive data, or qualitative data.

We will now create a pie chart to show the market share of 5 different firms.

  • First, we will import Pyplot:

    import << your code goes here >> as plt
  • Now, let us define the data:

    firms = ["Firm A", "Firm B", "Firm C", "Firm D", "Firm E"]
    market_shares = [10, 40, 30, 5, 15]
  • Next, we will define an array called Explode. Please note that this is an optional step for creating pie charts. However, if you want to focus on one particular firm, you can explode a part of your pie chart. Here, we will focus on Firm D which has the lowest market shares. So, let us define Explode now:

    Explode = [0, 0, 0, 0.5, 0]

    Please notice that in the above array, we have kept a non-zero value for the 3rd value, this index corresponds to Firm D.

  • Like we did in the last assessment, you need to write all the following codes in a single cell. Now we will create the pie chart using the pie method:

    plt.<< your code goes here >>(market_shares, explode=Explode, labels=firms, shadow=True, startangle=45)

    Here startangle=45 means that everything is rotated counter-clockwise by 45 degrees.

  • Next, we will define the axis and legends:

    plt.legend(title = "List of Firms")

    In the above code, 'equal' denotes equal aspect ratio to ensures that pie is drawn as a circle. Also, the title in legend is the title of the legends and not the entire chart.

  • Finally, we will show the chart:

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