Line, Bar and Pie Charts
Line, Bar, and Pie Charts in Python
Choosing the correct visualization type is essential for communicating data insights effectively. In this guide, we dive into the three most essential chart types in Matplotlib: Line Charts (for continuous trends), Bar Charts (for categorical comparisons), and Pie Charts (for proportional composition).
1. Line Charts: Trends Over Time (ax.plot)
Line charts excel at displaying continuous variables over a sequential progression (such as dates, months, or timestamps):
2. Bar Charts: Categorical Comparisons (ax.bar & ax.barh)
Bar charts compare discrete numerical values across distinct nominal categories.
Vertical & Horizontal Bar Charts
Grouped Bar Charts
To display multi-series comparisons across categories, offset the X-coordinates by the bar width:
3. Pie & Donut Charts: Proportions of a Whole (ax.pie)
Pie charts display how individual segments contribute to 100% of a whole.
Creating a Modern Donut Chart
Add a white center circle to transform a standard pie chart into a modern donut chart:
4. When to Use Which Chart?
| Chart Type | Best Used When... | Avoid When... |
|---|---|---|
| Line Chart | Showing trends, rate of change, or time-series | Categories have no logical sequence |
| Bar Chart | Comparing discrete categorical amounts | You have hundreds of continuous data points |
| Pie Chart | Showing 2-5 simple parts of a 100% total | You have >6 slices or tiny differences in size |
Multiple Choice Questions
1. Which chart type is best suited for visualizing website traffic trends over 30 days?
A. Pie chart B. Line chart C. Scatter plot D. Donut chart Answer: B Explanation: Line charts are specifically designed to illustrate continuous trends and rates of change across sequential intervals like time.
2. Which method on a Matplotlib Axes object creates a horizontal bar chart?
A. ax.hbar() B. ax.barh() C. ax.horizontal_bar() D. ax.plot_bar(horizontal=True) Answer: B Explanation: ax.barh() creates horizontal bar plots, whereas ax.bar() creates vertical bar plots.
3. What does the parameter autopct="%1.1f%%" accomplish in ax.pie()?
A. It rounds values to the nearest integer B. It automatically formats and displays the percentage contribution on each slice with one decimal place C. It sorts the slices in descending order D. It colors each slice automatically Answer: B Explanation: autopct formats the numeric percentage label rendered inside each pie slice.
4. What is the primary visual drawback of using pie charts with more than 7 or 8 categories?
A. Pie charts consume too much GPU memory B. Slices become narrow, cluttered, and difficult for human eyes to compare accurately C. Matplotlib crashes with more than 5 slices D. Percentages cannot add up to 100% Answer: B Explanation: Human visual perception struggles to compare angles when there are numerous small slices; a horizontal bar chart is far more legible for many categories.
5. In a multi-line chart, what method must be called to display the legend mapping line colors to their respective labels?
A. ax.show_labels() B. ax.legend() C. ax.render_key() D. ax.display_info() Answer: B Explanation: ax.legend() renders the legend box mapping line labels to their respective colors and markers.
Customizing Graphs
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Introduction to Matplotlib | Customizing Graphs |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.