Bird
Raised Fist0
Matplotlibdata~10 mins

Pie chart color customization in Matplotlib - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to set the colors of the pie chart slices.

Matplotlib
import matplotlib.pyplot as plt
sizes = [15, 30, 45, 10]
colors = [1]
plt.pie(sizes, colors=colors)
plt.show()
Drag options to blanks, or click blank then click option'
A['red', 'blue', 'green', 'yellow']
B['circle', 'square', 'triangle', 'star']
C[15, 30, 45, 10]
D['apple', 'banana', 'cherry', 'date']
Attempts:
3 left
💡 Hint
Common Mistakes
Using shapes or numbers instead of color names.
Passing the sizes list as colors.
2fill in blank
medium

Complete the code to add labels and set custom colors for the pie chart.

Matplotlib
import matplotlib.pyplot as plt
sizes = [20, 25, 35, 20]
labels = ['A', 'B', 'C', 'D']
colors = [1]
plt.pie(sizes, labels=labels, colors=colors)
plt.show()
Drag options to blanks, or click blank then click option'
A['circle', 'square', 'triangle', 'star']
B['cyan', 'magenta', 'yellow', 'black']
C['red', 'green', 'blue']
D[20, 25, 35, 20]
Attempts:
3 left
💡 Hint
Common Mistakes
Using fewer colors than slices.
Using invalid color names or shapes.
3fill in blank
hard

Fix the error in the code to correctly set the pie chart colors.

Matplotlib
import matplotlib.pyplot as plt
sizes = [10, 40, 30, 20]
colors = ['red', 'blue', 'green']
plt.pie(sizes, colors=[1])
plt.show()
Drag options to blanks, or click blank then click option'
A['red', 'blue', 'green', 'yellow']
Bsizes
Ccolors
D['red', 'blue']
Attempts:
3 left
💡 Hint
Common Mistakes
Using a colors list shorter than sizes list.
Passing sizes as colors.
4fill in blank
hard

Fill both blanks to create a pie chart with exploded slices and custom colors.

Matplotlib
import matplotlib.pyplot as plt
sizes = [25, 35, 20, 20]
colors = [1]
explode = [2]
plt.pie(sizes, colors=colors, explode=explode)
plt.show()
Drag options to blanks, or click blank then click option'
A['gold', 'lightcoral', 'lightskyblue', 'yellowgreen']
B[0.1, 0, 0, 0]
C['red', 'blue', 'green', 'yellow']
D[1, 1, 1, 1]
Attempts:
3 left
💡 Hint
Common Mistakes
Using explode values outside 0 to 1 range.
Mismatch between colors and sizes length.
5fill in blank
hard

Fill all three blanks to create a pie chart with labels, custom colors, and exploded slices.

Matplotlib
import matplotlib.pyplot as plt
sizes = [30, 20, 25, 25]
labels = [1]
colors = [2]
explode = [3]
plt.pie(sizes, labels=labels, colors=colors, explode=explode)
plt.show()
Drag options to blanks, or click blank then click option'
A['Apples', 'Bananas', 'Cherries', 'Dates']
B['#ff9999', '#66b3ff', '#99ff99', '#ffcc99']
C[0, 0.1, 0, 0]
D['red', 'blue', 'green']
Attempts:
3 left
💡 Hint
Common Mistakes
Using fewer colors than slices.
Exploding all slices instead of one.

Practice

(1/5)
1. What does the colors parameter do in a matplotlib pie chart?
easy
A. It changes the size of the pie chart.
B. It changes the chart title.
C. It adds labels to the pie slices.
D. It sets the colors of each slice in the pie chart.

Solution

  1. Step 1: Understand the role of colors in pie charts

    The colors parameter is used to specify the colors for each slice of the pie chart.
  2. Step 2: Compare with other options

    Options A, C, and D relate to size, labels, and title, which are controlled by other parameters.
  3. Final Answer:

    It sets the colors of each slice in the pie chart. -> Option D
  4. Quick Check:

    colors parameter = slice colors [OK]
Hint: Remember: colors parameter controls slice colors [OK]
Common Mistakes:
  • Confusing colors with labels
  • Thinking colors change chart size
  • Mixing colors with title settings
2. Which of the following is the correct way to set colors in a matplotlib pie chart?
easy
A. plt.pie(data, colors=['red', 'blue', 'green'])
B. plt.pie(data, color=['red', 'blue', 'green'])
C. plt.pie(data, col=['red', 'blue', 'green'])
D. plt.pie(data, set_colors=['red', 'blue', 'green'])

