All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Python Bar Graph With Labels

Last Updated : Mar 11, 2024

Python Bar Graph With Labels

In this tutorial we will show you the solution of python bar graph with labels, before starting the topic, first we have to understand what “Labels” mean. The label is the phrase or name of the bars in a bar chart.

Let’s get started.

Step By Step Guide On Python Bar Graph With Labels :-

For plotting the data in Python, we use the bar() function provided by the Matplotlib Library.

In this, we can pass our data as a parameter to visualize, but the default chart that is drawn on the given data doesn’t contain any value labels on each bar of the bar chart.

Since the default bar chart doesn’t contain any value label for each bar, it is difficult to analyze the exact value represented by the individual bars of the bar chart.

import matplotlib.pyplot as plt
student = [3,2,0,5,9,1]
weight = [30,35,40,45,50,55]
plt.bar(weight,student)
plt.title("Student’s data")
plt.xticks(rotation='vertical')
plt.yticks(rotation='vertical')
plt.xlabel("Weight of the students")
plt.ylabel("No.of students")
plt.show()
  1. The first line is an import statement that will import the “matplotlib.pyplot” library as “plt”.
  2. In the next line, there is a variable named “student”, which is a list that contains some numbers of students.
  3. In the next line there is another variable named “weight”, which is a list as well, that contains the values of weight for some students.
  4. In the next line, there is a function called plt.bar() that will create or generate a bar for you. This function will accept two values as parameters.
  5. The first one is the values of the x-axis co-ordinates. Here in this case, it will be the list variable “weight”.
  6. The second one is the values of the y-axis co-ordinates. In this case, it will be the list variable “student”.
  7. In the next line, there is a function called plt.title() that will display the title of the bar graph on the top of the graph. In this case, it will display “Student’s data”.
  8. In the next two lines, there are two different functions called “xticks()” and “yticks()”. The xticks() function can help you display the labels on the x-axis vertically.
  9. The yticks() function can help you display the labels on the y-axis vertically.
  10. In the next two lines, there are two functions called “xlabel()” and “ylabel()”.
  11. The xlabel() function will display the label you want to print on the x-axis of the graph.
  12. The ylabel() function will display the label you want to print on the y-axis of the graph.
  13. In the end, there is a function called plt.show() that will display the bar graph as the output.

Conclusion :-

So finally, in conclusion, we can say that with the help of this article, you can now plot a bar graph with labels in a Python program.

You can use the method mentioned above to display labels on the bar graph.

You can also provide value of the rotation parameter to display the labels on a certain angle.

I hope this tutorial on python bar graph with labels helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