As to perform the task by reading and learning its resources the task gives certain learnings that are :
Learning how to plot a DataFrame in Pandas makes it easy to turn your data into visual charts. You can create line plots, bar charts, histograms, and scatter plots right from the DataFrame.
This uses Matplotlib in the background, so you don’t need to write a lot of extra code. It helps you spot trends, patterns, and outliers quickly, making it easier to understand the data and make better decisions.
Learning Outcomes
- Create Visuals Easily: Learn to generate different types of plots (line, bar, scatter, histogram) directly from a DataFrame.
- Understand Data Trends: Spot trends, patterns, and outliers in the data quickly through visuals.
- Save Time: Use built-in plotting functions to create charts without needing to write extra code.
- Enhance Decision Making: Use visuals to better analyze data and make informed decisions.
Use of Pandas in Python for Plot a Dataframe
Code:
import pandas as pd import matplotlib.pyplot as plt # Create a sample DataFrame data = { 'Month': ['Jan', 'Feb', 'Mar', 'Apr', 'May'], 'Sales': [200, 300, 250, 400, 500], 'Expenses': [150, 200, 180, 220, 250] } df = pd.DataFrame(data) # Line chart for Sales and Expenses df.plot(x='Month', y=['Sales', 'Expenses'], kind='line', marker='o') plt.title('Sales and Expenses Over Time') plt.xlabel('Month') plt.ylabel('Amount') plt.grid(True) plt.show() # Bar chart for Sales df.plot(x='Month', y='Sales', kind='bar', color='green') plt.title('Sales by Month') plt.xlabel('Month') plt.ylabel('Sales') plt.show() # Scatter plot for Sales vs Expenses df.plot(x='Sales', y='Expenses', kind='scatter') plt.title('Sales vs Expenses') plt.xlabel('Sales') plt.ylabel('Expenses') plt.show() # Histogram for Sales df['Sales'].plot(kind='hist', bins=5, color='blue', alpha=0.7) plt.title('Sales Distribution') plt.xlabel('Sales') plt.show()
Explanation:
Here’s a explanation of the code:
The code helps you visualize data from a table (a DataFrame) by turning it into different types of charts. First, we create a small table with sales and expenses for five months. Then, we plot four different types of charts:
- Line Chart: This shows how sales and expenses changed over the months, with lines connecting the values.
- Bar Chart: This shows the sales for each month as bars, making it easy to compare them.
- Scatter Plot: This chart compares sales and expenses by plotting each month’s sales against its expenses. It helps to see if there’s any relationship between the two.
- Histogram: This chart shows how the sales values are spread out, with bars representing the number of months that fall within certain sales ranges.
Each chart helps you see the data in a different way, so you can better understand trends, patterns, and distributions. It’s a great way to make data more visual and easier to interpret!
Output :
- Line Chart:
- The chart will show two lines: one for Sales and one for Expenses. Both lines will have markers at each month (Jan, Feb, Mar, Apr, May), allowing you to see how both values change over time.
- X-axis: Months (Jan, Feb, Mar, Apr, May)
- Y-axis: Sales and Expenses values
- Bar Chart:
- This chart will show the Sales for each month as green bars. It allows you to compare the sales between each month visually.
- X-axis: Months (Jan, Feb, Mar, Apr, May)
- Y-axis: Sales values
- Scatter Plot:
- This plot will show the relationship between Sales and Expenses. Each point represents a month, with Sales on the x-axis and Expenses on the y-axis.
- X-axis: Sales values
- Y-axis: Expenses values
- Histogram:
- The histogram will display the distribution of Sales data. The x-axis will represent the ranges of sales values, and the y-axis will show how many months fall into each range.
- X-axis: Sales values (with bins representing ranges)
- Y-axis: Frequency (how many months fall within each range)
These plots help you understand different aspects of the data, like trends, comparisons, relationships, and distributions, in an easy-to-read visual format.
More resources to learn are :