Data Visualization using Pyplot: Line Chart, Pie Chart and Bar Chart: One Mark Questions, Book Back Answers and Notes
This chapter shows how to draw charts in Python with the Matplotlib library and its Pyplot module. You learn to plot line charts, bar charts and pie charts from Python lists, add labels, titles and legends, and read data at a glance instead of scanning columns of numbers.
Study this chapter
Asked in previous board exams
2 questions from this chapter came in 2 past public exams. Open any of them to see the answer and explanation.
-
March 2024
________ is a collection of resources assembled to create a single unified visual display.See answer in the solved paper →
-
Supplementary 2021
Using Matplotlib from within a Python Script, which method inside the file will display your plot ?See answer in the solved paper →
About Data Visualization using Pyplot: Line Chart, Pie Chart and Bar Chart
Medium ~90 min study
A table of a hundred numbers is hard to read. The same numbers drawn as a line or a set of bars show the pattern in a second. That is the whole idea of data visualization: turning data into a picture so that trends, comparisons and proportions become obvious. In Python, the usual tool for this is Matplotlib, and this chapter uses its Pyplot module.
Everything in the chapter starts from ordinary Python lists. A list of x values and a list of y values give you a line chart. A list of category names and a list of heights give you a bar chart. A list of sizes gives you a pie chart, where Pyplot works out each slice as a share of the total. On top of that you add axis labels, a title, colours and a legend so the reader knows what each line or slice means.
Exam questions from this unit usually ask you to recall a Pyplot function (plot(), bar(), pie(), xlabel(), show()), tell a histogram from a bar chart, or predict what a short script will draw. Type the textbook programs yourself and run them; seeing the output once makes the syntax much easier to remember.
What you'll learn
- Explain what data visualization is and why a chart is easier to read than raw numbers.
- Install Matplotlib with pip and import the Pyplot module as plt.
- Draw line charts with plt.plot() and add axis labels, a title and a legend.
- Draw bar charts with plt.bar() to compare values across categories.
- State the difference between a histogram and a bar graph.
- Draw pie charts with plt.pie() and show percentages on the slices using autopct.
Before you start
- Basic Python: variables, loops and function calls.
- Python lists, since every chart in this chapter is built from lists of values.
- Running a Python script and using pip from the command prompt.
Topics covered in this chapter
Data Visualization using Pyplot: Line Chart, Pie Chart and Bar Chart explained
Charts in Python with Matplotlib and Pyplot
Matplotlib and the Pyplot module
Data visualization is the graphical representation of information and data. Charts, graphs, maps, infographics and dashboards are all forms of it. Matplotlib is the most popular data visualization library in Python and is widely used for 2D plots. You install it from the command prompt with pip install matplotlib and bring it into a program with import matplotlib.pyplot as plt. Pyplot is a collection of functions that make Matplotlib easy to use: each function changes the current figure, for example by drawing a line, adding a label or setting a title. Nothing appears on screen until you call plt.show(), which opens the plot window with its navigation buttons.
Line charts
A line chart shows information as a series of data points, called markers, joined by straight line segments. It suits data that changes over time, such as marks across terms or temperature across months. If you give plt.plot() a single list, for example plt.plot([1, 2, 3, 4]), Pyplot treats those numbers as y values and uses 0, 1, 2, 3 as the x values, because list positions start from zero. To control both axes, pass two lists: plt.plot(x, y). Add plt.xlabel(), plt.ylabel() and plt.title() to describe the chart, and when you draw more than one line, give each a label and call plt.legend().
Bar charts
A bar chart shows the relationship between a numerical variable and a categorical variable using rectangular bars. The length or height of each bar is proportional to the value it stands for, and the bars can be vertical or horizontal. Use it to compare separate groups, such as the number of students in each class. Do not confuse it with a histogram. A bar chart compares discrete categories and its bars have gaps between them. A histogram shows the frequency distribution of a continuous variable grouped into ranges, so its bars touch. In Pyplot, plt.bar() takes the bar positions and the heights, and plt.xticks() can place the category names under the bars.
Pie charts
A pie chart is a circle divided into slices, where each slice shows one category's share of the whole. The angle of each slice is proportional to its value. For example, with sizes 20, 30 and 50, the slices take 20%, 30% and 50% of the circle. plt.pie() does this calculation for you and draws the slices one after another, counter-clockwise by default. The labels argument names the slices, and autopct prints the percentage on each slice; autopct="%.2f" shows it to two decimal places. Pie charts work well for things like a family budget or the share of votes, where the parts add up to one total.
Common mistakes to avoid
- Passing one list to plt.plot() and expecting those numbers on the x-axis. Pyplot uses a single list as y values and fills the x-axis with 0, 1, 2 and so on. Pass plt.plot(x, y) when you want your own x values.
- Leaving out plt.show() at the end of a script. The chart is built in memory but no window opens, so it looks as if nothing happened.
- Calling a chart with touching bars a bar graph. Touching bars over number ranges is a histogram; separate bars for separate categories is a bar graph.
- Giving x and y lists of different lengths to plt.plot() or plt.bar(). Each x value needs exactly one y value, otherwise Python raises an error.
- Calling plt.legend() without giving any line a label. The legend has nothing to show; add label='...' inside each plt.plot() call first.
Test yourself on these with the practice test, then check the worked reasoning in the solved MCQs.
Frequently asked questions
How do I install Matplotlib in Python?
Open the command prompt and type pip install matplotlib. When the installation finishes, write import matplotlib.pyplot as plt at the top of your program. You can check the installation by running a short script: the import line, then plt.plot([1, 2, 3]) and plt.show().
Why does my line chart x-axis start at zero?
You passed only one list to plt.plot(). Pyplot takes those numbers as y values and uses the list positions 0, 1, 2, 3 as x values. So plt.plot([5, 8, 6]) puts the points at x = 0, 1 and 2. Pass two lists, plt.plot(x, y), to set the x values yourself.
What is the difference between a histogram and a bar chart?
A bar chart compares separate categories, like marks in different subjects, and leaves gaps between the bars. A histogram shows how often values of a continuous variable fall into ranges, like the number of students scoring 0-10, 10-20 and so on, and its bars touch each other.
How can I add labels and titles to Pyplot graphs?
Use plt.xlabel('text') for the horizontal axis, plt.ylabel('text') for the vertical axis and plt.title('text') for the heading. If the graph has several lines, give each plt.plot() call a label and then call plt.legend() to show a box naming each line.
How do I show percentage values on a pie chart in Python?
Use the autopct argument in plt.pie(). For example, plt.pie(sizes, labels=labels, autopct='%.2f') works out each slice's share of the total and prints it on the slice with two decimal places. Use '%.1f%%' if you want a percent sign after the number.
What is the purpose of the Home and Zoom buttons in the Pyplot window?
The Zoom button lets you drag a rectangle over part of the chart to see that area enlarged. The Home button takes you back to the original view after zooming or panning. The Save button stores the figure as an image file.
How can I plot multiple lines on the same graph?
Call plt.plot() once for each line before you call plt.show(). All the lines are drawn on the same figure. Give each call a label, such as label='Term 1', and add plt.legend() so the reader can tell the lines apart.
Last updated 15 September 2026