In today’s world, data plays a critical role in the decision-making of any size of the organization. Analyzing data usually begins with its visualization and one of the most common ways of visualizing data is through graphs.
In this tutorial, we will learn how to generate graphs from Excel data using Python. We will use Python’s two powerful libraries: Pandas for Data Analysis and Matplotlib for data visualization.
Step 1: Install Necessary Libraries
Install Pandas, Numpy, Matplotlib, and xlrd using pip:
1 |
pip install pandas numpy matplotlib xlrd |
Step 2: Import Required Libraries
You need to import the necessary libraries into your Python script. Below is the Python code to import them:
1 2 |
import pandas as pd import matplotlib.pyplot as plt |
Step 3: Load Excel Data with pandas
Below is an example of Excel data with two columns “Months” and “Sales”. The file is named “sales_data.xlsx”.
Month,Sales Jan,100 Feb,120 Mar,190 Apr,110 May,105 Jun,120 Jul,135 Aug,145 Sep,130 Oct,120 Nov,115 Dec,130
Now, Let’s load this Excel data using the pandas read_excel function:
1 2 |
data = pd.read_excel('sales_data.xlsx') print(data) |
Output:
Month Sales 0 Jan 100 1 Feb 120 2 Mar 190 3 Apr 110 4 May 105 5 Jun 120 6 Jul 135 7 Aug 145 8 Sep 130 9 Oct 120 10 Nov 115 11 Dec 130
Step 4: Plot the Data
Now, use Matplotlib to visualize this data:
1 2 3 4 5 |
plt.plot(data['Month'], data['Sales']) plt.title('Sales Trend') plt.xlabel('Month') plt.ylabel('Sales') plt.show() |
Step 5: Full Code
1 2 3 4 5 6 7 8 9 10 |
import pandas as pd import matplotlib.pyplot as plt data = pd.read_excel('sales_data.xlsx') print(data) plt.plot(data['Month'], data['Sales']) plt.title('Sales Trend') plt.xlabel('Month') plt.ylabel('Sales') plt.show() |
Conclusion
That’s it! You have plotted Excel data using Python. Python provides powerful libraries such as Pandas for data manipulation and Matplotlib for data visualization which makes your job easier and more efficient.
So, start incorporating them into your workflow and save precious time and effort for important decision-making.