Bar Charts and Data Sorting with Matplotlib
Master Data Visualization with Matplotlib Bar Charts
Charts display data in an X-Y coordinate system where the y-axis typically represents numeric values and the x-axis represents categories or time series data.
Vertical vs Horizontal Bar Charts
| Feature | Vertical Bars | Horizontal Bars |
|---|---|---|
| Function | plt.bar | plt.barh |
| Label Readability | Poor for long labels | Excellent readability |
| Best Use Case | Short category names | Long category names |
Setting Up Bar Chart Data
Extract Count Values
Convert the count column to a list using edu_group_df['count'] and listify it for chart data
Get Index Labels
Extract category names from the DataFrame index using list() on the index values
Prepare Chart Variables
Create two datasets: bar labels and numeric values to set bar lengths
Use enumerate() to unlock both the index and item when looping. Syntax: for index, item in enumerate(some_list). This is essential for positioning chart labels.
Loop Access Patterns
Regular For Loop
Provides access only to the item itself. Limited when you need positional information for chart labeling.
enumerate() Loop
Unlocks both index and item access. Essential for chart text positioning and consecutive data pairing.
Creating the Bar Chart
Initialize Chart
Use plt.barh() for horizontal bars, feeding in category list and count values
Reverse Order
Apply .reverse() to both lists to display largest bars at the top
Add Data Labels
Use plt.text() with enumerate to position count values next to each bar
Format and Style
Set colors, title, axis labels, and adjust chart limits for proper spacing
Chart Enhancement Checklist
Use single color for same data type, like DodgerBlue for all bars
Charts should always have clear, informative titles
X-axis should be labeled, y-axis when categories aren't obvious
Use plt.text() with count + offset for breathing room
Use plt.xlim() to widen chart beyond default data range
Use va='center' for vertical alignment of labels
Customize chart element colors including title, axis labels, and ticks using color parameters. Use hex codes like #555 or #237 for precise color control.
Single vs Multi-Level Sorting
| Feature | Single Sort | Multi-Level Sort |
|---|---|---|
| Syntax | sort_values(by='column') | sort_values(by=['col1', 'col2']) |
| Use Case | Simple ranking | Tiebreaker scenarios |
| Result | May have unclear ties | Clear hierarchical ranking |
Advanced Data Sorting
Primary Sort
Sort by main criteria like average score using ascending=False for descending order
Secondary Sort
Add second column as tiebreaker, such as math_score for tied average scores
Slice Results
Use slicing to display top results, like top 40 students with highest scores
You've progressed through core programming, NumPy, Pandas, and now Matplotlib visualization. This foundation enables endless possibilities for data analysis and presentation.
This lesson is a preview from our Data Science & AI Certificate Online (includes software) and Python Certification Online (includes software & exam). Enroll in a course for detailed lessons, live instructor support, and project-based training.
Key Takeaways