0
0
Matplotlibdata~30 mins

Diverging bar charts in Matplotlib - Mini Project: Build & Apply

Choose your learning style9 modes available
Diverging Bar Charts
📖 Scenario: You are working as a data analyst for a company that wants to visualize customer satisfaction survey results. The survey asked customers to rate their satisfaction on a scale from -5 (very dissatisfied) to +5 (very satisfied). You want to create a diverging bar chart to clearly show positive and negative feedback.
🎯 Goal: Build a diverging bar chart using matplotlib that displays customer satisfaction scores for different product features. Positive scores should extend to the right, negative scores to the left, with bars colored differently based on satisfaction.
📋 What You'll Learn
Create a dictionary called features with exact keys and integer values for satisfaction scores.
Create a list called colors that assigns 'green' for positive scores and 'red' for negative scores.
Use a for loop with variables feature and score to plot horizontal bars with correct colors and positions.
Print the matplotlib figure with the diverging bar chart showing feature names on the y-axis.
💡 Why This Matters
🌍 Real World
Diverging bar charts are useful to show positive and negative feedback clearly, such as customer satisfaction, survey results, or sentiment analysis.
💼 Career
Data analysts and scientists often use diverging bar charts to communicate insights from data that has both positive and negative values, making it easier for stakeholders to understand.
Progress0 / 4 steps
1
Create the data dictionary
Create a dictionary called features with these exact entries: 'Ease of Use': 4, 'Performance': -3, 'Design': 5, 'Support': -2, 'Price': -4.
Matplotlib
Need a hint?

Use curly braces {} to create the dictionary with the exact keys and values.

2
Create the colors list
Create a list called colors that contains 'green' for each positive score and 'red' for each negative score in features.values(). Use a list comprehension with score as the variable.
Matplotlib
Need a hint?

Use a list comprehension with an if condition inside square brackets.

3
Plot the diverging bar chart
Import matplotlib.pyplot as plt. Use plt.barh() inside a for loop with variables feature, score, and color to plot horizontal bars. Use enumerate(features.items()) to get the index i. Set the bar length to score, the y-position to i, and the bar color to color. Set align='center'.
Matplotlib
Need a hint?

Use enumerate and zip together to loop over features and colors with index.

4
Display the chart with labels
Set the y-axis ticks to the range of feature count using plt.yticks() with positions as range(len(features)) and labels as list(features.keys()). Add a vertical line at x=0 using plt.axvline(0, color='black'). Finally, call plt.show() to display the diverging bar chart.
Matplotlib
Need a hint?

Use plt.yticks() to label bars and plt.axvline() to draw the center line.