Running a Simple Bar Chart in Python

Leave a comment

In this post, we will look at how to run a simple bar chart in Python. We will use matplotlib.

Here’s the code. We will display a chart with x-axis showing “team” data vs y-axis showing “score”:

import matplotlib.pyplot as plt

team = ('A', 'B', 'C', 'D', 'E', 'F')
score = [9,2,5,4,8,6]

plt.bar(team, score)
plt.show()

Let’s run this in Visual Studio Code. Save the code with a .py extension, right-click and Run Python File in Terminal:

This will open a bar chart:

If we want to add labels to the axis and chart, we can use:

import matplotlib.pyplot as plt

team = ('A', 'B', 'C', 'D', 'E', 'F')
score = [9,2,5,4,8,6]

plt.xlabel('Team')
plt.ylabel('Score')
plt.title('Team Scores')

plt.bar(team, score)
plt.show()

This displays:

 

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, Power BI, Azure, and AI.

carldesouza.comLinkedIn Twitter | YouTube

 

Leave a Reply

Your email address will not be published. Required fields are marked *