Making a Bar Chart in Python with Matplotlib
Turn numbers into a picture: install matplotlib, draw a bar chart from your data, add titles and labels, colour the bars and save the image. Runnable code, a worked example and a try-it challenge.
Key takeaways
- matplotlib is the standard Python library for drawing charts; install it with pip install matplotlib
- import matplotlib.pyplot as plt, then plt.bar(labels, values) draws a bar chart
- plt.title, plt.xlabel and plt.ylabel add the words that make a chart readable
- plt.show() opens the chart in a window; plt.savefig("chart.png") saves it as an image
- A chart is only as honest as its data and its labels — always label your axes
From numbers to a picture
A column of numbers is hard to feel. The same numbers as bars — tall ones and short ones side by side — tell their story instantly. That's why turning data into charts is one of the most valued skills in coding. In this lesson you'll use matplotlib, Python's most popular plotting library, to draw a clean, labelled bar chart from your own data.
This builds on getting data into the right shape, so it helps to have met loading data into Python and using Python libraries. Let's draw.
Installing matplotlib
matplotlib isn't part of Python's standard library, so install it once from a terminal or command prompt:
pip install matplotlib
(On some systems the command is pip3 install matplotlib.) If you're using Google Colab or many science setups, it's already installed and you can skip this step.
Your first bar chart
Three lines is genuinely all it takes. We give matplotlib a list of labels and a matching list of values:
import matplotlib.pyplot as plt
fruits = ["Apples", "Bananas", "Cherries", "Dates"]
counts = [12, 19, 7, 15]
plt.bar(fruits, counts)
plt.show()
plt.bar(fruits, counts) builds a bar for each fruit, as tall as its count. plt.show() opens a window with the finished chart. The two lists must be the same length — one value per label.
Making it readable: titles and labels
A chart without words is a puzzle. Add a title and label both axes before plt.show():
import matplotlib.pyplot as plt
fruits = ["Apples", "Bananas", "Cherries", "Dates"]
counts = [12, 19, 7, 15]
plt.bar(fruits, counts)
plt.title("Fruit Sold This Week")
plt.xlabel("Fruit")
plt.ylabel("Number sold")
plt.show()
plt.title(...)sets the heading.plt.xlabel(...)labels the horizontal axis (the categories).plt.ylabel(...)labels the vertical axis (the amounts).
These small additions are the difference between a chart and a usable chart.
Colour and style
You can colour the bars to suit the data. Pass one colour for all bars, or a list of colours for individual ones:
import matplotlib.pyplot as plt
fruits = ["Apples", "Bananas", "Cherries", "Dates"]
counts = [12, 19, 7, 15]
colours = ["red", "gold", "crimson", "brown"]
plt.bar(fruits, counts, color=colours)
plt.title("Fruit Sold This Week")
plt.ylabel("Number sold")
plt.show()
matplotlib understands plenty of colour names ("red", "gold", "skyblue") as well as hex codes like "#3498db".
Saving the chart to a file
To put a chart in a report, save it as an image instead of (or as well as) showing it. Use plt.savefig before plt.show(), because show() can clear the figure:
plt.savefig("fruit_chart.png")
plt.show()
This writes fruit_chart.png next to your program. You can save .png, .jpg or .pdf just by changing the file name.
Worked example: chart from a list of dictionaries
Real data usually arrives as a list of dictionaries (one dict per row), not as two neat lists. Here's the full pattern: pull the labels and values out of the data, then plot them.
import matplotlib.pyplot as plt
# Data: average test scores by subject
data = [
{"subject": "Maths", "score": 78},
{"subject": "Science", "score": 85},
{"subject": "English", "score": 72},
{"subject": "History", "score": 80},
{"subject": "Art", "score": 90},
]
# 1. Split the rows into two parallel lists
subjects = [row["subject"] for row in data]
scores = [row["score"] for row in data]
# 2. Draw the chart
plt.bar(subjects, scores, color="skyblue")
# 3. Label everything
plt.title("Average Score by Subject")
plt.xlabel("Subject")
plt.ylabel("Average score")
plt.ylim(0, 100) # fix the scale from 0 to 100
# 4. Show and save
plt.savefig("scores.png")
plt.show()
Step by step:
- Two list comprehensions pull the
subjectandscoreout of every row into matching lists. plt.bardraws the bars, here all in"skyblue".- We title the chart and label both axes.
plt.ylim(0, 100)pins the vertical scale from 0 to 100 so the bars can't exaggerate small differences — an honest scale matters. savefigstores the image, thenshowdisplays it.
Notice the pattern: prepare the data, draw, label, output. Every matplotlib chart you ever make follows it.
Try it yourself
Take the scores example and:
- Change the colours so the highest-scoring subject is gold and the rest are grey. Hint: build a
colourslist with a loop or comprehension that checksif score == max(scores). - Add
plt.grid(axis="y")beforeshow()to draw faint horizontal grid lines, making values easier to read. - Rotate long labels so they don't overlap:
plt.xticks(rotation=45). - Challenge: load the data from a CSV file instead of typing it in, then chart it. The plotting code won't change at all — that's the power of separating data from display.
A chart is the moment data becomes insight. When you understand the numbers behind a picture, you're doing exactly the kind of analysis that powers everything from sports stats to the recommendation systems you use every day.
Quick quiz
Test yourself and earn XP
What is the usual import line for plotting?
The standard, widely used convention is import matplotlib.pyplot as plt.
Which function draws a bar chart?
plt.bar() takes the category labels and their values and draws the bars.
What does plt.show() do?
plt.show() opens a window showing the figure you have built.
How do you save a chart as an image file?
plt.savefig("chart.png") writes the current figure to a PNG (or other) image file.
Why add plt.xlabel and plt.ylabel?
Labels explain what each axis represents, so the chart can actually be understood.
FAQ
Python ships with a large standard library, but matplotlib is a separate, third-party package, so you install it once with pip install matplotlib. After that you can import it in any program. If you use an environment like Anaconda or Google Colab, matplotlib is usually already there.
Most often you forgot plt.show(), which is what actually opens the window. In some setups (like a plain server, or certain online notebooks) no window can pop up; there, save the chart with plt.savefig("chart.png") and open the image file instead. In Jupyter or Colab the chart appears inline automatically.
Keep exploring
More in Coding