19/02/2026

Plotting Graphs

1️⃣ Time Series Trend Graph (Correct Way)

Python Code:

import pandas as pd
import matplotlib.pyplot as plt

df['Date'] = pd.to_datetime(df['Date'])

plt.figure(figsize=(10,5))
plt.plot(df['Date'], df['Price'])
plt.title("Time Series Trend")
plt.xlabel("Date")
plt.ylabel("Price")
plt.xticks(rotation=45)
plt.show()

Output:

✅ When We Use It:

  • When data has date / time column
  • Before applying ARIMA, SARIMA, LSTM
  • To check trend and pattern over years

🎯 Why We Use It:

  • To see upward/downward trend
  • To detect structural breaks
  • To identify seasonality
  • To check if data is stationary

2️⃣ Multiple Line Comparison Graph

Python Code:

plt.figure(figsize=(10,5))
plt.plot(df['Date'], df['Min_Price'], label="Min Price")
plt.plot(df['Date'], df['Max_Price'], label="Max Price")
plt.plot(df['Date'], df['Modal_Price'], label="Modal Price")
plt.legend()
plt.xticks(rotation=45)
plt.show()

Output:


✅ When We Use It:

  • When comparing Min, Max, Modal prices
  • When comparing multiple variables over time

🎯 Why We Use It:

  • To check spread between values
  • To analyze volatility
  • To see if variables move together
  • To detect market instability

3️⃣ Monthly Seasonal Pattern Graph

Python Code:

df['Month'] = df['Date'].dt.month
monthly_avg = df.groupby('Month')['Price'].mean()

plt.figure(figsize=(8,5))
plt.plot(monthly_avg.index, monthly_avg.values)
plt.title("Monthly Seasonal Pattern")
plt.xlabel("Month")
plt.ylabel("Average Price")
plt.show()

Output:



✅ When We Use It:

  • When data spans multiple years
  • When checking seasonality
  • Before applying SARIMA

🎯 Why We Use It:

  • To confirm seasonal pattern
  • To understand cyclic behavior
  • To detect repeating trends

4️⃣ Scatter Plot (Correlation Check)

Python Code:

plt.figure(figsize=(6,5))
plt.scatter(df['Min_Price'], df['Max_Price'])
plt.xlabel("Min Price")
plt.ylabel("Max Price")
plt.show()

Output:



✅ When We Use It:

  • Before Linear Regression
  • To check relationship between variables
  • For feature selection

🎯 Why We Use It:

  • To detect correlation
  • To check linearity
  • To detect outliers
  • To avoid useless predictors

5️⃣ Histogram (Distribution Check)

Python Code:

plt.figure(figsize=(6,5))
plt.hist(df['Price'], bins=30)
plt.xlabel("Price")
plt.ylabel("Frequency")
plt.show()

Output:



✅ When We Use It:

  • During EDA
  • Before regression modeling
  • To check normality

🎯 Why We Use It:

  • To check skewness
  • To detect heavy tails
  • To decide transformation

6️⃣ Box Plot (Outlier Detection)

Python Code:

plt.figure(figsize=(6,5))
plt.boxplot(df['Price'])
plt.show()

Output:



✅ When We Use It:

  • Before cleaning dataset
  • To detect extreme values
  • During preprocessing

🎯 Why We Use It:

  • Shows median
  • Shows IQR
  • Detects outliers visually
  • Helps cleaning decision

7️⃣ Correlation Matrix Heatmap

Python Code:

import seaborn as sns

plt.figure(figsize=(8,6))
corr = df.corr()
sns.heatmap(corr, annot=True)
plt.show()

Output:


✅ When We Use It:

  • Before ML modeling
  • When multiple numeric features exist
  • For feature selection

🎯 Why We Use It:

  • To detect multicollinearity
  • To remove redundant variables
  • To choose best predictors
  • To improve model accuracy

1️⃣ LINE PLOT (Most Important for You)

Python Code

plt.figure(figsize=(10,5))

plt.plot(data["Month"],
         data["Modal Price (Rs./Quintal)"],
         marker="o",
         linewidth=2)

plt.xlabel("Month")
plt.ylabel("Modal Price")
plt.title("Monthly Modal Price Trend")

plt.show()

Explanation of Each Function

Function What It Does
plt.figure(figsize=(10,5)) Sets graph size
plt.plot(x,y) Creates line graph
marker="o" Adds circle dots
linewidth=2 Controls line thickness
plt.xlabel() Name of X-axis
plt.ylabel() Name of Y-axis
plt.title() Graph title
plt.show() Displays graph

When We Use Line Plot?

  • Time series data
  • Monthly/Yearly crop prices
  • Trend analysis
  • Forecasting models (ARIMA, LSTM)
Because your crop price dataset is time-based. It helps to see trends, seasonality, and price movement.

2️⃣ HISTOGRAM

Python Code

plt.hist(data["Modal Price (Rs./Quintal)"],
         bins=10,
         edgecolor="black")

plt.xlabel("Modal Price")
plt.ylabel("Frequency")
plt.title("Distribution of Modal Price")

plt.show()

Explanation

Function What It Does
plt.hist() Creates histogram
bins=10 Divides data into 10 ranges
edgecolor Border color of bars

When We Use Histogram?

  • To check data distribution
  • Before normalization
  • Before regression models
  • To detect skewness
Histogram helps check if data is normally distributed and whether outliers exist.

3️⃣ SCATTER PLOT

Python Code

plt.scatter(data["Min Price (Rs./Quintal)"],
            data["Max Price (Rs./Quintal)"])

plt.xlabel("Min Price")
plt.ylabel("Max Price")
plt.title("Min vs Max Price Relationship")

plt.show()

When We Use Scatter Plot?

  • Check relationship between two variables
  • Before regression
  • Correlation checking
If points move upward → Positive correlation If downward → Negative correlation If random → No strong relation

4️⃣ BAR GRAPH

Python Code

yearly_avg = data.groupby("Year")["Modal Price (Rs./Quintal)"].mean()

plt.bar(yearly_avg.index,
        yearly_avg.values)

plt.xlabel("Year")
plt.ylabel("Average Modal Price")
plt.title("Yearly Average Modal Price")

plt.show()

When We Use Bar Graph?

  • Compare categories
  • Compare yearly performance
  • Compare ML model metrics
Bar graph is best for comparison of categories and yearly averages.

5️⃣ STACK / AREA PLOT

Python Code

plt.stackplot(data["Month"],
              data["Min Price (Rs./Quintal)"],
              data["Modal Price (Rs./Quintal)"],
              data["Max Price (Rs./Quintal)"],
              labels=["Min", "Modal", "Max"])

plt.xlabel("Month")
plt.ylabel("Price")
plt.title("Price Comparison Area Plot")
plt.legend()

plt.show()

When We Use It?

  • Compare contribution
  • Show composition of multiple variables
Shows which price component dominates over time.

6️⃣ PIE CHART

Python Code

avg_prices = [
    data["Min Price (Rs./Quintal)"].mean(),
    data["Modal Price (Rs./Quintal)"].mean(),
    data["Max Price (Rs./Quintal)"].mean()
]

labels = ["Min", "Modal", "Max"]

plt.pie(avg_prices,
        labels=labels,
        autopct="%1.1f%%")

plt.title("Average Price Contribution")

plt.show()

When We Use It?

  • To show percentage distribution
  • For presentation
  • For reports
Pie chart shows percentage contribution of each price component.
Share This
Previous Post
Next Post