Challenge: Plot Department Distribution
Recalling what you have learned about working with lists and dictionaries, you know that counting items in a list can be efficiently achieved using a dictionary, where each unique item serves as a key and its value is the count of occurrences. This is particularly useful in HR scenarios, such as determining how many employees belong to each department. You have also previously explored the basics of using matplotlib to create bar charts, which is a powerful way to visualize categorical data like department distributions.
123456789101112departments = ["HR", "IT", "Finance", "IT", "HR", "Marketing", "Finance", "IT"] # Count occurrences using a dictionary department_counts = {} for dept in departments: if dept in department_counts: department_counts[dept] += 1 else: department_counts[dept] = 1 print(department_counts) # Output: {'HR': 2, 'IT': 3, 'Finance': 2, 'Marketing': 1}
Once you have the counts for each department stored in a dictionary, you can pass the department names and their corresponding counts to matplotlib's bar chart function. The keys of the dictionary will serve as the labels for each bar, and the values will determine the height of each bar, making it easy to visualize the employee distribution across departments.
Swipe to start coding
Write a function that takes a list of department names and visualizes the number of employees in each department as a bar chart.
- Count the number of occurrences for each unique department in the list.
- Use the department names as labels on the x-axis and their counts as the heights of the bars.
- Display the bar chart with appropriate axis labels and a title.
Lösning
Tack för dina kommentarer!
single
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Fantastiskt!
Completion betyg förbättrat till 4.76
Challenge: Plot Department Distribution
Svep för att visa menyn
Recalling what you have learned about working with lists and dictionaries, you know that counting items in a list can be efficiently achieved using a dictionary, where each unique item serves as a key and its value is the count of occurrences. This is particularly useful in HR scenarios, such as determining how many employees belong to each department. You have also previously explored the basics of using matplotlib to create bar charts, which is a powerful way to visualize categorical data like department distributions.
123456789101112departments = ["HR", "IT", "Finance", "IT", "HR", "Marketing", "Finance", "IT"] # Count occurrences using a dictionary department_counts = {} for dept in departments: if dept in department_counts: department_counts[dept] += 1 else: department_counts[dept] = 1 print(department_counts) # Output: {'HR': 2, 'IT': 3, 'Finance': 2, 'Marketing': 1}
Once you have the counts for each department stored in a dictionary, you can pass the department names and their corresponding counts to matplotlib's bar chart function. The keys of the dictionary will serve as the labels for each bar, and the values will determine the height of each bar, making it easy to visualize the employee distribution across departments.
Swipe to start coding
Write a function that takes a list of department names and visualizes the number of employees in each department as a bar chart.
- Count the number of occurrences for each unique department in the list.
- Use the department names as labels on the x-axis and their counts as the heights of the bars.
- Display the bar chart with appropriate axis labels and a title.
Lösning
Tack för dina kommentarer!
single