
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Plot a Simple Bar Chart in Python with Matplotlib
To plot a very simple bar chart from an input text file, we can take the following steps −
Make an empty list for bar names and heights.
Read a text file and iterate each line.
Append names and heights into lists.
Plot the bar using lists (Step 1).
To display the figure, use show() method.
Example
from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True bar_names = [] bar_heights = [] for line in open("test_data.txt", "r"): bar_name, bar_height = line.split() bar_names.append(bar_name) bar_heights.append(bar_height) plt.bar(bar_names, bar_heights) plt.show()
"test_data.txt" contains the following data −
Javed 75 Raju 65 Kiran 55 Rishi 95
Output
Advertisements