- November 25, 2020
- saptrxuy_learnit
- 0 Comments
- 1683 Views
- 0 Likes
- Python
Bar Graphs Using Matplotlib
by Razkummara
1. Introduction
2. Importing matplotlib & pyplot
3. Creating the list of Strings & integers
4. Setting the figure size(length,width)
5. Set the title for the figure
6. Creating the subplot for plotting the graph
7. Executing bar graph using names & values
8. Printing the graph
9. Executing another slot and executing the bar graph
1.Introduction▲
We use matplotlib to create a simple bar graph. Matplotlib is a Python 2D plotting library. Matplotlib can be used in Python scripts, the Python and IPython shells, the Jupyter notebook, web application servers, and four graphical user interface toolkits. Read more at official website: https://matplotlib.org/
2.Importing matplotlib & pyplot▲
matplotlib.pyplot is a collection of command style functions that make matplotlib work like MATLAB. Each pyplot function makes some change to a figure: e.g., creates a figure, creates a plotting area in a figure, plots some lines in a plotting area, decorates the plot with labels, etc. Read more at https://matplotlib.org/3.1.1/tutorials/introductory/pyplot.html
2.1.Code▲
10:%matplotlib inline 20: 30:# 200 Importing matplotlib & pylot 40:import matplotlib as mpl 50:import matplotlib.pyplot as plt 60: 2000030: 2000040:
3.Creating the list of Strings & integers▲
This is simple data that we will use for creating a bar graph.
3.1.Code▲
70:# 300 Creating the list of Strings & integers to be executed on bar graph 80:names = ['pcm','eng'] 90:values = [70,65] 100:
4.Setting the figure size(length,width)▲
We create a figure which will contain our plots, subplots and graphs etc.
4.1.Code▲
140:# 400 Setting the figure size upto which we need(length,width) 150:plt.figure(figsize = (15,3)) 160:
5.Set the title for the figure▲
5.1.Code▲
170:# 500 Creating the title for the figure 180:plt.suptitle('Student_id') 190:
6.Creating the subplot for plotting the graph▲
131 means we have 1 row and 3 columns. The last 1 says that we are plotting this graph in the first column. Similarly, if we say 243, it means we have 2 rows and 4 columns. We are creating our graph in 2nd row, 3rd column of 4 columns. A subplot 234 will be invalid as we have 2 rows and 3 columns and we cannot have 4th column of 3 columns.
6.1.Code▲
200:# 600 Creating the subplot for plotting the graph 210:plt.subplot(131) 220:
7.Executing bar graph using names & values▲
7.1.Code▲
230:# 700 Executing bar graph using names & values 240:plt.bar(names,values) 250:
8.Printing the graph▲
8.1.Code▲
300:# 800 Printing the graph 310:plt.show() 320:
9.Executing another slot and executing the bar graph▲
9.1.Code▲
110:names1 = ['P','C','M','eng'] 120:values1 = [70,70,70,70] 130: 260:# 700.1 Executing another slot and executing the bar graph 270:plt.subplot(132) 280:plt.bar(names1,values1) 290:
Leave a Comment