Skip to content
Programs · Program 4-18
Q.

(Program 4-18) Customise the pie plot of Program 4-17 (the forest-cover data of Table 4.10, GeoArea/ForestCover by state)

StateGeoAreaForestCover
Arunachal Pradesh8374367353
Assam7843827692
Manipur2232717280
Meghalaya2242917321
Mizoram2108119240
Nagaland1657913464
Tripura104868073
Yanam CbseNCERTSubjective· 4mImportance★★★★★est
48% · 21/44 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

This solution customizes a matplotlib pie chart to display forest cover data. It demonstrates how to use the explode parameter to separate specific wedges, the autopct parameter to show percentage labels, and the colors parameter to set custom wedge colors.

Pie charts are a common way to visualize how different parts contribute to a whole. Each slice represents a category's proportion of the total. While intuitive for showing "parts of a whole," they are often misused. Comparing the sizes of different slices, especially when there are many categories or when slice sizes are similar, can be difficult for the human eye. For precise comparisons, bar charts are generally more effective. However, for a clear, high-level overview of a few distinct proportions, a well-designed pie chart can be quite informative.

In this problem, we are asked to customize a pie chart to enhance its readability and highlight specific data points. We will use matplotlib to achieve this, focusing on three key properties:

  1. Explode: This property allows you to "pull out" one or more slices from the center of the pie, drawing attention to them. The value for explode is a tuple or list, where each element corresponds to a slice and specifies the fraction of the radius by which that slice should be offset. A value of 0.0 means no explosion.
  2. Autopct: This feature automatically displays the percentage value for each slice directly on the chart. It takes a Python format string (e.g., '%1.1f%%') or a function as an argument, which dictates how the percentage text will be formatted.
  3. Colors: This allows you to assign specific colors to each wedge, improving visual distinction and aligning with any desired color scheme.

Here's the Python code to create the customized pie chart:

import matplotlib.pyplot as plt

# Data from Table 4.10 (ForestCover values)
states = [
    'Arunachal Pradesh', 'Assam', 'Manipur', 'Meghalaya',
    'Mizoram', 'Nagaland', 'Tripura'
]
forest_cover = [67353, 27692, 17280, 17321, 19240, 13464, 8073]

# Customization parameters
# Explode the first wedge (Arunachal Pradesh) by 0.1 and the fifth (Mizoram) by 0.2
explode_values = (0.1, 0.0, 0.0, 0.0, 0.2, 0.0, 0.0)

# Set custom colors for the wedges
wedge_colors = ['r', 'g', 'm', 'c', 'brown', 'pink', 'purple']

# Create the pie chart
plt.figure(figsize=(10, 8)) # Optional: Adjust figure size for better readability
plt.pie(
    forest_cover,
    explode=explode_values,
    labels=states,
    colors=wedge_colors,
    autopct='%1.1f%%', # Display percentage with one decimal place
    shadow=True,       # Optional: Add a shadow for better visual effect
    startangle=90      # Optional: Start the first wedge at 90 degrees (top)
)

# Add a title to the chart
plt.title('Forest Cover Distribution in Northeastern States')

# Ensure the pie chart is drawn as a circle (equal aspect ratio)
plt.axis('equal')

# Display the chart
plt.show()

Explanation of Key Lines

  1. import matplotlib.pyplot as plt: This line imports the pyplot module from the matplotlib library, which provides a MATLAB-like interface for plotting. It's conventionally aliased as plt.
  2. states = [...] and forest_cover = [...]: These lines define the labels (state names) and the numerical data (forest cover values) that will be used to create the pie chart. The forest_cover list represents the sizes of the wedges.
  3. explode_values = (0.1, 0.0, 0.0, 0.0, 0.2, 0.0, 0.0): This tuple specifies the "explosion" for each wedge.
    • The first value 0.1 means the first wedge (Arunachal Pradesh) will be pulled out by 10% of the radius.
    • The fifth value 0.2 means the fifth wedge (Mizoram) will be pulled out by 20% of the radius.
    • All other 0.0 values indicate that those wedges will remain in their normal position.
  4. wedge_colors = ['r', 'g', 'm', 'c', 'brown', 'pink', 'purple']: This list assigns specific colors to each wedge in the order they appear in the forest_cover data. r is red, g is green, m is magenta, c is cyan, and the others are named colors.
  5. plt.pie(...): This is the core function call that generates the pie chart.
    • forest_cover: The data array whose values are used to determine the size of each wedge.
    • explode=explode_values: Applies the defined explosion effect to the wedges.
    • labels=states: Assigns the state names as labels to each wedge. These labels typically appear in a legend.
    • colors=wedge_colors: Sets the custom colors for the wedges. …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.