Solution

  1. Step 1: Recall the correct parameter name

    The correct parameter to set slice colors is colors, not color, col, or set_colors.
  2. Step 2: Check syntax correctness

    plt.pie(data, colors=['red', 'blue', 'green']) uses colors with a list of color names, which is the correct syntax.
  3. Final Answer:

    plt.pie(data, colors=['red', 'blue', 'green']) -> Option A
  4. Quick Check:

    Use colors= list for slice colors [OK]
Hint: Use exact parameter name 'colors' for slice colors [OK]
Common Mistakes:
  • Using 'color' instead of 'colors'
  • Using undefined parameters like 'col' or 'set_colors'
  • Passing colors as a string instead of a list
3. What will be the color of the second slice in this code?
import matplotlib.pyplot as plt
sizes = [30, 40, 30]
colors = ['#FF0000', '#00FF00', '#0000FF']
plt.pie(sizes, colors=colors)
plt.show()
medium
A. Blue
B. Red
C. Green
D. Black

Solution

  1. Step 1: Identify the colors list order

    The colors list is ['#FF0000', '#00FF00', '#0000FF'], which correspond to red, green, and blue in order.
  2. Step 2: Match the second slice color

    The second slice uses the second color '#00FF00', which is green.
  3. Final Answer:

    Green -> Option C
  4. Quick Check:

    Second color in list = green [OK]
Hint: Colors list order matches slice order [OK]
Common Mistakes:
  • Mixing hex codes with color names
  • Assuming default colors override given list
  • Confusing slice index starting at 0 or 1
4. Find the error in this pie chart color code:
import matplotlib.pyplot as plt
sizes = [25, 25, 50]
colors = 'red, blue, green'
plt.pie(sizes, colors=colors)
plt.show()
medium
A. colors should be a list, not a string
B. sizes list has wrong values
C. plt.pie does not accept colors parameter
D. Missing plt.show() call

Solution

  1. Step 1: Check the type of colors parameter

    The colors parameter must be a list of color names or hex codes, but here it is a single string.
  2. Step 2: Understand the impact of wrong type

    Passing a string will cause matplotlib to treat it as one color or raise an error.
  3. Final Answer:

    colors should be a list, not a string -> Option A
  4. Quick Check:

    colors must be list, not string [OK]
Hint: Always pass colors as a list, not a single string [OK]
Common Mistakes:
  • Passing colors as comma-separated string
  • Ignoring type requirements for colors
  • Assuming plt.show() is optional
5. You want to create a pie chart with 4 slices and use custom colors: red, green, blue, and yellow. Which code correctly applies these colors and also adds labels for each slice?
hard
A. plt.pie([10, 20, 30, 40], colors=['red', 'green', 'blue', 'yellow'], label=['A', 'B', 'C', 'D'])
B. plt.pie([10, 20, 30, 40], colors=['red', 'green', 'blue', 'yellow'], labels=['A', 'B', 'C', 'D'])
C. plt.pie([10, 20, 30, 40], colors=['red', 'green', 'blue'], labels=['A', 'B', 'C', 'D'])
D. plt.pie([10, 20, 30, 40], color=['red', 'green', 'blue', 'yellow'], labels=['A', 'B', 'C', 'D'])

Solution

  1. Step 1: Verify the colors parameter and list length

    The colors parameter must be named exactly 'colors' and have 4 colors for 4 slices.
  2. Step 2: Check the labels parameter name and length

    The labels parameter must be named 'labels' and have 4 labels matching the slices.
  3. Step 3: Identify errors in other options

    plt.pie([10, 20, 30, 40], color=['red', 'green', 'blue', 'yellow'], labels=['A', 'B', 'C', 'D']) uses 'color' instead of 'colors'. plt.pie([10, 20, 30, 40], colors=['red', 'green', 'blue'], labels=['A', 'B', 'C', 'D']) has only 3 colors for 4 slices. plt.pie([10, 20, 30, 40], colors=['red', 'green', 'blue', 'yellow'], label=['A', 'B', 'C', 'D']) uses 'label' instead of 'labels'.
  4. Final Answer:

    plt.pie([10, 20, 30, 40], colors=['red', 'green', 'blue', 'yellow'], labels=['A', 'B', 'C', 'D']) -> Option B
  5. Quick Check:

    Use colors= and labels= with matching list lengths [OK]
Hint: Match colors and labels list lengths with data slices [OK]
Common Mistakes:
  • Using wrong parameter names like 'color' or 'label'
  • Mismatched list lengths for colors or labels
  • Forgetting to pass labels parameter
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard