Welcome folks today in this blog post we will be plotting bar graph of excel files
using matplotlib
and pandas
library in python All the full source code of the application is shown below.
Get Started
In order to get started you need to install the below libraries using the pip
command as shown below
pip install matplotlib
pip install pandas
pip install numpy
After installing all the above libraries you need to make an app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import pandas as pd import numpy as np import matplotlib.pyplot as plt excel_1_file = "one.xlsx" excel_2_file = 'two.xlsx' df_first_shift = pd.read_excel(excel_1_file,sheet_name="first") df_second_shift = pd.read_excel(excel_1_file,sheet_name="second") df_third_shift = pd.read_excel(excel_2_file) df_all = pd.concat([df_first_shift,df_second_shift,df_third_shift]) print(df_all) pivot = df_all.groupby(['shift']).mean() shift_productivity = pivot.loc[:,"Production Run Time (Min)":"Products Produced (Units)"] shift_productivity.plot(kind="bar") plt.show() |
As you can see we are importing the pandas
and matplotlib
library at the very top. And as you can see we have two excel files which we are first of all merging with the third one. We are adding all the content of the excel files inside the data frames. And we are using the method of concat()
to concatenate all the dataframes into one. And then we are grouping all the data using the groupby()
method to find the pivot element. And then we are calculating the mean()
method. And then we are plotting the data in the form of bar graph